validb 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 566534268bb605006d7cf55057b4a464288d06b8
4
+ data.tar.gz: 4a537ce0ff1455021aa66f2bb0b9c36f363c1a33
5
+ SHA512:
6
+ metadata.gz: 8d3d8f204fce01e3cc0921be0e1feb235835779c7a33eeb67c574ddbf1b2988f29d431dcaf7fbff0f57a17a1b38c43062421c5de860e1b2a73077a34ab34605c
7
+ data.tar.gz: 210d1d1e919cb341f91cbb4ca4fe66c0e2df7424243b25a7d221460affc776c70ccda9ac6131bdbb14d3c086d57af659118acc074d3c6077962149cb84e04cec
data/.gitignore ADDED
@@ -0,0 +1,45 @@
1
+ !.gitignore
2
+ *.gem
3
+ *.rbc
4
+ *.sw[a-p]
5
+ *.tmproj
6
+ *.tmproject
7
+ *.un~
8
+ *~
9
+ .Spotlight-V100
10
+ .Trashes
11
+ ._*
12
+ .bundle
13
+ .config
14
+ .directory
15
+ .elc
16
+ .emacs.desktop
17
+ .emacs.desktop.lock
18
+ .idea
19
+ .redcar
20
+ .rvmrc
21
+ .yardoc
22
+ Desktop.ini
23
+ Gemfile.lock
24
+ Icon?
25
+ InstalledFiles
26
+ Session.vim
27
+ \#*\#
28
+ _yardoc
29
+ auto-save-list
30
+ coverage
31
+ doc/
32
+ lib/bundler/man
33
+ pkg
34
+ pkg/*
35
+ rdoc
36
+ spec/reports
37
+ spec/sandbox
38
+ test/tmp
39
+ test/version_tmp
40
+ tmp
41
+ tmtags
42
+ tramp
43
+ .rbx
44
+ b/
45
+ vendor/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ ## 0.0.1, released 2013-03-24
2
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org/'
2
+
3
+ gemspec
4
+
5
+ gem "rake"
6
+ gem "pry"
7
+
8
+ gem "rspec"
9
+
10
+ gem "sqlite3"
11
+ gem "activerecord"
data/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # Validb
2
+
3
+ ## Description
4
+
5
+ Check your database for invalid models.
6
+
7
+ ## Installation
8
+ gem install validb
9
+
10
+ ### Rails 3 In Gemfile
11
+
12
+ gem 'validb'
13
+
14
+ ## Usage
15
+
16
+ ### All models
17
+ rake validb:validate
18
+
19
+ ### Specific models
20
+ rake validb:validate[Model,Model]
21
+
22
+ ## License
23
+ Released under the MIT License
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ task :default => :spec
5
+
6
+ def bundle_exec(command)
7
+ sh %Q{bundle update && bundle exec #{command}}
8
+ end
9
+
10
+ def bundle_install_dependencies
11
+ sh %Q{bundle install --path vendor/bundle}
12
+ end
13
+
14
+ desc "Run all specs"
15
+ task "spec" do
16
+ bundle_exec("rspec spec")
17
+ end
18
+
19
+ desc "Install dependencies"
20
+ task "dependencies" do
21
+ bundle_install_dependencies
22
+ end
@@ -0,0 +1,7 @@
1
+ namespace :validb do
2
+ desc "Check DB for invalid records"
3
+ task :validate, [:model_names] => [:environment] do |t, args|
4
+ Rails.application.eager_load!
5
+ Validb::Checker.check(args[:model_names])
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ module Validb
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
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,24 @@
1
+ module Validb
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
+
9
+ private
10
+
11
+ def all
12
+ @models ||= ActiveRecord::Base.descendants.select { |model| model.table_name }
13
+ end
14
+
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
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,21 @@
1
+ module Validb
2
+ class ModelValidator
3
+ class << self
4
+ def validate(model)
5
+ puts "Checking #{model}"
6
+
7
+ model.find_in_batches do |batch|
8
+ validate_batch(batch)
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def validate_batch(record_batch)
15
+ record_batch.each do |record|
16
+ RecordValidator.validate(record)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ require 'validb'
2
+ require 'rails'
3
+
4
+ module Validb
5
+ class Railtie < Rails::Railtie
6
+ railtie_name :validb
7
+
8
+ rake_tasks do
9
+ load "tasks/validb.rake"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ module Validb
2
+ class RecordValidator
3
+ def self.validate(record)
4
+ if !record.valid?
5
+ puts "#{record.id} - #{record.errors.full_messages.join(',')}"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Validb
2
+ VERSION = "0.0.1"
3
+ end
data/lib/validb.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "active_record"
2
+ require "active_support/concern"
3
+ require "active_support/core_ext/module/attribute_accessors"
4
+ require 'validb/version'
5
+
6
+ module Validb
7
+ require 'validb/railtie' if defined?(Rails)
8
+ autoload :Checker, 'validb/checker'
9
+ autoload :Finder, 'validb/finder'
10
+ autoload :RecordValidator, 'validb/record_validator'
11
+ autoload :ModelValidator, 'validb/model_validator'
12
+ end
@@ -0,0 +1,44 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/validb')
2
+
3
+ require "pry"
4
+
5
+ RSpec.configure do |config|
6
+ config.treat_symbols_as_metadata_keys_with_true_values = true
7
+ config.run_all_when_everything_filtered = true
8
+ end
9
+
10
+ RSpec::Matchers::OperatorMatcher.register(ActiveRecord::Relation, '=~', RSpec::Matchers::BuiltIn::MatchArray)
11
+
12
+ ActiveRecord::Base.establish_connection(
13
+ adapter: 'sqlite3',
14
+ database: ':memory:'
15
+ )
16
+
17
+ ActiveRecord::Schema.define do
18
+ self.verbose = false
19
+
20
+ create_table :blogs, :force => true do |t|
21
+ t.string :title
22
+ t.timestamps
23
+ end
24
+
25
+ create_table :posts, :force => true do |t|
26
+ t.string :title
27
+ t.timestamps
28
+ end
29
+
30
+ create_table :comments, :force => true do |t|
31
+ t.string :body
32
+ t.timestamps
33
+ end
34
+ end
35
+
36
+ class Blog < ActiveRecord::Base
37
+ validates :title, presence: true
38
+ end
39
+
40
+ class Post < ActiveRecord::Base
41
+ end
42
+
43
+ class Comment < ActiveRecord::Base
44
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
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
12
+ end
13
+
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
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Validb::Finder do
4
+ describe ".get_models" do
5
+ context "with an empty string" do
6
+ Validb::Finder.get_models("").should =~ [Post, Blog, Comment]
7
+ end
8
+
9
+ context "valid model names" do
10
+ Validb::Finder.get_models("Post, Blog").should =~ [Post, Blog]
11
+ end
12
+
13
+ context "with invalid model names" do
14
+ Validb::Finder.get_models("Post, Blog, Fake").should =~ [Post, Blog]
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
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
8
+ $stdout.should_receive(:puts).with("Checking Blog")
9
+
10
+ Validb::ModelValidator.validate(model)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe Validb::RecordValidator do
4
+ describe "#validate" do
5
+ context "with a valid record" do
6
+ it "validates the passed in record" do
7
+ record = Blog.new(title: "title")
8
+
9
+ $stdout.should_not_receive(:puts)
10
+
11
+ Validb::RecordValidator.validate(record)
12
+ end
13
+ end
14
+
15
+ context "with an invalid record" do
16
+ it "outputs the record information" do
17
+ record = Blog.new
18
+ record.save(validate: false)
19
+
20
+ $stdout.should_receive(:puts).with("#{record.id} - Title can't be blank")
21
+
22
+ Validb::RecordValidator.validate(record)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Validb do
4
+ it "returns the version" do
5
+ Validb::VERSION.should == "0.0.1"
6
+ end
7
+ end
data/validb.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "validb/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'validb'
7
+ s.version = Validb::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+
10
+ s.authors = ['Joey Geiger']
11
+ s.email = 'jgeiger@gmail.com'
12
+ s.homepage = 'http://github.com/jgeiger/validb'
13
+
14
+ s.summary = 'Validb checks for invalid data in your database.'
15
+ s.description = 'Check the contents of your database by validating the models.'
16
+ s.licenses = ["MIT"]
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
20
+ s.require_paths = ['lib']
21
+
22
+ s.add_dependency 'activerecord', '>=3'
23
+ s.add_dependency 'activesupport', '>=3'
24
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Joey Geiger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '3'
41
+ description: Check the contents of your database by validating the models.
42
+ email: jgeiger@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - .gitignore
48
+ - .rspec
49
+ - CHANGELOG.md
50
+ - Gemfile
51
+ - README.md
52
+ - Rakefile
53
+ - lib/tasks/validb.rake
54
+ - lib/validb.rb
55
+ - lib/validb/checker.rb
56
+ - lib/validb/finder.rb
57
+ - lib/validb/model_validator.rb
58
+ - lib/validb/railtie.rb
59
+ - lib/validb/record_validator.rb
60
+ - lib/validb/version.rb
61
+ - spec/spec_helper.rb
62
+ - spec/validb/checker_spec.rb
63
+ - spec/validb/finder_spec.rb
64
+ - spec/validb/model_validator_spec.rb
65
+ - spec/validb/record_validator_spec.rb
66
+ - spec/validb/version_spec.rb
67
+ - validb.gemspec
68
+ homepage: http://github.com/jgeiger/validb
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.0.0
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Validb checks for invalid data in your database.
92
+ test_files:
93
+ - spec/spec_helper.rb
94
+ - spec/validb/checker_spec.rb
95
+ - spec/validb/finder_spec.rb
96
+ - spec/validb/model_validator_spec.rb
97
+ - spec/validb/record_validator_spec.rb
98
+ - spec/validb/version_spec.rb