validb 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6cf59a57d2e7a7218ba7e757ff4f749d6580582d
4
- data.tar.gz: 84a2ece5b8606e99232db4441be80cab9ad50cd6
3
+ metadata.gz: 28235bb827e10873757a172aa4341de2125825b5
4
+ data.tar.gz: 534c056bd73c20c1a720283757e569d64bfa9cec
5
5
  SHA512:
6
- metadata.gz: f0bacdfd14f9864abb0fae1a8031041c3ca77ba214381a0d2febed0cfda3d6ff297f2bcde18f1c49475c5d3ce0d09c9d4a0a152e790e451ee9c6ca0d3ae5bb22
7
- data.tar.gz: 43f299bccc537dca44c532eeaaee011c88ada7b328186b6bf94e2629ac9b996749b8153b55651430ec285939747e5fed6936a8aa6101fe2e068835193a1c44f9
6
+ metadata.gz: 7b58b722f5f0f96b7aea4b0f9c89e228682145c4a3876dbaf9a3bd71fb7a56d5fcc60f2b1ac624c6c6c58fcbdc6c777762644dd64812b32338db1fb5ea09f9ca
7
+ data.tar.gz: 61ded8105b8e95ff8c116adf0ebb88e9ec51bc24e960d21595437b9b8b310c4d1681f6e8731ec19ec1392875dea5b5534478656fd8d5e2e07530bd8062c4a1e7
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.1.0, released 2013-03-26
2
+ * Rake task now takes parameters differently
3
+ * Classes now use instances, not class methods
4
+ * Add new logger class to allow for custom logging
5
+
1
6
  ## 0.0.3, released 2013-03-25
2
7
  * Use table_exists? in place of table name
3
8
  * Add progress indicator during run
data/README.md CHANGED
@@ -13,11 +13,14 @@ Check your database for invalid models.
13
13
 
14
14
  ## Usage
15
15
 
16
- ### All models
16
+ ### All models, default logger
17
17
  rake validb:validate
18
18
 
19
19
  ### Specific models
20
- rake validb:validate[Model,Model]
20
+ rake validb:validate models="Model,Model"
21
+
22
+ ### Specific logger
23
+ rake validb:validate logger="Validb::ConsoleLogger"
21
24
 
22
25
  ## License
23
26
  Released under the MIT License
@@ -1,7 +1,15 @@
1
1
  namespace :validb do
2
2
  desc "Check DB for invalid records"
3
- task :validate, [:model_names] => [:environment] do |t, args|
3
+ task :validate => :environment do
4
+ models = ENV["models"] || ""
5
+ logger = ENV["logger"] || "Validb::ConsoleLogger"
6
+
7
+ # force all models to load so we can find them
4
8
  Rails.application.eager_load!
5
- Validb::Checker.check(args[:model_names])
9
+ finder = Validb::Finder.new(models)
10
+
11
+ console_logger = logger.constantize.new
12
+ checker = Validb::Checker.new(console_logger)
13
+ checker.check(finder.models)
6
14
  end
7
15
  end
@@ -0,0 +1,15 @@
1
+ module Validb
2
+ class Batcher
3
+
4
+ def initialize(logger)
5
+ @record_validator = RecordValidator.new(logger)
6
+ end
7
+
8
+ def validate(record_batch)
9
+ puts "."
10
+ record_batch.each do |record|
11
+ @record_validator.validate(record)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,11 +1,13 @@
1
1
  module Validb
2
2
  class Checker
3
- class << self
4
- def check(model_names)
5
- models = Validb::Finder.get_models(model_names)
6
- models.each do |model|
7
- Validb::ModelValidator.validate(model)
8
- end
3
+
4
+ def initialize(logger)
5
+ @model_validator = Validb::ModelValidator.new(logger)
6
+ end
7
+
8
+ def check(models)
9
+ models.each do |model|
10
+ @model_validator.validate(model)
9
11
  end
10
12
  end
11
13
  end
@@ -0,0 +1,8 @@
1
+ module Validb
2
+ class ConsoleLogger
3
+
4
+ def out(record_hash)
5
+ puts "#{record_hash[:model]}:#{record_hash[:id]} - #{record_hash[:error_messages]}"
6
+ end
7
+ end
8
+ end
data/lib/validb/finder.rb CHANGED
@@ -1,24 +1,22 @@
1
1
  module Validb
2
2
  class Finder
3
- class << self
4
- def get_models(model_name_string)
5
- model_name_string ||= ""
6
- filter_models(model_name_string.split(",").map(&:strip))
7
- end
8
3
 
9
- private
4
+ def initialize(model_name_string)
5
+ @model_name_string = model_name_string || ""
6
+ end
7
+
8
+ def models
9
+ model_names.empty? ? all : all.select { |model| model_names.include?(model.name) }
10
+ end
10
11
 
11
- def all
12
- @models ||= ActiveRecord::Base.descendants.select { |model| model.table_exists? }
13
- end
12
+ private
13
+
14
+ def model_names
15
+ @model_names ||= @model_name_string.split(",").map(&:strip)
16
+ end
14
17
 
