Skip to content

Commit

Permalink
Remove simplecof and add rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Bassett committed Mar 14, 2017
1 parent 6c6025b commit 6c1e54e
Show file tree
Hide file tree
Showing 18 changed files with 230 additions and 217 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*.gem
.bundle
coverage
.yardoc
Gemfile.lock
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
AllCops:
Exclude:
- "bin/**/*"
- "mache.gemspec"
- "rein.gemspec"

Metrics/AbcSize:
Max: 27
Expand Down
6 changes: 4 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"
require "rubocop/rake_task"

RSpec::Core::RakeTask.new
RSpec::Core::RakeTask.new(:spec)
RuboCop::RakeTask.new

task default: :spec
task default: [:spec, :rubocop]
49 changes: 21 additions & 28 deletions lib/rein.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
module Rein
module Constraint
end
end

RC = Rein::Constraint

require "active_record"
require "active_support/core_ext/hash"
require "active_support/inflector"
require 'active_record/connection_adapters/abstract_mysql_adapter'
require "active_record/connection_adapters/abstract_mysql_adapter"

require "rein/constraint/primary_key"
require "rein/constraint/foreign_key"
Expand All @@ -17,25 +8,27 @@ module Constraint
require "rein/constraint/presence"
require "rein/view"

module ActiveRecord::ConnectionAdapters
class MysqlAdapter < AbstractAdapter
include RC::PrimaryKey
include RC::ForeignKey
include Rein::View
end
module ActiveRecord
module ConnectionAdapters # :nodoc:
class MysqlAdapter < AbstractAdapter # :nodoc:
include Rein::Constraint::PrimaryKey
include Rein::Constraint::ForeignKey
include Rein::View
end

class Mysql2Adapter < AbstractMysqlAdapter
include RC::PrimaryKey
include RC::ForeignKey
include Rein::View
end
class Mysql2Adapter < AbstractMysqlAdapter # :nodoc:
include Rein::Constraint::PrimaryKey
include Rein::Constraint::ForeignKey
include Rein::View
end

class PostgreSQLAdapter < AbstractAdapter
include RC::PrimaryKey
include RC::ForeignKey
include RC::Inclusion
include RC::Numericality
include RC::Presence
include Rein::View
class PostgreSQLAdapter < AbstractAdapter # :nodoc:
include Rein::Constraint::PrimaryKey
include Rein::Constraint::ForeignKey
include Rein::Constraint::Inclusion
include Rein::Constraint::Numericality
include Rein::Constraint::Presence
include Rein::View
end
end
end
88 changes: 43 additions & 45 deletions lib/rein/constraint/foreign_key.rb
Original file line number Diff line number Diff line change
@@ -1,66 +1,64 @@
module RC
module ForeignKey
def add_foreign_key_constraint(referencing_table, referenced_table, options = {})
referencing_attribute = (options[:referencing] || "#{referenced_table.to_s.singularize}_id").to_sym
referenced_attribute = (options[:referenced] || "id").to_sym
module Rein
module Constraint
# This module contains methods for defining foreign key constraints.
module ForeignKey
def add_foreign_key_constraint(referencing_table, referenced_table, options = {})
referencing_attribute = (options[:referencing] || "#{referenced_table.to_s.singularize}_id").to_sym
referenced_attribute = (options[:referenced] || "id").to_sym

name = options[:name] || default_constraint_name(referencing_table, referencing_attribute)
name = options[:name] || default_constraint_name(referencing_table, referencing_attribute)

sql = "ALTER TABLE #{referencing_table}".tap do |sql|
sql = "ALTER TABLE #{referencing_table}"
sql << " ADD CONSTRAINT #{name}"
sql << " FOREIGN KEY (#{referencing_attribute})"
sql << " REFERENCES #{referenced_table} (#{referenced_attribute})"
sql << " ON DELETE #{referential_action(options[:on_delete])}" if options[:on_delete].present?
sql << " ON UPDATE #{referential_action(options[:on_update])}" if options[:on_update].present?
end

execute(sql)
execute(sql)

