dry-logic 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +7 -12
- data/CHANGELOG.md +23 -10
- data/CONTRIBUTING.md +29 -0
- data/README.md +4 -27
- data/dry-logic.gemspec +1 -2
- data/examples/basic.rb +5 -10
- data/lib/dry/logic/predicates.rb +16 -2
- data/lib/dry/logic/rule.rb +2 -2
- data/lib/dry/logic/version.rb +1 -1
- data/spec/shared/predicates.rb +4 -0
- data/spec/spec_helper.rb +12 -4
- data/spec/unit/predicates/case_spec.rb +33 -0
- data/spec/unit/predicates/eql_spec.rb +1 -1
- data/spec/unit/predicates/includes_spec.rb +14 -46
- data/spec/unit/rule_spec.rb +52 -5
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 554db40a47bc9eaa08659c38fd706fd15317f703
|
4
|
+
data.tar.gz: cdf04c815e657c282eef1c0d28d8923ba6b4dc12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa2ff6136f6407d523dcb8f5dee97c7f01da3aa71ebde81ce05ac92ab1408db9909d9e997cf81b46ad2bb8120000cdbe84c456b3866f22d37259674883445721
|
7
|
+
data.tar.gz: 3b6148d888eb2fb6ab6c8e87f3dedbee81feb13a310c7256abc06de9e8643555bf671b9d215045fc00db5c8a09e1b35f6ac1e57e57a6f91d09ca509eee14aa7a
|
data/.travis.yml
CHANGED
@@ -5,27 +5,22 @@ cache: bundler
|
|
5
5
|
bundler_args: --without console tools
|
6
6
|
script:
|
7
7
|
- bundle exec rake spec
|
8
|
-
before_install:
|
9
|
-
- gem update --system
|
10
|
-
- rvm @global do gem uninstall bundler -a -x
|
11
|
-
- rvm @global do gem install bundler -v 1.13.7
|
12
8
|
after_success:
|
13
|
-
|
14
|
-
- '[ "${TRAVIS_JOB_NUMBER#*.}" = "1" ] && [ "$TRAVIS_BRANCH" = "master" ] && bundle exec codeclimate-test-reporter'
|
9
|
+
- '[ -d coverage ] && bundle exec codeclimate-test-reporter'
|
15
10
|
rvm:
|
16
|
-
- 2.
|
17
|
-
- 2.2
|
18
|
-
- 2.
|
19
|
-
-
|
11
|
+
- 2.1.10
|
12
|
+
- 2.2.7
|
13
|
+
- 2.3.4
|
14
|
+
- 2.4.1
|
15
|
+
- jruby-9.1.10.0
|
20
16
|
- ruby-head
|
21
|
-
- rbx-3
|
22
17
|
env:
|
23
18
|
global:
|
24
19
|
- JRUBY_OPTS='--dev -J-Xmx1024M'
|
20
|
+
- COVERAGE=true
|
25
21
|
matrix:
|
26
22
|
allow_failures:
|
27
23
|
- rvm: ruby-head
|
28
|
-
- rvm: rbx-3
|
29
24
|
notifications:
|
30
25
|
email: false
|
31
26
|
webhooks:
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
# v0.4.2 2017-09-15
|
2
|
+
|
3
|
+
### Added
|
4
|
+
|
5
|
+
* New `:case?` predicate matches a value against the given object with `#===` (flash-gordon)
|
6
|
+
* New `:is?` predicate checks objects identity (using `#equal?`) (flash-gordon)
|
7
|
+
|
8
|
+
### Fixed
|
9
|
+
|
10
|
+
* A bug with using custom predicates within a standalone module in `dry-validation` (flash-gordon)
|
11
|
+
|
12
|
+
[Compare v0.4.1...v0.4.2(https://github.com/dry-rb/dry-logic/compare/v0.4.1...v0.4.2)
|
13
|
+
|
1
14
|
# v0.4.1 2017-01-23
|
2
15
|
|
3
16
|
### Changed
|
@@ -8,7 +21,7 @@
|
|
8
21
|
|
9
22
|
* Warnings on MRI 2.4.0 are gone (jtippett)
|
10
23
|
|
11
|
-
[Compare v0.4.0...v0.4.1](https://github.com/
|
24
|
+
[Compare v0.4.0...v0.4.1](https://github.com/dry-rb/dry-logic/compare/v0.4.0...v0.4.1)
|
12
25
|
|
13
26
|
# v0.4.0 2016-09-21
|
14
27
|
|
@@ -36,7 +49,7 @@ This is a partial rewrite focused on internal clean up and major performance imp
|
|
36
49
|
* [BREAKING] `Rule#name` is now `Rule#id` (solnic)
|
37
50
|
* `Rule#parameters` is public now (solnic)
|
38
51
|
|
39
|
-
[Compare v0.3.0...v0.4.0](https://github.com/
|
52
|
+
[Compare v0.3.0...v0.4.0](https://github.com/dry-rb/dry-logic/compare/v0.3.0...v0.4.0)
|
40
53
|
|
41
54
|
# v0.3.0 2016-07-01
|
42
55
|
|
@@ -51,7 +64,7 @@ This is a partial rewrite focused on internal clean up and major performance imp
|
|
51
64
|
* Predicates raise errors when they are called with invalid arity (fran-worley + solnic)
|
52
65
|
* Rules no longer evaluate input twice when building result objects (solnic)
|
53
66
|
|
54
|
-
[Compare v0.2.3...v0.3.0](https://github.com/
|
67
|
+
[Compare v0.2.3...v0.3.0](https://github.com/dry-rb/dry-logic/compare/v0.2.3...v0.3.0)
|
55
68
|
|
56
69
|
# v0.2.3 2016-05-11
|
57
70
|
|
@@ -64,7 +77,7 @@ This is a partial rewrite focused on internal clean up and major performance imp
|
|
64
77
|
* Renamed `inclusion?` to `included_in?` and deprecated `inclusion?` (fran-worley)
|
65
78
|
* Renamed `exclusion?` to `excluded_from?` and deprecated `exclusion?` (fran-worley)
|
66
79
|
|
67
|
-
[Compare v0.2.2...v0.2.3](https://github.com/
|
80
|
+
[Compare v0.2.2...v0.2.3](https://github.com/dry-rb/dry-logic/compare/v0.2.2...v0.2.3)
|
68
81
|
|
69
82
|
# v0.2.2 2016-03-30
|
70
83
|
|
@@ -72,7 +85,7 @@ This is a partial rewrite focused on internal clean up and major performance imp
|
|
72
85
|
|
73
86
|
* `number?`, `odd?`, `even?` predicates (fran-worley)
|
74
87
|
|
75
|
-
[Compare v0.2.1...v0.2.2](https://github.com/
|
88
|
+
[Compare v0.2.1...v0.2.2](https://github.com/dry-rb/dry-logic/compare/v0.2.1...v0.2.2)
|
76
89
|
|
77
90
|
# v0.2.1 2016-03-20
|
78
91
|
|
@@ -86,7 +99,7 @@ This is a partial rewrite focused on internal clean up and major performance imp
|
|
86
99
|
|
87
100
|
* Entire AST has been redefined (solnic)
|
88
101
|
|
89
|
-
[Compare v0.1.4...v0.2.0](https://github.com/
|
102
|
+
[Compare v0.1.4...v0.2.0](https://github.com/dry-rb/dry-logic/compare/v0.1.4...v0.2.0)
|
90
103
|
|
91
104
|
# v0.1.4 2016-01-27
|
92
105
|
|
@@ -95,7 +108,7 @@ This is a partial rewrite focused on internal clean up and major performance imp
|
|
95
108
|
* Support for hash-names in `Check` and `Result` which can properly resolve input
|
96
109
|
from nested results (solnic)
|
97
110
|
|
98
|
-
[Compare v0.1.3...v0.1.4](https://github.com/
|
111
|
+
[Compare v0.1.3...v0.1.4](https://github.com/dry-rb/dry-logic/compare/v0.1.3...v0.1.4)
|
99
112
|
|
100
113
|
# v0.1.3 2016-01-27
|
101
114
|
|
@@ -107,7 +120,7 @@ This is a partial rewrite focused on internal clean up and major performance imp
|
|
107
120
|
|
108
121
|
* `Check` and `Result` carry original input(s) (solnic)
|
109
122
|
|
110
|
-
[Compare v0.1.2...v0.1.3](https://github.com/
|
123
|
+
[Compare v0.1.2...v0.1.3](https://github.com/dry-rb/dry-logic/compare/v0.1.2...v0.1.3)
|
111
124
|
|
112
125
|
# v0.1.2 2016-01-19
|
113
126
|
|
@@ -115,7 +128,7 @@ This is a partial rewrite focused on internal clean up and major performance imp
|
|
115
128
|
|
116
129
|
* `xor` returns wrapped results when used against another result-rule (solnic)
|
117
130
|
|
118
|
-
[Compare v0.1.1...v0.1.2](https://github.com/
|
131
|
+
[Compare v0.1.1...v0.1.2](https://github.com/dry-rb/dry-logic/compare/v0.1.1...v0.1.2)
|
119
132
|
|
120
133
|
# v0.1.1 2016-01-18
|
121
134
|
|
@@ -125,7 +138,7 @@ This is a partial rewrite focused on internal clean up and major performance imp
|
|
125
138
|
* `Rule::Result` which can be applied to a result object (solnic)
|
126
139
|
* `true?` and `false?` predicates (solnic)
|
127
140
|
|
128
|
-
[Compare v0.1.0...v0.1.1](https://github.com/
|
141
|
+
[Compare v0.1.0...v0.1.1](https://github.com/dry-rb/dry-logic/compare/v0.1.0...v0.1.1)
|
129
142
|
|
130
143
|
# v0.1.0 2016-01-11
|
131
144
|
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Issue Guidelines
|
2
|
+
|
3
|
+
## Reporting bugs
|
4
|
+
|
5
|
+
If you found a bug, report an issue and describe what's the expected behavior versus what actually happens. If the bug causes a crash, attach a full backtrace. If possible, a reproduction script showing the problem is highly appreciated.
|
6
|
+
|
7
|
+
## Reporting feature requests
|
8
|
+
|
9
|
+
Report a feature request **only after discussing it first on [discuss.dry-rb.org](https://discuss.dry-rb.org)** where it was accepted. Please provide a concise description of the feature, don't link to a discussion thread, and instead summarize what was discussed.
|
10
|
+
|
11
|
+
## Reporting questions, support requests, ideas, concerns etc.
|
12
|
+
|
13
|
+
**PLEASE DON'T** - use [discuss.dry-rb.org](http://discuss.dry-rb.org) instead.
|
14
|
+
|
15
|
+
# Pull Request Guidelines
|
16
|
+
|
17
|
+
A Pull Request will only be accepted if it addresses a specific issue that was reported previously, or fixes typos, mistakes in documentation etc.
|
18
|
+
|
19
|
+
Other requirements:
|
20
|
+
|
21
|
+
1) Do not open a pull request if you can't provide tests along with it. If you have problems writing tests, ask for help in the related issue.
|
22
|
+
2) Follow the style conventions of the surrounding code. In most cases, this is standard ruby style.
|
23
|
+
3) Add API documentation if it's a new feature
|
24
|
+
4) Update API documentation if it changes an existing feature
|
25
|
+
5) Bonus points for sending a PR to [github.com/dry-rb/dry-rb.org](github.com/dry-rb/dry-rb.org) which updates user documentation and guides
|
26
|
+
|
27
|
+
# Asking for help
|
28
|
+
|
29
|
+
If these guidelines aren't helpful, and you're stuck, please post a message on [discuss.dry-rb.org](https://discuss.dry-rb.org).
|
data/README.md
CHANGED
@@ -20,36 +20,13 @@ Predicate logic and rule composition used by:
|
|
20
20
|
* [dry-validation](https://github.com/dry-rb/dry-validation) for composing validation rules
|
21
21
|
* your project...?
|
22
22
|
|
23
|
-
##
|
23
|
+
## Links
|
24
24
|
|
25
|
-
|
26
|
-
require 'dry/logic'
|
27
|
-
require 'dry/logic/predicates'
|
25
|
+
* [Documentation](http://dry-rb.org/gems/dry-logic)
|
28
26
|
|
29
|
-
|
27
|
+
## Contributing
|
30
28
|
|
31
|
-
|
32
|
-
|
33
|
-
has_min_age = Rule::Key.new(Predicates[:int?], name: [:user, :age])
|
34
|
-
& Rule::Key.new(Predicates[:gt?].curry(18), name: [:user, :age])
|
35
|
-
|
36
|
-
user_rule = user_present & has_min_age
|
37
|
-
|
38
|
-
user_rule.(user: { age: 19 })
|
39
|
-
# #<Dry::Logic::Result::Named success?=true input={:user=>{:age=>19}} rule=#<Dry::Logic::Rule::Key predicate=#<Dry::Logic::Predicate id=:gt? args=[18, 19]> options={:evaluator=>#<Dry::Logic::Evaluator::Key path=[:user, :age]>, :name=>[:user, :age]}>>
|
40
|
-
|
41
|
-
user_rule.(user: { age: 18 })
|
42
|
-
# #<Dry::Logic::Result::Named success?=false input={:user=>{:age=>18}} rule=#<Dry::Logic::Rule::Key predicate=#<Dry::Logic::Predicate id=:gt? args=[18, 18]> options={:evaluator=>#<Dry::Logic::Evaluator::Key path=[:user, :age]>, :name=>[:user, :age]}>>
|
43
|
-
|
44
|
-
user_rule.(user: { age: 'seventeen' }).inspect
|
45
|
-
#<Dry::Logic::Result::Named success?=false input={:user=>{:age=>"seventeen"}} rule=#<Dry::Logic::Rule::Key predicate=#<Dry::Logic::Predicate id=:int? args=["seventeen"]> options={:evaluator=>#<Dry::Logic::Evaluator::Key path=[:user, :age]>, :name=>[:user, :age]}>>
|
46
|
-
|
47
|
-
user_rule.(user: { }).inspect
|
48
|
-
#<Dry::Logic::Result::Named success?=false input={:user=>{}} rule=#<Dry::Logic::Rule::Key predicate=#<Dry::Logic::Predicate id=:filled? args=[{}]> options={:evaluator=>#<Dry::Logic::Evaluator::Key path=[:user]>, :name=>:user}>>
|
49
|
-
|
50
|
-
puts user_rule.({}).inspect
|
51
|
-
#<Dry::Logic::Result::Named success?=false input={} rule=#<Dry::Logic::Rule::Key predicate=#<Dry::Logic::Predicate id=:filled? args=[nil]> options={:evaluator=>#<Dry::Logic::Evaluator::Key path=[:user]>, :name=>:user}>>
|
52
|
-
```
|
29
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/dry-rb/dry-logic.
|
53
30
|
|
54
31
|
## License
|
55
32
|
|
data/dry-logic.gemspec
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# coding: utf-8
|
2
1
|
require File.expand_path('../lib/dry/logic/version', __FILE__)
|
3
2
|
|
4
3
|
Gem::Specification.new do |spec|
|
@@ -7,7 +6,7 @@ Gem::Specification.new do |spec|
|
|
7
6
|
spec.authors = ['Piotr Solnica']
|
8
7
|
spec.email = ['piotr.solnica@gmail.com']
|
9
8
|
spec.summary = 'Predicate logic with rule composition'
|
10
|
-
spec.homepage = 'https://github.com/
|
9
|
+
spec.homepage = 'https://github.com/dry-rb/dry-logic'
|
11
10
|
spec.license = 'MIT'
|
12
11
|
|
13
12
|
spec.files = `git ls-files -z`.split("\x0")
|
data/examples/basic.rb
CHANGED
@@ -3,17 +3,12 @@ require 'dry/logic/predicates'
|
|
3
3
|
|
4
4
|
include Dry::Logic
|
5
5
|
|
6
|
-
user_present = Rule::
|
7
|
-
has_min_age = Rule::Key.new(Predicates[:int?], name: [:user, :age]) & Rule::Key.new(Predicates[:gt?].curry(18), name: [:user, :age])
|
6
|
+
user_present = Rule::Predicate.new(Predicates[:key?]).curry(:user)
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
puts user_rule.(user: { age: 19 }).inspect
|
8
|
+
has_min_age = Operations::Key.new(Rule::Predicate.new(Predicates[:gt?]).curry(18), name: [:user, :age])
|
12
9
|
|
13
|
-
|
14
|
-
|
15
|
-
puts user_rule.(user: { age: 'seventeen' }).inspect
|
10
|
+
user_rule = user_present & has_min_age
|
16
11
|
|
17
|
-
puts user_rule.(user: { }).
|
12
|
+
puts user_rule.(user: { age: 19 }).success?
|
18
13
|
|
19
|
-
puts user_rule.({}).
|
14
|
+
puts user_rule.(user: { age: 18 }).success?
|
data/lib/dry/logic/predicates.rb
CHANGED
@@ -166,6 +166,10 @@ module Dry
|
|
166
166
|
left.eql?(right)
|
167
167
|
end
|
168
168
|
|
169
|
+
def is?(left, right)
|
170
|
+
left.equal?(right)
|
171
|
+
end
|
172
|
+
|
169
173
|
def not_eql?(left, right)
|
170
174
|
!left.eql?(right)
|
171
175
|
end
|
@@ -178,8 +182,18 @@ module Dry
|
|
178
182
|
value.equal?(false)
|
179
183
|
end
|
180
184
|
|
181
|
-
|
182
|
-
|
185
|
+
if RUBY_VERSION < '2.4'
|
186
|
+
def format?(regex, input)
|
187
|
+
!regex.match(input).nil?
|
188
|
+
end
|
189
|
+
else
|
190
|
+
def format?(regex, input)
|
191
|
+
regex.match?(input)
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
def case?(pattern, input)
|
196
|
+
pattern === input
|
183
197
|
end
|
184
198
|
|
185
199
|
def predicate(name, &block)
|
data/lib/dry/logic/rule.rb
CHANGED
@@ -62,7 +62,7 @@ module Dry
|
|
62
62
|
end
|
63
63
|
|
64
64
|
def bind(object)
|
65
|
-
if predicate
|
65
|
+
if UnboundMethod === predicate
|
66
66
|
self.class.new(predicate.bind(object), options)
|
67
67
|
else
|
68
68
|
self.class.new(
|
@@ -73,7 +73,7 @@ module Dry
|
|
73
73
|
end
|
74
74
|
|
75
75
|
def eval_args(object)
|
76
|
-
with(args: args.map { |arg|
|
76
|
+
with(args: args.map { |arg| UnboundMethod === arg ? arg.bind(object).() : arg })
|
77
77
|
end
|
78
78
|
|
79
79
|
def with(new_opts)
|
data/lib/dry/logic/version.rb
CHANGED
data/spec/shared/predicates.rb
CHANGED
@@ -30,6 +30,10 @@ RSpec.shared_examples 'predicates' do
|
|
30
30
|
let(:eql?) { Dry::Logic::Predicates[:eql?] }
|
31
31
|
|
32
32
|
let(:size?) { Dry::Logic::Predicates[:size?] }
|
33
|
+
|
34
|
+
let(:case?) { Dry::Logic::Predicates[:case?] }
|
35
|
+
|
36
|
+
let(:equal?) { Dry::Logic::Predicates[:equal?] }
|
33
37
|
end
|
34
38
|
|
35
39
|
RSpec.shared_examples 'a passing predicate' do
|
data/spec/spec_helper.rb
CHANGED
@@ -1,7 +1,13 @@
|
|
1
|
-
if RUBY_ENGINE == 'ruby' && ENV['
|
2
|
-
require '
|
3
|
-
|
4
|
-
|
1
|
+
if RUBY_ENGINE == 'ruby' && ENV['COVERAGE'] == 'true'
|
2
|
+
require 'yaml'
|
3
|
+
rubies = YAML.load(File.read(File.join(__dir__, '..', '.travis.yml')))['rvm']
|
4
|
+
latest_mri = rubies.select { |v| v =~ /\A\d+\.\d+.\d+\z/ }.max
|
5
|
+
|
6
|
+
if RUBY_VERSION == latest_mri
|
7
|
+
require 'simplecov'
|
8
|
+
SimpleCov.start do
|
9
|
+
add_filter '/spec/'
|
10
|
+
end
|
5
11
|
end
|
6
12
|
end
|
7
13
|
|
@@ -23,4 +29,6 @@ include Dry::Core::Constants
|
|
23
29
|
|
24
30
|
RSpec.configure do |config|
|
25
31
|
config.disable_monkey_patching!
|
32
|
+
|
33
|
+
config.warnings = true
|
26
34
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'dry/logic/predicates'
|
2
|
+
|
3
|
+
RSpec.describe Dry::Logic::Predicates do
|
4
|
+
describe '#case?' do
|
5
|
+
let(:predicate_name) { :case? }
|
6
|
+
|
7
|
+
context 'when the value matches the pattern' do
|
8
|
+
let(:arguments_list) do
|
9
|
+
[
|
10
|
+
[11, 11],
|
11
|
+
[:odd?.to_proc, 11],
|
12
|
+
[/\Af/, 'foo'],
|
13
|
+
[Integer, 11]
|
14
|
+
]
|
15
|
+
end
|
16
|
+
|
17
|
+
it_behaves_like 'a passing predicate'
|
18
|
+
end
|
19
|
+
|
20
|
+
context "when the value doesn't match the pattern" do
|
21
|
+
let(:arguments_list) do
|
22
|
+
[
|
23
|
+
[13, 14],
|
24
|
+
[:odd?.to_proc, 12],
|
25
|
+
[/\Af/, 'bar'],
|
26
|
+
[String, 11]
|
27
|
+
]
|
28
|
+
end
|
29
|
+
|
30
|
+
it_behaves_like 'a failing predicate'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -1,56 +1,24 @@
|
|
1
1
|
require 'dry/logic/predicates'
|
2
2
|
|
3
|
-
RSpec.describe Dry::Logic::Predicates do
|
4
|
-
|
5
|
-
|
3
|
+
RSpec.describe Dry::Logic::Predicates, '#is?' do
|
4
|
+
let(:predicate_name) { :is? }
|
5
|
+
let(:one) { Object.new }
|
6
|
+
let(:two) { Object.new }
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
['Jill', ['Jill', 'John']],
|
11
|
-
['John', ['Jill', 'John']],
|
12
|
-
[1, 1..2],
|
13
|
-
[2, 1..2],
|
14
|
-
['Hello', 'Hello World'],
|
15
|
-
['World', 'Hello World'],
|
16
|
-
[:bar, { bar: 0 }],
|
17
|
-
[nil, [nil, false]],
|
18
|
-
[false, [nil, false]]
|
19
|
-
]
|
20
|
-
end
|
21
|
-
|
22
|
-
it_behaves_like 'a passing predicate'
|
8
|
+
context 'when value is equal to the arg' do
|
9
|
+
let(:arguments_list) do
|
10
|
+
[[one, one], [:one, :one]]
|
23
11
|
end
|
24
12
|
|
25
|
-
|
26
|
-
|
27
|
-
[
|
28
|
-
[2, 1],
|
29
|
-
[1, nil],
|
30
|
-
["foo", 1],
|
31
|
-
[1, "foo"],
|
32
|
-
[1..2, "foo"],
|
33
|
-
["foo", 1..2],
|
34
|
-
[:key, "foo"]
|
35
|
-
]
|
36
|
-
end
|
13
|
+
it_behaves_like 'a passing predicate'
|
14
|
+
end
|
37
15
|
|
38
|
-
|
16
|
+
context 'with value is not equal to the arg' do
|
17
|
+
let(:arguments_list) do
|
18
|
+
# Strings are not equal. Yet
|
19
|
+
[[one, two], ['one', 'one']]
|
39
20
|
end
|
40
21
|
|
41
|
-
|
42
|
-
let(:arguments_list) do
|
43
|
-
[
|
44
|
-
['Jack', ['Jill', 'John']],
|
45
|
-
[0, 1..2],
|
46
|
-
[3, 1..2],
|
47
|
-
['foo', 'Hello World'],
|
48
|
-
[:foo, { bar: 0 }],
|
49
|
-
[true, [nil, false]]
|
50
|
-
]
|
51
|
-
end
|
52
|
-
|
53
|
-
it_behaves_like 'a failing predicate'
|
54
|
-
end
|
22
|
+
it_behaves_like 'a failing predicate'
|
55
23
|
end
|
56
24
|
end
|
data/spec/unit/rule_spec.rb
CHANGED
@@ -4,6 +4,32 @@ RSpec.describe Dry::Logic::Rule do
|
|
4
4
|
let(:predicate) { -> { true } }
|
5
5
|
let(:options) { {} }
|
6
6
|
|
7
|
+
let(:schema) do
|
8
|
+
Class.new(BasicObject) do
|
9
|
+
define_method(:class, Kernel.instance_method(:class))
|
10
|
+
|
11
|
+
def method_missing(m, *)
|
12
|
+
if m.to_s.end_with?('?')
|
13
|
+
self.class.new
|
14
|
+
else
|
15
|
+
super
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_proc
|
20
|
+
-> value { value }
|
21
|
+
end
|
22
|
+
|
23
|
+
def arity
|
24
|
+
1
|
25
|
+
end
|
26
|
+
|
27
|
+
def parameters
|
28
|
+
[[:req, :value]]
|
29
|
+
end
|
30
|
+
end.new
|
31
|
+
end
|
32
|
+
|
7
33
|
it_behaves_like Dry::Logic::Rule
|
8
34
|
|
9
35
|
describe '.new' do
|
@@ -80,15 +106,36 @@ RSpec.describe Dry::Logic::Rule do
|
|
80
106
|
expect(bound.options[:parameters]).to eql(rule.parameters)
|
81
107
|
end
|
82
108
|
end
|
109
|
+
|
110
|
+
context 'with a schema instance' do
|
111
|
+
let(:object) { schema }
|
112
|
+
let(:predicate) { schema }
|
113
|
+
|
114
|
+
it 'returns a new with its predicate executed in the context of the provided object' do
|
115
|
+
expect(bound.(true)).to be_success
|
116
|
+
expect(bound.(false)).to be_failure
|
117
|
+
end
|
118
|
+
end
|
83
119
|
end
|
84
120
|
|
85
121
|
describe '#eval_args' do
|
86
|
-
|
87
|
-
|
88
|
-
|
122
|
+
context 'with an unbound method' do
|
123
|
+
let(:options) { { args: [1, klass.instance_method(:num), :foo] } }
|
124
|
+
let(:klass) { Class.new { def num; 7; end } }
|
125
|
+
let(:object) { klass.new }
|
89
126
|
|
90
|
-
|
91
|
-
|
127
|
+
it 'evaluates args in the context of the provided object' do
|
128
|
+
expect(rule.eval_args(object).args).to eql([1, 7, :foo])
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
context 'with a schema instance' do
|
133
|
+
let(:options) { { args: [1, schema, :foo] } }
|
134
|
+
let(:object) { Object.new }
|
135
|
+
|
136
|
+
it 'returns a new with its predicate executed in the context of the provided object' do
|
137
|
+
expect(rule.eval_args(object).args).to eql([1, schema, :foo])
|
138
|
+
end
|
92
139
|
end
|
93
140
|
end
|
94
141
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-logic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Solnica
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-core
|
@@ -114,6 +114,7 @@ files:
|
|
114
114
|
- ".rubocop_todo.yml"
|
115
115
|
- ".travis.yml"
|
116
116
|
- CHANGELOG.md
|
117
|
+
- CONTRIBUTING.md
|
117
118
|
- Gemfile
|
118
119
|
- LICENSE
|
119
120
|
- README.md
|
@@ -164,6 +165,7 @@ files:
|
|
164
165
|
- spec/unit/predicates/array_spec.rb
|
165
166
|
- spec/unit/predicates/attr_spec.rb
|
166
167
|
- spec/unit/predicates/bool_spec.rb
|
168
|
+
- spec/unit/predicates/case_spec.rb
|
167
169
|
- spec/unit/predicates/date_spec.rb
|
168
170
|
- spec/unit/predicates/date_time_spec.rb
|
169
171
|
- spec/unit/predicates/decimal_spec.rb
|
@@ -199,7 +201,7 @@ files:
|
|
199
201
|
- spec/unit/rule/predicate_spec.rb
|
200
202
|
- spec/unit/rule_compiler_spec.rb
|
201
203
|
- spec/unit/rule_spec.rb
|
202
|
-
homepage: https://github.com/
|
204
|
+
homepage: https://github.com/dry-rb/dry-logic
|
203
205
|
licenses:
|
204
206
|
- MIT
|
205
207
|
metadata: {}
|
@@ -219,7 +221,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
219
221
|
version: '0'
|
220
222
|
requirements: []
|
221
223
|
rubyforge_project:
|
222
|
-
rubygems_version: 2.6.
|
224
|
+
rubygems_version: 2.6.11
|
223
225
|
signing_key:
|
224
226
|
specification_version: 4
|
225
227
|
summary: Predicate logic with rule composition
|
@@ -243,6 +245,7 @@ test_files:
|
|
243
245
|
- spec/unit/predicates/array_spec.rb
|
244
246
|
- spec/unit/predicates/attr_spec.rb
|
245
247
|
- spec/unit/predicates/bool_spec.rb
|
248
|
+
- spec/unit/predicates/case_spec.rb
|
246
249
|
- spec/unit/predicates/date_spec.rb
|
247
250
|
- spec/unit/predicates/date_time_spec.rb
|
248
251
|
- spec/unit/predicates/decimal_spec.rb
|