15
- def filter_models(model_names)
16
- if model_names.any?
17
- all.select { |model| model_names.include?(model.name) }
18
- else
19
- all
20
- end
21
- end
18
+ def all
19
+ @models ||= ActiveRecord::Base.descendants.select { |model| model.table_exists? }
22
20
  end
23
21
  end
24
22
  end
@@ -1,21 +1,14 @@
1
1
  module Validb
2
2
  class ModelValidator
3
- class << self
4
- def validate(model)
5
- puts "Checking #{model}"
6
3
 
7
- model.find_in_batches do |batch|
8
- validate_batch(batch)
9
- end
10
- end
11
-
12
- private
4
+ def initialize(logger)
5
+ @batcher = Validb::Batcher.new(logger)
6
+ end
13
7
 
14
- def validate_batch(record_batch)
15
- puts "."
16
- record_batch.each do |record|
17
- RecordValidator.validate(record)
18
- end
8
+ def validate(model)
9
+ puts "Checking #{model}"
10
+ model.find_in_batches do |record_batch|
11
+ @batcher.validate(record_batch)
19
12
  end
20
13
  end
21
14
  end
@@ -1,8 +1,19 @@
1
1
  module Validb
2
2
  class RecordValidator
3
- def self.validate(record)
3
+
4
+ def initialize(logger)
5
+ @logger = logger
6
+ end
7
+
8
+ def validate(record)
4
9
  if !record.valid?
5
- puts "#{record.class.name}:#{record.id} - #{record.errors.full_messages.join(',')}"
10
+ @logger.out(
11
+ {
12
+ model: record.class.name,
13
+ id: record.id,
14
+ error_messages: record.errors.full_messages.join(',')
15
+ }
16
+ )
6
17
  end
7
18
  end
8
19
  end
@@ -1,3 +1,3 @@
1
1
  module Validb
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/validb.rb CHANGED
@@ -5,8 +5,10 @@ require 'validb/version'
5
5
 
6
6
  module Validb
7
7
  require 'validb/railtie' if defined?(Rails)
8
+ autoload :ConsoleLogger, 'validb/console_logger'
8
9
  autoload :Checker, 'validb/checker'
9
10
  autoload :Finder, 'validb/finder'
10
- autoload :RecordValidator, 'validb/record_validator'
11
11
  autoload :ModelValidator, 'validb/model_validator'
12
+ autoload :Batcher, 'validb/batcher'
13
+ autoload :RecordValidator, 'validb/record_validator'
12
14
  end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Validb::Batcher do
4
+ describe "#initialize" do
5
+ it "creates a batcher" do
6
+ logger = double('logger')
7
+ Validb::RecordValidator.should_receive(:new).with(logger)
8
+ Validb::Batcher.new(logger)
9
+ end
10
+ end
11
+
12
+ describe "#validate" do
13
+ it "validates the batch of records" do
14
+ record_batch = [Blog.new(title: "Title")]
15
+ logger = double('logger')
16
+ batcher = Validb::Batcher.new(logger)
17
+
18
+ $stdout.should_receive(:puts).with(".")
19
+ batcher.validate(record_batch)
20
+ end
21
+ end
22
+ end
@@ -1,23 +1,25 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Validb::Checker do
4
- describe ".check" do
5
- context "without specified models" do
6
- it "validates the passed in model's records" do
7
- Validb::ModelValidator.should_receive(:validate).with(Blog)
8
- Validb::ModelValidator.should_receive(:validate).with(Post)
9
- Validb::ModelValidator.should_receive(:validate).with(Comment)
10
- Validb::Checker.check("")
11
- end
4
+ describe "#initialize" do
5
+ it "creates a checker" do
6
+ logger = double('logger')
7
+ Validb::ModelValidator.should_receive(:new).with(logger)
8
+ Validb::Checker.new(logger)
12
9
  end
10
+ end
11
+
12
+ describe "#check" do
13
+ it "validates the passed in models records" do
14
+ logger = double('logger')
15
+ model_validator = double('model_validator')
16
+ Validb::ModelValidator.should_receive(:new).with(logger).and_return(model_validator)
17
+ checker = Validb::Checker.new(logger)
18
+
19
+ model_validator.should_receive(:validate).with(Blog)
20
+ model_validator.should_receive(:validate).with(Post)
13
21
 
14
- context "with specified models" do
15
- it "validates the passed in model's records" do
16
- Validb::ModelValidator.should_receive(:validate).with(Blog)
17
- Validb::ModelValidator.should_receive(:validate).with(Post)
18
- Validb::ModelValidator.should_not_receive(:validate).with(Comment)
19
- Validb::Checker.check("Blog, Post")
20
- end
22
+ checker.check([Blog, Post])
21
23
  end
22
24
  end
23
25
  end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe Validb::ConsoleLogger do