# A foreign key constraint doesn't have an implicit index.
add_index(referencing_table, referencing_attribute) if options[:add_index] == true
end
alias_method :add_foreign_key, :add_foreign_key_constraint
# A foreign key constraint doesn't have an implicit index.
add_index(referencing_table, referencing_attribute) if options[:add_index] == true
end
alias add_foreign_key add_foreign_key_constraint

def remove_foreign_key_constraint(referencing_table, referenced_table, options = {})
referencing_attribute = options[:referencing] || "#{referenced_table.to_s.singularize}_id".to_sym
def remove_foreign_key_constraint(referencing_table, referenced_table, options = {})
referencing_attribute = options[:referencing] || "#{referenced_table.to_s.singularize}_id".to_sym

name = options[:name] || default_constraint_name(referencing_table, referencing_attribute)
name = options[:name] || default_constraint_name(referencing_table, referencing_attribute)

if is_a_mysql_adapter?
execute "ALTER TABLE #{referencing_table} DROP FOREIGN KEY #{name}"
else
execute "ALTER TABLE #{referencing_table} DROP CONSTRAINT #{name}"
if mysql_adapter?
execute "ALTER TABLE #{referencing_table} DROP FOREIGN KEY #{name}"
else
execute "ALTER TABLE #{referencing_table} DROP CONSTRAINT #{name}"
end

# A foreign key constraint doesn't have an implicit index.
remove_index(referencing_table, referencing_attribute) if options[:remove_index] == true
end
alias remove_foreign_key remove_foreign_key_constraint

# A foreign key constraint doesn't have an implicit index.
remove_index(referencing_table, referencing_attribute) if options[:remove_index] == true
end
alias_method :remove_foreign_key, :remove_foreign_key_constraint
private

private
def referential_action(action)
case action.to_sym
when :no_action
"NO ACTION"
when :cascade
"CASCADE"
when :restrict
"RESTRICT"
when :nullify
"SET NULL"
when :default
"SET DEFAULT"
else
raise "Unknown referential action '#{action}'"
def referential_action(action)
case action.to_sym
when :no_action then "NO ACTION"
when :cascade then "CASCADE"
when :restrict then "RESTRICT"
when :nullify then "SET NULL"
when :default then "SET DEFAULT"
else
raise "Unknown referential action '#{action}'"
end
end
end

def is_a_mysql_adapter?
self.class.to_s =~ /Mysql[2]?Adapter/
end
def mysql_adapter?
self.class.to_s =~ /Mysql[2]?Adapter/
end

def default_constraint_name(referencing_table, referencing_attribute)
"#{referencing_table}_#{referencing_attribute}_fk".to_sym
def default_constraint_name(referencing_table, referencing_attribute)
"#{referencing_table}_#{referencing_attribute}_fk".to_sym
end
end
end
end
21 changes: 12 additions & 9 deletions lib/rein/constraint/inclusion.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
require 'active_record/connection_adapters/abstract/quoting'
require "active_record/connection_adapters/abstract/quoting"

module RC
module Inclusion
include ActiveRecord::ConnectionAdapters::Quoting
module Rein
module Constraint
# This module contains methods for defining inclusion constraints.
module Inclusion
include ActiveRecord::ConnectionAdapters::Quoting

def add_inclusion_constraint(table, attribute, options = {})
name = "#{table}_#{attribute}"
values = options[:in].map {|value| quote(value) }.join(", ")
conditions = "#{attribute} IN (#{values})"
execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})")
def add_inclusion_constraint(table, attribute, options = {})
name = "#{table}_#{attribute}"
values = options[:in].map { |value| quote(value) }.join(", ")
conditions = "#{attribute} IN (#{values})"
execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})")
end
end
end
end
37 changes: 20 additions & 17 deletions lib/rein/constraint/numericality.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
module RC
module Numericality
OPERATORS = {
:greater_than => :>,
:greater_than_or_equal_to => :>=,
:equal_to => :"=",
:not_equal_to => :"!=",
:less_than => :<,
:less_than_or_equal_to => :<=
}.freeze
module Rein
module Constraint
# This module contains methods for defining numericality constraints.
module Numericality
OPERATORS = {
greater_than: :>,
greater_than_or_equal_to: :>=,
equal_to: :"=",
not_equal_to: :"!=",
less_than: :<,
less_than_or_equal_to: :<=
}.freeze

