Skip to content

Commit

Permalink
Merge pull request #23 from lleger/lleger-add-run
Browse files Browse the repository at this point in the history
Add `run` method to top-level module
  • Loading branch information
trptcolin committed Feb 8, 2015
2 parents 82cdea1 + 0f0cf72 commit b9d15e1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
32 changes: 1 addition & 31 deletions bin/consistency_fail
Expand Up @@ -13,37 +13,7 @@ require File.join(Dir.pwd, "config", "environment")
$:<< File.join(File.dirname(__FILE__), "..", "lib")
require "consistency_fail"

def problems(models, introspector)
models.map do |m|
[m, introspector.missing_indexes(m)]
end.reject do |m, indexes|
indexes.empty?
end
end

models = ConsistencyFail::Models.new($LOAD_PATH)
models.preload_all

reporter = ConsistencyFail::Reporter.new

success = true

introspector = ConsistencyFail::Introspectors::ValidatesUniquenessOf.new
problems = problems(models.all, introspector)
reporter.report_validates_uniqueness_problems(problems)
success &&= problems.empty?

introspector = ConsistencyFail::Introspectors::HasOne.new
problems = problems(models.all, introspector)
reporter.report_has_one_problems(problems)
success &&= problems.empty?

introspector = ConsistencyFail::Introspectors::Polymorphic.new
problems = problems(models.all, introspector)
reporter.report_polymorphic_problems(problems)
success &&= problems.empty?

if success
if ConsistencyFail.run
exit 0
else
exit 1
Expand Down
38 changes: 38 additions & 0 deletions lib/consistency_fail.rb
Expand Up @@ -4,3 +4,41 @@
require 'consistency_fail/introspectors/has_one'
require 'consistency_fail/introspectors/polymorphic'
require 'consistency_fail/reporter'

module ConsistencyFail
def self.run
models = ConsistencyFail::Models.new($LOAD_PATH)
models.preload_all

reporter = ConsistencyFail::Reporter.new

success = true

introspector = ConsistencyFail::Introspectors::ValidatesUniquenessOf.new
problems = problems(models.all, introspector)
reporter.report_validates_uniqueness_problems(problems)
success &&= problems.empty?

introspector = ConsistencyFail::Introspectors::HasOne.new
problems = problems(models.all, introspector)
reporter.report_has_one_problems(problems)
success &&= problems.empty?

introspector = ConsistencyFail::Introspectors::Polymorphic.new
problems = problems(models.all, introspector)
reporter.report_polymorphic_problems(problems)
success &&= problems.empty?

success
end

private

def self.problems(models, introspector)
models.map do |m|
[m, introspector.missing_indexes(m)]
end.reject do |m, indexes|
indexes.empty?
end
end
end

0 comments on commit b9d15e1

Please sign in to comment.