4
+ describe "#out" do
5
+ it "prints the hash output" do
6
+ hash = { model: Blog, id: 1, error_messages: "Error message" }
7
+
8
+ console_logger = Validb::ConsoleLogger.new
9
+ $stdout.should_receive(:puts).with("Blog:1 - Error message")
10
+ console_logger.out(hash)
11
+ end
12
+ end
13
+ end
@@ -4,17 +4,26 @@ describe Validb::Finder do
4
4
  class NoTable < ActiveRecord::Base
5
5
  end
6
6
 
7
- describe ".get_models" do
7
+ describe "#models" do
8
8
  context "with an empty string" do
9
- Validb::Finder.get_models("").should =~ [Post, Blog, Comment]
9
+ it "returns all the models with tables" do
10
+ finder = Validb::Finder.new("")
11
+ finder.models.should =~ [Post, Blog, Comment]
12
+ end
10
13
  end
11
14
 
12
15
  context "valid model names" do
13
- Validb::Finder.get_models("Post, Blog").should =~ [Post, Blog]
16
+ it "returns all the selected models with tables" do
17
+ finder = Validb::Finder.new("Post, Blog")
18
+ finder.models.should =~ [Post, Blog]
19
+ end
14
20
  end
15
21
 
16
22
  context "with invalid model names" do
17
- Validb::Finder.get_models("Post, Blog, Fake").should =~ [Post, Blog]
23
+ it "returns all the valid models with tables" do
24
+ finder = Validb::Finder.new("Post, Blog, Fake")
25
+ finder.models.should =~ [Post, Blog]
26
+ end
18
27
  end
19
28
  end
20
29
  end
@@ -1,15 +1,22 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Validb::ModelValidator do
4
- describe "#validate" do
5
- context "with a model" do
6
- it "validates the records of the model" do
7
- model = Blog
4
+ describe "#initialize" do
5
+ it "creates a model validator" do
6
+ logger = double('logger')
7
+ Validb::Batcher.should_receive(:new).with(logger)
8
+ Validb::ModelValidator.new(logger)
9
+ end
10
+ end
8
11
 
9
- $stdout.should_receive(:puts).with("Checking Blog")
12
+ describe "#validate" do
13
+ it "validates the records of the model" do
14
+ model = Blog
15
+ logger = double('logger')
16
+ model_validator = Validb::ModelValidator.new(logger)
10
17
 
11
- Validb::ModelValidator.validate(model)
12
- end
18
+ $stdout.should_receive(:puts).with("Checking Blog")
19
+ model_validator.validate(model)
13
20
  end
14
21
  end
15
22
  end
@@ -4,22 +4,29 @@ describe Validb::RecordValidator do
4
4
  describe "#validate" do
5
5
  context "with a valid record" do
6
6
  it "validates the passed in record" do
7
+ logger = double(:logger)
8
+ record_validator = Validb::RecordValidator.new(logger)
7
9
  record = Blog.new(title: "title")
8
10
 
9
- $stdout.should_not_receive(:puts)
10
-
11
- Validb::RecordValidator.validate(record)
11
+ logger.should_not_receive(:out)
12
+ record_validator.validate(record)
12
13
  end
13
14
  end
14
15
 
15
16
  context "with an invalid record" do
16
17
  it "outputs the record information" do
18
+ logger = double(:logger)
19
+ record_validator = Validb::RecordValidator.new(logger)
17
20
  record = Blog.new
18
21
  record.save(validate: false)
19
22
 
20
- $stdout.should_receive(:puts).with("Blog:#{record.id} - Title can't be blank")
21
-
22
- Validb::RecordValidator.validate(record)
23
+ hash = {
24
+ model: "Blog",
25
+ id: record.id,
26
+ error_messages: "Title can't be blank"
27
+ }
28
+ logger.should_receive(:out).with(hash)
29
+ record_validator.validate(record)
23
30
  end
24
31
  end
25
32
  end
@@ -2,6 +2,6 @@ require 'spec_helper'
2
2
 
3
3
  describe Validb do
4
4
  it "returns the version" do
5
- Validb::VERSION.should == "0.0.3"
5
+ Validb::VERSION.should == "0.1.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joey Geiger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-25 00:00:00.000000000 Z
11
+ date: 2013-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,14 +38,18 @@ files:
38
38
  - Rakefile
39
39
  - lib/tasks/validb.rake
40
40
  - lib/validb.rb
41
+ - lib/validb/batcher.rb
41
42
  - lib/validb/checker.rb
43
+ - lib/validb/console_logger.rb
42
44
  - lib/validb/finder.rb
43
45
  - lib/validb/model_validator.rb
44
46
  - lib/validb/railtie.rb
45
47
  - lib/validb/record_validator.rb
46
48
  - lib/validb/version.rb
47
49
  - spec/spec_helper.rb
50
+ - spec/validb/batcher_spec.rb
48
51
  - spec/validb/checker_spec.rb
52
+ - spec/validb/console_logger_spec.rb
49
53
  - spec/validb/finder_spec.rb
50
54
  - spec/validb/model_validator_spec.rb
51
55
  - spec/validb/record_validator_spec.rb