def add_numericality_constraint(table, attribute, options = {})
name = "#{table}_#{attribute}"
def add_numericality_constraint(table, attribute, options = {})
name = "#{table}_#{attribute}"

conditions = OPERATORS.slice(*options.keys).map do |key, operator|
value = options[key]
[attribute, operator, value].join(" ")
end.join(" AND ")
conditions = OPERATORS.slice(*options.keys).map do |key, operator|
value = options[key]
[attribute, operator, value].join(" ")
end.join(" AND ")

execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})")
execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})")
end
end
end
end
17 changes: 10 additions & 7 deletions lib/rein/constraint/presence.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
module RC
module Presence
include ActiveRecord::ConnectionAdapters::Quoting
module Rein
module Constraint
# This module contains methods for defining presence constraints.
module Presence
include ActiveRecord::ConnectionAdapters::Quoting

def add_presence_constraint(table, attribute)
name = "#{table}_#{attribute}"
conditions = "#{attribute} !~ '^\s*$'"
execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})")
def add_presence_constraint(table, attribute)
name = "#{table}_#{attribute}"
conditions = "#{attribute} !~ '^\s*$'"
execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})")
end
end
end
end
15 changes: 9 additions & 6 deletions lib/rein/constraint/primary_key.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
module RC
module PrimaryKey
def add_primary_key(table, options = {})
attribute = (options[:column] || "id").to_sym
sql = "ALTER TABLE #{table} ADD PRIMARY KEY (#{attribute})"
execute(sql)
module Rein
module Constraint
# This module contains methods for defining primary key constraints.
module PrimaryKey
def add_primary_key(table, options = {})
attribute = (options[:column] || "id").to_sym
sql = "ALTER TABLE #{table} ADD PRIMARY KEY (#{attribute})"
execute(sql)
end
end
end
end
1 change: 1 addition & 0 deletions lib/rein/view.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module Rein
# This module contains methods for defining views.
module View
def create_view(view_name, sql)
sql = "CREATE VIEW #{view_name} AS #{sql}"
Expand Down
39 changes: 20 additions & 19 deletions rein.gemspec
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
$:.push File.expand_path("../lib", __FILE__)
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

require "rein/version"

Gem::Specification.new do |s|
s.name = "rein"
s.version = Rein::VERSION
s.author = "Josh Bassett"
s.email = "[email protected]"
s.homepage = "http://github.com/nullobject/rein"
s.summary = "Database constraints made easy for ActiveRecord."
s.description = "Rein adds bunch of methods to your ActiveRecord migrations so you can easily tame your database."
Gem::Specification.new do |spec|
spec.name = "rein"
spec.version = Rein::VERSION
spec.author = "Joshua Bassett"
spec.email = "[email protected]"
spec.summary = "Database constraints made easy for ActiveRecord."
spec.description = "Rein adds bunch of methods to your ActiveRecord migrations so you can easily tame your database."
spec.homepage = "http://github.com/nullobject/rein"
spec.license = "MIT"
spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end
spec.require_paths = ["lib"]

s.rubyforge_project = "rein"
spec.add_runtime_dependency "activerecord", ">= 3.2.0"

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map {|f| File.basename(f) }

s.add_development_dependency "rake"
s.add_development_dependency "rspec"
s.add_development_dependency "simplecov"

s.add_runtime_dependency "activerecord", '>= 3.2.0'
spec.add_development_dependency "bundler", "~> 1.14"
spec.add_development_dependency "rake", "~> 12.0"
spec.add_development_dependency "rspec", "~> 3.5"
spec.add_development_dependency "rubocop", "~> 0.47"
end
Loading

0 comments on commit 6c1e54e

Please sign in to comment.