cavalry 0.2.2
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 +7 -0
- data/.gitignore +13 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +142 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/cavalry.gemspec +30 -0
- data/lib/cavalry.rb +35 -0
- data/lib/cavalry/client.rb +38 -0
- data/lib/cavalry/config.rb +27 -0
- data/lib/cavalry/error.rb +17 -0
- data/lib/cavalry/validator.rb +46 -0
- data/lib/cavalry/validator/each_validator.rb +37 -0
- data/lib/cavalry/validator/group_validator.rb +117 -0
- data/lib/cavalry/version.rb +3 -0
- metadata +159 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 6eda4fc1aad68c89ea024c523d771058b1e6ac07e1f2fe02fec18ac36b2c9389
|
|
4
|
+
data.tar.gz: b2a175e104849d8db86ebd34d0a82a0fb33e4a59031d20543249f02c31653479
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 3b13ef9bd60a755e2a051c3ce93e5b294bd12790f4f6e48af243d4078bd636d8bc22b88f0b5adfd46a776e73b76631ff7eff3ce292ed779fbb4352544937cdf7
|
|
7
|
+
data.tar.gz: 0e4023701f4827f2dedb047f6bd37d021d623deda7a52c4b43f2cedad90e14cf471dbd9f13ad0db79301800cffb13ce33add69563c6811ad27bee266a3f837c5
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 Masashi AKISUE
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
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.
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
## License
|
|
141
|
+
|
|
142
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
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
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,35 @@
|
|
|
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
|
+
def config
|
|
26
|
+
@config ||= Config.new
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def client
|
|
32
|
+
@client ||= Client.new(config)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
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,27 @@
|
|
|
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
|
+
mattr_accessor :force_check_belongs_to_association
|
|
10
|
+
|
|
11
|
+
def load_models
|
|
12
|
+
load_rb_files(models_path)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def load_validators
|
|
16
|
+
load_rb_files(validators_path)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def load_rb_files(path)
|
|
22
|
+
return unless path
|
|
23
|
+
files = Dir.glob(File.join("#{path}", "**/*.rb"))
|
|
24
|
+
files.each { |f| require f }
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
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,46 @@
|
|
|
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
|
+
|
|
12
|
+
allocate_each_validators
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def validate_each(&block)
|
|
16
|
+
@each_validators.each {|v| v.append(&block) }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def validate_group(&block)
|
|
20
|
+
@group_validators += @klasses.map {|k| GroupValidator.new(k, &block) }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def execute_validation
|
|
24
|
+
error_records = [].tap do |res|
|
|
25
|
+
res << @each_validators.map(&:validate).compact
|
|
26
|
+
res << @group_validators.map(&:validate).compact
|
|
27
|
+
end.flatten
|
|
28
|
+
|
|
29
|
+
error_records.map {|e| Error.new(e) }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def append_required_module(klass)
|
|
35
|
+
unless klass.include?(ActiveModel::Validations)
|
|
36
|
+
klass.include(ActiveModel::Validations)
|
|
37
|
+
end
|
|
38
|
+
klass
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def allocate_each_validators
|
|
42
|
+
@each_validators += @klasses.map {|k| EachValidator.new(k) }
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module Cavalry
|
|
2
|
+
class Validator
|
|
3
|
+
class EachValidator
|
|
4
|
+
attr_reader :source_class
|
|
5
|
+
|
|
6
|
+
def initialize(klass)
|
|
7
|
+
@source_class = klass
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def append(&block)
|
|
11
|
+
@source_class.class_eval(&block)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def validate
|
|
15
|
+
install_force_check_belongs_to_association if Cavalry.config.force_check_belongs_to_association && @source_class.respond_to?(:reflections)
|
|
16
|
+
source_class.all.flat_map {|record| validate_record(record) }.compact
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def validate_record(record)
|
|
22
|
+
return if record.valid?
|
|
23
|
+
record
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def install_force_check_belongs_to_association
|
|
27
|
+
return unless defined?(ActiveRecord::Reflection::BelongsToReflection)
|
|
28
|
+
|
|
29
|
+
@source_class.class_eval do
|
|
30
|
+
reflections.values.select { |r| r.is_a?(ActiveRecord::Reflection::BelongsToReflection) }.each do |r|
|
|
31
|
+
validates r.name, presence: true
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
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
|
metadata
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: cavalry
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.2.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Masashi AKISUE
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2019-10-10 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
|
+
- LICENSE.txt
|
|
123
|
+
- README.md
|
|
124
|
+
- Rakefile
|
|
125
|
+
- bin/console
|
|
126
|
+
- bin/setup
|
|
127
|
+
- cavalry.gemspec
|
|
128
|
+
- lib/cavalry.rb
|
|
129
|
+
- lib/cavalry/client.rb
|
|
130
|
+
- lib/cavalry/config.rb
|
|
131
|
+
- lib/cavalry/error.rb
|
|
132
|
+
- lib/cavalry/validator.rb
|
|
133
|
+
- lib/cavalry/validator/each_validator.rb
|
|
134
|
+
- lib/cavalry/validator/group_validator.rb
|
|
135
|
+
- lib/cavalry/version.rb
|
|
136
|
+
homepage: https://github.com/the40san/cavalry
|
|
137
|
+
licenses: []
|
|
138
|
+
metadata: {}
|
|
139
|
+
post_install_message:
|
|
140
|
+
rdoc_options: []
|
|
141
|
+
require_paths:
|
|
142
|
+
- lib
|
|
143
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
144
|
+
requirements:
|
|
145
|
+
- - ">="
|
|
146
|
+
- !ruby/object:Gem::Version
|
|
147
|
+
version: '0'
|
|
148
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
149
|
+
requirements:
|
|
150
|
+
- - ">="
|
|
151
|
+
- !ruby/object:Gem::Version
|
|
152
|
+
version: '0'
|
|
153
|
+
requirements: []
|
|
154
|
+
rubyforge_project:
|
|
155
|
+
rubygems_version: 2.7.6
|
|
156
|
+
signing_key:
|
|
157
|
+
specification_version: 4
|
|
158
|
+
summary: Simple whole data validation via ActiveRecord
|
|
159
|
+
test_files: []
|