cavalry 0.1.0

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: 967d285751a7b9c490680cbfc6291dab9e9cbb43
4
+ data.tar.gz: 1b87f1ff8ef5d2749b57572d07dd9cb0c8cfa68a
5
+ SHA512:
6
+ metadata.gz: '008f80c63a66b341afc6d372474ae7cc90d47c67ff21124063345b2d58c6bf2c23a5a2db83b72b01de9ec9477eb201e6841f2654b16e53c43d99908758cd576c'
7
+ data.tar.gz: cfeb4852f8614f9b4f1823d49bc79be93445af6f8522fa390eeef9a74a858dab697b4e3845b1f76e559573e9c1291c51305f02f028a16dbaf1677a0260eb83fa
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ *.swp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in cavalry.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,137 @@
1
+ # Cavalry
2
+
3
+ Cavalry is whole data validation DSL.
4
+
5
+
6
+ # Strength
7
+
8
+ The situation: When you need to validate some data, with foreign-key or has some constraints on associations(like Online-game's master-data), you need to insert all data first for validation.
9
+
10
+ Cavalry can:
11
+ 1. Write some validation with DSL like ActiveModel implementation.
12
+ 2. Validate to each records, or grouped records.
13
+ 3. Validations will not depend it's data-model.
14
+ 3. Integrate friendly.
15
+
16
+ # Workflow
17
+
18
+ 1. Prepare your data via ActiveModel or Insert your data to database via ActiveRecord. (like rake db:seed)
19
+ 2. Write your Validation
20
+ 3. Run Cavalry.run
21
+ 4. Fix the data if you need.
22
+
23
+ ## Installation
24
+
25
+ Add this line to your application's Gemfile:
26
+
27
+ ```ruby
28
+ gem 'cavalry'
29
+ ```
30
+
31
+ And then execute:
32
+
33
+ $ bundle
34
+
35
+ Or install it yourself as:
36
+
37
+ $ gem install cavalry
38
+
39
+ ## Usage
40
+
41
+ ### How to write Validation
42
+
43
+ Here is example model and validator!
44
+
45
+
46
+ ```:person.rb
47
+ class Person < ActiveRecord::Base
48
+ has_many :books
49
+ has_one :life
50
+ end
51
+ ```
52
+
53
+ ```:person_validator.rb
54
+ # 1. inherit Cavalry::Validator to define validator
55
+ class PersonValidator < Cavalry::Validator
56
+ # 2. call Cavalry::Validator.validate_for, to define target model
57
+ validate_for Person
58
+
59
+ # 3. pass a block to Cavalry::Validator.validate_each. it runs EVERY data-record.
60
+ validate_each do
61
+ # people needs life...
62
+ validates :life, presence: true
63
+
64
+ validate :name_is_downcased
65
+
66
+ def name_is_downcased
67
+ if name != name.downcase
68
+ errors.add(:name, "should be downcase.")
69
+ end
70
+ end
71
+ end
72
+
73
+ # 4. pass a block to Cavalry::Validator.validate_group. it runs ONCE for whole data-record
74
+ validate_group do
75
+
76
+ # 5. call validate and give it block. it receives Person.all as argument "records"
77
+ validate do |records|
78
+ return unless records.count > 5
79
+ errors.add(:base, "Too many people.")
80
+ end
81
+
82
+ # 5. call validate and chain any methods. it receives Person.method as argument "records"
83
+ validate.where(name: "bob") do |records|
84
+ return if records.count == 1
85
+ errors.add(:base, "bob must be unique.")
86
+ end
87
+
88
+ # 6. call validate with symbol, it calls method that you defined same scope.
89
+ validate :books_are_unique
90
+
91
+ def number_of_books
92
+ all_books = Person.all.flat_map(&:book)
93
+
94
+ return if all_books == all_books.uniq
95
+
96
+ errors.add(:books, "books that people have should be unique")
97
+ end
98
+ end
99
+ end
100
+ ```
101
+
102
+ ### How to execute
103
+
104
+ Here is sample rake task file.
105
+ I'm waiting some PR for improve!
106
+
107
+ ```:Rakefile
108
+ namespace :data do
109
+ task check: :environment do
110
+ Cavalry.configure do |config|
111
+ config.modelss_path = "app/models"
112
+ config.validators_path = "app/validators"
113
+ end
114
+
115
+ unless Cavalry.valid?
116
+ print Cavalry.dump.to_yaml
117
+ end
118
+ end
119
+ end
120
+ ```
121
+
122
+ Then,
123
+
124
+ ```
125
+ bundle exec rake data:check
126
+ ```
127
+
128
+
129
+ ## Development
130
+
131
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
132
+
133
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
134
+
135
+ ## Contributing
136
+
137
+ Bug reports and pull requests are welcome on GitHub at https://github.com/the40san/cavalry.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cavalry"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/cavalry.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "cavalry/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cavalry"
8
+ spec.version = Cavalry::VERSION
9
+ spec.authors = ["Masashi AKISUE"]
10
+ spec.email = ["m.akisue.b@gmail.com"]
11
+
12
+ spec.summary = %q{Simple whole data validation via ActiveRecord}
13
+ spec.description = %q{Simple whole data validation via ActiveRecord}
14
+ spec.homepage = "https://github.com/the40san/cavalry"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "activemodel", "~> 5.1"
24
+ spec.add_dependency "activesupport", "~> 5.1"
25
+ spec.add_development_dependency "bundler", "~> 1.15"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_development_dependency "pry"
29
+ spec.add_development_dependency "pry-byebug"
30
+ end
data/lib/cavalry.rb ADDED
@@ -0,0 +1,36 @@
1
+ require "cavalry/version"
2
+ require "active_support"
3
+ require "active_model"
4
+
5
+ module Cavalry
6
+ autoload :Config, 'cavalry/config'
7
+ autoload :Client, 'cavalry/client'
8
+ autoload :Validator, 'cavalry/validator'
9
+ autoload :Error, 'cavalry/error'
10
+
11
+ class DSLError < RuntimeError; end
12
+
13
+ class << self
14
+ def configure
15
+ yield config
16
+ end
17
+
18
+ delegate :run, :errors, :dump, to: :client
19
+
20
+ def valid?
21
+ run unless client.done?
22
+ errors.blank?
23
+ end
24
+
25
+
26
+ private
27
+
28
+ def client
29
+ @client ||= Client.new(config)
30
+ end
31
+
32
+ def config
33
+ @config ||= Config.new
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,38 @@
1
+ module Cavalry
2
+ class Client
3
+ attr_reader :errors
4
+
5
+ def initialize(config)
6
+ @config = config
7
+ @errors = []
8
+
9
+ config.load_models
10
+ config.load_validators
11
+ end
12
+
13
+ def run
14
+ @errors = all_validators.flat_map do |klass|
15
+ klass.execute_validation
16
+ end
17
+ @done = true
18
+ end
19
+
20
+ def done?
21
+ @done
22
+ end
23
+
24
+ def dump
25
+ errors.map(&:dump)
26
+ end
27
+
28
+ private
29
+
30
+ def all_validators
31
+ @all_validators ||= ObjectSpace.each_object(::Cavalry::Validator.singleton_class).map do |k|
32
+ next if k.singleton_class?
33
+ next if k == ::Cavalry::Validator
34
+ k
35
+ end.compact
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,25 @@
1
+ module Cavalry
2
+ class Config
3
+ # Defines validator definition path. files required with **/*.rb
4
+ mattr_accessor :models_path
5
+
6
+ # Defines validator definition path. files required with **/*.rb
7
+ mattr_accessor :validators_path
8
+
9
+ def load_models
10
+ load_rb_files(models_path)
11
+ end
12
+
13
+ def load_validators
14
+ load_rb_files(validators_path)
15
+ end
16
+
17
+ private
18
+
19
+ def load_rb_files(path)
20
+ return unless path
21
+ files = Dir.glob(File.join("#{path}", "**/*.rb"))
22
+ files.each { |f| require f }
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ module Cavalry
2
+ class Error
3
+ delegate :errors, to: :@record
4
+
5
+ def initialize(record)
6
+ @record = record
7
+ dump
8
+ end
9
+
10
+ def dump
11
+ { record: @record.class.name }.tap do |h|
12
+ h.merge!(attributes: @record.attributes) if @record.respond_to?(:attributes)
13
+ h.merge!(errors: errors.to_hash)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,40 @@
1
+ module Cavalry
2
+ class Validator
3
+ autoload :EachValidator, 'cavalry/validator/each_validator'
4
+ autoload :GroupValidator, 'cavalry/validator/group_validator'
5
+
6
+ class << self
7
+ def validate_for(*klasses)
8
+ @klasses = klasses.map { |k| append_required_module(k) }
9
+ @each_validators = []
10
+ @group_validators = []
11
+ end
12
+
13
+ def validate_each(&block)
14
+ @each_validators += @klasses.map {|k| EachValidator.new(k, &block) }
15
+ end
16
+
17
+ def validate_group(&block)
18
+ @group_validators += @klasses.map {|k| GroupValidator.new(k, &block) }
19
+ end
20
+
21
+ def execute_validation
22
+ error_records = [].tap do |res|
23
+ res << @each_validators.map(&:validate).compact
24
+ res << @group_validators.map(&:validate).compact
25
+ end.flatten
26
+
27
+ error_records.map {|e| Error.new(e) }
28
+ end
29
+
30
+ private
31
+
32
+ def append_required_module(klass)
33
+ unless klass.include?(ActiveModel::Validations)
34
+ klass.include(ActiveModel::Validations)
35
+ end
36
+ klass
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,23 @@
1
+ module Cavalry
2
+ class Validator
3
+ class EachValidator
4
+ attr_reader :source_class
5
+
6
+ def initialize(klass, &block)
7
+ @source_class = klass
8
+ @source_class.class_eval(&block)
9
+ end
10
+
11
+ def validate
12
+ source_class.all.flat_map {|record| validate_record(record) }.compact
13
+ end
14
+
15
+ private
16
+
17
+ def validate_record(record)
18
+ return if record.valid?
19
+ record
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,117 @@
1
+ module Cavalry
2
+ class Validator
3
+ class GroupValidator
4
+ attr_reader :source_class
5
+ attr_reader :definitions
6
+
7
+ def initialize(klass, &block)
8
+ @source_class = klass
9
+ @definitions = block
10
+ end
11
+
12
+ def validate
13
+ validator_context = create_validations
14
+ validator_context.instance.validate
15
+ validator_context.instance.errors.any? ? validator_context.instance : nil
16
+ end
17
+
18
+ private
19
+
20
+ def create_validations
21
+ new_class = Class.new
22
+ new_class.include(GroupValidations)
23
+ new_class.source_class = source_class
24
+ new_class.class_eval(&@definitions)
25
+ new_class
26
+ end
27
+ end
28
+
29
+ module GroupValidations
30
+ extend ActiveSupport::Concern
31
+
32
+ included do
33
+ cattr_accessor :source_class
34
+ cattr_accessor :validators
35
+ end
36
+
37
+ class_methods do
38
+ def name
39
+ "GroupValidation"
40
+ end
41
+
42
+ def validate(*args, &block)
43
+ self.validators ||= []
44
+
45
+ if args.count.zero?
46
+ InlineValidator.new(source_class, instance, &block).tap do |validator|
47
+ self.validators << validator
48
+ end
49
+ else
50
+ args.each do |method_name|
51
+ self.validators << MethodCallValidator.new(source_class, instance, method_name)
52
+ end
53
+ end
54
+ end
55
+
56
+ def instance
57
+ @instance ||= new
58
+ end
59
+ end
60
+
61
+ def validate
62
+ self.validators.map(&:validate)
63
+ end
64
+
65
+ def errors
66
+ @errors ||= ActiveModel::Errors.new(self)
67
+ end
68
+ end
69
+
70
+ class MethodCallValidator
71
+ def initialize(source_class, context, method_name)
72
+ @source_class = source_class
73
+ @context = context
74
+ @method_name = method_name
75
+ end
76
+
77
+ def validate
78
+ if @context.method(@method_name).arity.zero?
79
+ @context.send(@method_name)
80
+ else
81
+ @context.send(@method_name, @source_class.all)
82
+ end
83
+ end
84
+ end
85
+
86
+ class InlineValidator
87
+ def initialize(source, context, &block)
88
+ @source = source
89
+ @context = context
90
+ @definition = block
91
+ end
92
+
93
+ # execute
94
+ def validate
95
+ fail(DSLError, "Give me a definition") unless @definition
96
+
97
+ tap do
98
+ @context.class.send(:define_method, :cavalry_validation, @definition)
99
+ @context.send(:cavalry_validation, @source.respond_to?(:all) ? @source.all : @source)
100
+ @context.class.send(:undef_method, :cavalry_validation)
101
+ end
102
+ end
103
+
104
+ # Modify the validation source when method is chained.
105
+ # if you pass a block to method, block will be stored as validation definition.
106
+ def method_missing(method_name, *arg, &block)
107
+ if @source.respond_to?(method_name)
108
+ @source = @source.send(method_name, *arg)
109
+ else
110
+ fail DSLError, "Method #{method_name} is missing for #{@source.inspect}"
111
+ end
112
+
113
+ @definition = block if block_given?
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,3 @@
1
+ module Cavalry
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cavalry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Masashi AKISUE
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.15'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.15'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Simple whole data validation via ActiveRecord
112
+ email:
113
+ - m.akisue.b@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - README.md
123
+ - Rakefile
124
+ - bin/console
125
+ - bin/setup
126
+ - cavalry.gemspec
127
+ - lib/cavalry.rb
128
+ - lib/cavalry/client.rb
129
+ - lib/cavalry/config.rb
130
+ - lib/cavalry/error.rb
131
+ - lib/cavalry/validator.rb
132
+ - lib/cavalry/validator/each_validator.rb
133
+ - lib/cavalry/validator/group_validator.rb
134
+ - lib/cavalry/version.rb
135
+ homepage: https://github.com/the40san/cavalry
136
+ licenses: []
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.6.13
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Simple whole data validation via ActiveRecord
158
+ test_files: []