dry-logic 0.4.2 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 554db40a47bc9eaa08659c38fd706fd15317f703
4
- data.tar.gz: cdf04c815e657c282eef1c0d28d8923ba6b4dc12
2
+ SHA256:
3
+ metadata.gz: 1a6ff887f61b513496991a96d7903685886ab29dae71cb4f75185cd7866fd4b2
4
+ data.tar.gz: 9e063f346341b395ac21645f84319e619af19151e62bd97413badf246440d353
5
5
  SHA512:
6
- metadata.gz: fa2ff6136f6407d523dcb8f5dee97c7f01da3aa71ebde81ce05ac92ab1408db9909d9e997cf81b46ad2bb8120000cdbe84c456b3866f22d37259674883445721
7
- data.tar.gz: 3b6148d888eb2fb6ab6c8e87f3dedbee81feb13a310c7256abc06de9e8643555bf671b9d215045fc00db5c8a09e1b35f6ac1e57e57a6f91d09ca509eee14aa7a
6
+ metadata.gz: f643a0238e3b67c91c83af1e90cb4398279b4c01957ce663a5186ae8437a87e94c9d9cb2f7a2aabca1c7edb47be28a531317a044e58e270ffff59c44e6a9ccf5
7
+ data.tar.gz: 17b0ff20fc5c41258a537ad0c57e959cd3359765801261157772a62ee5d1ce525c15bca2ce92c8ee624b1fda67a5cf6cbf4228af552cf99efc8e8428026dbccb
data/.gitignore CHANGED
@@ -2,7 +2,6 @@
2
2
  coverage
3
3
  /.bundle
4
4
  vendor/bundle
5
- bin/
6
5
  tmp/
7
6
  .idea/
8
7
  Gemfile.lock
@@ -1,31 +1,30 @@
1
1
  language: ruby
2
- dist: trusty
3
- sudo: false
4
2
  cache: bundler
5
- bundler_args: --without console tools
3
+ sudo: false
4
+ bundler_args: --without benchmarks tools
5
+ before_install: gem update --system
6
+ before_script:
7
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
8
+ - chmod +x ./cc-test-reporter
9
+ - ./cc-test-reporter before-build
10
+ after_script:
11
+ - "[ -d coverage ] && ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
6
12
  script:
7
- - bundle exec rake spec
8
- after_success:
9
- - '[ -d coverage ] && bundle exec codeclimate-test-reporter'
13
+ - bundle exec rake
10
14
  rvm:
11
- - 2.1.10
12
- - 2.2.7
13
- - 2.3.4
14
- - 2.4.1
15
- - jruby-9.1.10.0
16
- - ruby-head
15
+ - 2.6.0
16
+ - 2.5.3
17
+ - 2.4.5
18
+ - 2.3.8
19
+ - jruby-9.2.5.0
17
20
  env:
18
21
  global:
19
- - JRUBY_OPTS='--dev -J-Xmx1024M'
20
22
  - COVERAGE=true
21
- matrix:
22
- allow_failures:
23
- - rvm: ruby-head
24
23
  notifications:
25
24
  email: false
26
25
  webhooks:
27
26
  urls:
28
27
  - https://webhooks.gitter.im/e/19098b4253a72c9796db
29
- on_success: change # options: [always|never|change] default: always
30
- on_failure: always # options: [always|never|change] default: always
31
- on_start: false # default: false
28
+ on_success: change # options: [always|never|change] default: always
29
+ on_failure: always # options: [always|never|change] default: always
30
+ on_start: false # default: false
@@ -1,25 +1,37 @@
1
+ # v0.5.0 2019-01-29
2
+
3
+ ### Added
4
+
5
+ - `:nil?` predicate (`none?` is now an alias) (solnic)
6
+
7
+ ### Fixed
8
+
9
+ - `Operation::Key#ast` will now return a correct AST with non-Undefined inputs (solnic)
10
+
11
+ [Compare v0.4.2...v0.5.0](https://github.com/dry-rb/dry-logic/compare/v0.4.2...v0.5.0)
12
+
1
13
  # v0.4.2 2017-09-15
2
14
 
3
15
  ### Added
4
16
 
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)
17
+ - New `:case?` predicate matches a value against the given object with `#===` (flash-gordon)
18
+ - New `:is?` predicate checks objects identity (using `#equal?`) (flash-gordon)
7
19
 
8
20
  ### Fixed
9
21
 
10
- * A bug with using custom predicates within a standalone module in `dry-validation` (flash-gordon)
22
+ - A bug with using custom predicates within a standalone module in `dry-validation` (flash-gordon)
11
23
 
12
- [Compare v0.4.1...v0.4.2(https://github.com/dry-rb/dry-logic/compare/v0.4.1...v0.4.2)
24
+ [Compare v0.4.1...v0.4.2](https://github.com/dry-rb/dry-logic/compare/v0.4.1...v0.4.2)
13
25
 
14
26
  # v0.4.1 2017-01-23
15
27
 
16
28
  ### Changed
17
29
 
18
- * Predicates simply reuse other predicate methods instead of referring to them via `#[]` (georgemillo)
30
+ - Predicates simply reuse other predicate methods instead of referring to them via `#[]` (georgemillo)
19
31
 
20
32
  ### Fixed
21
33
 
22
- * Warnings on MRI 2.4.0 are gone (jtippett)
34
+ - Warnings on MRI 2.4.0 are gone (jtippett)
23
35
 
24
36
  [Compare v0.4.0...v0.4.1](https://github.com/dry-rb/dry-logic/compare/v0.4.0...v0.4.1)
25
37
 
@@ -29,25 +41,25 @@ This is a partial rewrite focused on internal clean up and major performance imp
29
41
 
30
42
  ### Added
31
43
 
32
- * `Rule#[]` which applies a rule and always returns `true` or `false` (solnic)
33
- * `Rule#bind` which returns a rule with its predicate bound to a given object (solnic)
34
- * `Rule#eval_args` which evaluates unbound-methods-args in the context of a given object (solnic)
35
- * `Logic.Rule` builder function (solnic)
36
- * Nice `#inspect` on rules and operation objects (solnic)
44
+ - `Rule#[]` which applies a rule and always returns `true` or `false` (solnic)
45
+ - `Rule#bind` which returns a rule with its predicate bound to a given object (solnic)
46
+ - `Rule#eval_args` which evaluates unbound-methods-args in the context of a given object (solnic)
47
+ - `Logic.Rule` builder function (solnic)
48
+ - Nice `#inspect` on rules and operation objects (solnic)
37
49
 
38
50
  ### Changed
39
51
 
40
- * [BRAEKING] New result API (solnic)
41
- * [BREAKING] `Predicate` is now `Rule::Predicate` (solnic)
42
- * [BREAKING] `Rule::Conjunction` is now `Operation::And` (solnic)
43
- * [BREAKING] `Rule::Disjunction` is now `Operation::Or` (solnic)
44
- * [BREAKING] `Rule::ExlusiveDisjunction` is now `Operation::Xor` (solnic)
45
- * [BREAKING] `Rule::Implication` is now `Operation::Implication` (solnic)
46
- * [BREAKING] `Rule::Set` is now `Operation::Set` (solnic)
47
- * [BREAKING] `Rule::Each` is now `Operation::Each` (solnic)
48
- * [BREAKING] `Rule.new` accepts a predicate function as its first arg now (solnic)
49
- * [BREAKING] `Rule#name` is now `Rule#id` (solnic)
50
- * `Rule#parameters` is public now (solnic)
52
+ - [BRAEKING] New result API (solnic)
53
+ - [BREAKING] `Predicate` is now `Rule::Predicate` (solnic)
54
+ - [BREAKING] `Rule::Conjunction` is now `Operation::And` (solnic)
55
+ - [BREAKING] `Rule::Disjunction` is now `Operation::Or` (solnic)
56
+ - [BREAKING] `Rule::ExlusiveDisjunction` is now `Operation::Xor` (solnic)
57
+ - [BREAKING] `Rule::Implication` is now `Operation::Implication` (solnic)
58
+ - [BREAKING] `Rule::Set` is now `Operation::Set` (solnic)
59
+ - [BREAKING] `Rule::Each` is now `Operation::Each` (solnic)
60
+ - [BREAKING] `Rule.new` accepts a predicate function as its first arg now (solnic)
61
+ - [BREAKING] `Rule#name` is now `Rule#id` (solnic)
62
+ - `Rule#parameters` is public now (solnic)
51
63
 
52
64
  [Compare v0.3.0...v0.4.0](https://github.com/dry-rb/dry-logic/compare/v0.3.0...v0.4.0)
53
65
 
@@ -55,14 +67,14 @@ This is a partial rewrite focused on internal clean up and major performance imp
55
67
 
56
68
  ### Added
57
69
 
58
- * `:type?` predicate imported from dry-types (solnic)
59
- * `Rule#curry` interface (solnic)
70
+ - `:type?` predicate imported from dry-types (solnic)
71
+ - `Rule#curry` interface (solnic)
60
72
 
61
73
  ### Changed
62
74
 
63
- * Predicates AST now includes information about args (names & possible values) (fran-worley + solnic)
64
- * Predicates raise errors when they are called with invalid arity (fran-worley + solnic)
65
- * Rules no longer evaluate input twice when building result objects (solnic)
75
+ - Predicates AST now includes information about args (names & possible values) (fran-worley + solnic)
76
+ - Predicates raise errors when they are called with invalid arity (fran-worley + solnic)
77
+ - Rules no longer evaluate input twice when building result objects (solnic)
66
78
 
67
79
  [Compare v0.2.3...v0.3.0](https://github.com/dry-rb/dry-logic/compare/v0.2.3...v0.3.0)
68
80
 
@@ -70,12 +82,12 @@ This is a partial rewrite focused on internal clean up and major performance imp
70
82
 
71
83
  ### Added
72
84
 
73
- * `not_eql?`, `includes?`, `excludes?` predicates (fran-worley)
85
+ - `not_eql?`, `includes?`, `excludes?` predicates (fran-worley)
74
86
 
75
87
  ### Changed
76
88
 
77
- * Renamed `inclusion?` to `included_in?` and deprecated `inclusion?` (fran-worley)
78
- * Renamed `exclusion?` to `excluded_from?` and deprecated `exclusion?` (fran-worley)
89
+ - Renamed `inclusion?` to `included_in?` and deprecated `inclusion?` (fran-worley)
90
+ - Renamed `exclusion?` to `excluded_from?` and deprecated `exclusion?` (fran-worley)
79
91
 
80
92
  [Compare v0.2.2...v0.2.3](https://github.com/dry-rb/dry-logic/compare/v0.2.2...v0.2.3)
81
93
 
@@ -83,7 +95,7 @@ This is a partial rewrite focused on internal clean up and major performance imp
83
95
 
84
96
  ### Added
85
97
 
86
- * `number?`, `odd?`, `even?` predicates (fran-worley)
98
+ - `number?`, `odd?`, `even?` predicates (fran-worley)
87
99
 
88
100
  [Compare v0.2.1...v0.2.2](https://github.com/dry-rb/dry-logic/compare/v0.2.1...v0.2.2)
89
101
 
@@ -91,13 +103,13 @@ This is a partial rewrite focused on internal clean up and major performance imp
91
103
 
92
104
  ### Fixed
93
105
 
94
- * Result AST for `Rule::Each` correctly maps elements with eql inputs (solnic)
106
+ - Result AST for `Rule::Each` correctly maps elements with eql inputs (solnic)
95
107
 
96
108
  # v0.2.0 2016-03-11
97
109
 
98
110
  ### Changed
99
111
 
100
- * Entire AST has been redefined (solnic)
112
+ - Entire AST has been redefined (solnic)
101
113
 
102
114
  [Compare v0.1.4...v0.2.0](https://github.com/dry-rb/dry-logic/compare/v0.1.4...v0.2.0)
103
115
 
@@ -105,7 +117,7 @@ This is a partial rewrite focused on internal clean up and major performance imp
105
117
 
106
118
  ### Added
107
119
 
108
- * Support for hash-names in `Check` and `Result` which can properly resolve input
120
+ - Support for hash-names in `Check` and `Result` which can properly resolve input
109
121
  from nested results (solnic)
110
122
 
111
123
  [Compare v0.1.3...v0.1.4](https://github.com/dry-rb/dry-logic/compare/v0.1.3...v0.1.4)
@@ -114,11 +126,11 @@ This is a partial rewrite focused on internal clean up and major performance imp
114
126
 
115
127
  ### Added
116
128
 
117
- * Support for resolving input from `Rule::Result` (solnic)
129
+ - Support for resolving input from `Rule::Result` (solnic)
118
130
 
119
131
  ### Changed
120
132
 
121
- * `Check` and `Result` carry original input(s) (solnic)
133
+ - `Check` and `Result` carry original input(s) (solnic)
122
134
 
123
135
  [Compare v0.1.2...v0.1.3](https://github.com/dry-rb/dry-logic/compare/v0.1.2...v0.1.3)
124
136
 
@@ -126,7 +138,7 @@ This is a partial rewrite focused on internal clean up and major performance imp
126
138
 
127
139
  ### Fixed
128
140
 
129
- * `xor` returns wrapped results when used against another result-rule (solnic)
141
+ - `xor` returns wrapped results when used against another result-rule (solnic)
130
142
 
131
143
  [Compare v0.1.1...v0.1.2](https://github.com/dry-rb/dry-logic/compare/v0.1.1...v0.1.2)
132
144
 
@@ -134,9 +146,9 @@ This is a partial rewrite focused on internal clean up and major performance imp
134
146
 
135
147
  ### Added
136
148
 
137
- * `Rule::Attr` which can be applied to a data object with attr readers (SunnyMagadan)
138
- * `Rule::Result` which can be applied to a result object (solnic)
139
- * `true?` and `false?` predicates (solnic)
149
+ - `Rule::Attr` which can be applied to a data object with attr readers (SunnyMagadan)
150
+ - `Rule::Result` which can be applied to a result object (solnic)
151
+ - `true?` and `false?` predicates (solnic)
140
152
 
141
153
  [Compare v0.1.0...v0.1.1](https://github.com/dry-rb/dry-logic/compare/v0.1.0...v0.1.1)
142
154
 
data/Gemfile CHANGED
@@ -3,7 +3,6 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  group :test do
6
- gem 'codeclimate-test-reporter', platform: :mri
7
6
  gem 'simplecov', require: false, platform: :mri
8
7
  end
9
8
 
data/README.md CHANGED
@@ -1,6 +1,5 @@
1
1
  [gem]: https://rubygems.org/gems/dry-logic
2
2
  [travis]: https://travis-ci.org/dry-rb/dry-logic
3
- [gemnasium]: https://gemnasium.com/dry-rb/dry-logic
4
3
  [codeclimate]: https://codeclimate.com/github/dry-rb/dry-logic
5
4
  [coveralls]: https://coveralls.io/r/dry-rb/dry-logic
6
5
  [inchpages]: http://inch-ci.org/github/dry-rb/dry-logic
@@ -9,7 +8,6 @@
9
8
 
10
9
  [![Gem Version](https://badge.fury.io/rb/dry-logic.svg)][gem]
11
10
  [![Build Status](https://travis-ci.org/dry-rb/dry-logic.svg?branch=master)][travis]
12
- [![Dependency Status](https://gemnasium.com/dry-rb/dry-logic.svg)][gemnasium]
13
11
  [![Code Climate](https://codeclimate.com/github/dry-rb/dry-logic/badges/gpa.svg)][codeclimate]
14
12
  [![Test Coverage](https://codeclimate.com/github/dry-rb/dry-logic/badges/coverage.svg)][codeclimate]
15
13
  [![Inline docs](http://inch-ci.org/github/dry-rb/dry-logic.svg?branch=master)][inchpages]
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require 'dry/logic'
5
+ require 'dry/logic/predicates'
6
+
7
+ include Dry::Logic
8
+
9
+ require "irb"
10
+ IRB.start
@@ -10,7 +10,6 @@ Gem::Specification.new do |spec|
10
10
  spec.license = 'MIT'
11
11
 
12
12
  spec.files = `git ls-files -z`.split("\x0")
13
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
13
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
14
  spec.require_paths = ['lib']
16
15
 
@@ -49,11 +49,11 @@ module Dry
49
49
  rule[evaluator[hash]]
50
50
  end
51
51
 
52
- def ast(input = nil)
53
- if input
54
- [type, [path, rule.ast(evaluator[input])]]
55
- else
52
+ def ast(input = Undefined)
53
+ if input.equal?(Undefined) || !input.is_a?(Hash)
56
54
  [type, [path, rule.ast]]
55
+ else
56
+ [type, [path, rule.ast(evaluator[input])]]
57
57
  end
58
58
  end
59
59
 
@@ -10,7 +10,11 @@ module Dry
10
10
  end
11
11
 
12
12
  def call(input)
13
- Result.new(!rule[input], id) { ast(input) }
13
+ Result.new(rule.(input).failure?, id) { ast(input) }
14
+ end
15
+
16
+ def [](input)
17
+ !rule[input]
14
18
  end
15
19
  end
16
20
  end
@@ -14,9 +14,10 @@ module Dry
14
14
  input.kind_of?(type)
15
15
  end
16
16
 
17
- def none?(input)
17
+ def nil?(input)
18
18
  input.nil?
19
19
  end
20
+ alias_method :none?, :nil?
20
21
 
21
22
  def key?(name, input)
22
23
  input.key?(name)
@@ -1,5 +1,7 @@
1
1
  require 'dry/core/constants'
2
+
2
3
  require 'dry/logic/rule'
4
+ require 'dry/logic/rule/predicate'
3
5
 
4
6
  module Dry
5
7
  module Logic
@@ -1,5 +1,5 @@
1
1
  module Dry
2
2
  module Logic
3
- VERSION = '0.4.2'.freeze
3
+ VERSION = '0.5.0'.freeze
4
4
  end
5
5
  end
@@ -1,7 +1,7 @@
1
1
  require 'dry/logic/predicates'
2
2
 
3
3
  RSpec.shared_examples 'predicates' do
4
- let(:none?) { Dry::Logic::Predicates[:none?] }
4
+ let(:nil?) { Dry::Logic::Predicates[:nil?] }
5
5
 
6
6
  let(:array?) { Dry::Logic::Predicates[:array?] }
7
7
 
@@ -92,6 +92,20 @@ RSpec.describe Operations::Key do
92
92
  end
93
93
  end
94
94
 
95
+ describe '#ast' do
96
+ it 'returns ast without the input' do
97
+ expect(operation.ast).to eql(
98
+ [:key, [:user, [:predicate, [:key?, [[:name, :age], [:input, Undefined]]]]]]
99
+ )
100
+ end
101
+
102
+ it 'returns ast with the input' do
103
+ expect(operation.ast(user: 'jane')).to eql(
104
+ [:key, [:user, [:predicate, [:key?, [[:name, :age], [:input, 'jane']]]]]]
105
+ )
106
+ end
107
+ end
108
+
95
109
  describe '#and' do
96
110
  subject(:operation) do
97
111
  Operations::Key.new(Rule::Predicate.new(str?), name: [:user, :name])
@@ -10,6 +10,15 @@ RSpec.describe Operations::Negation do
10
10
  expect(operation.('19')).to be_success
11
11
  expect(operation.(17)).to be_failure
12
12
  end
13
+
14
+ context 'double negation' do
15
+ subject(:double_negation) { Operations::Negation.new(operation) }
16
+
17
+ it 'works as rule' do
18
+ expect(double_negation.('19')).to be_failure
19
+ expect(double_negation.(17)).to be_success
20
+ end
21
+ end
13
22
  end
14
23
 
15
24
  describe '#to_ast' do
@@ -3,7 +3,7 @@ RSpec.describe Operations::Or do
3
3
 
4
4
  include_context 'predicates'
5
5
 
6
- let(:left) { Rule::Predicate.new(none?) }
6
+ let(:left) { Rule::Predicate.new(nil?) }
7
7
  let(:right) { Rule::Predicate.new(gt?).curry(18) }
8
8
 
9
9
  let(:other) do
@@ -22,7 +22,7 @@ RSpec.describe Operations::Or do
22
22
  it 'returns ast' do
23
23
  expect(operation.to_ast).to eql(
24
24
  [:or, [
25
- [:predicate, [:none?, [[:input, Undefined]]]],
25
+ [:predicate, [:nil?, [[:input, Undefined]]]],
26
26
  [:predicate, [:gt?, [[:num, 18], [:input, Undefined]]]]]
27
27
  ]
28
28
  )
@@ -31,7 +31,7 @@ RSpec.describe Operations::Or do
31
31
  it 'returns result ast' do
32
32
  expect(operation.(17).to_ast).to eql(
33
33
  [:or, [
34
- [:predicate, [:none?, [[:input, 17]]]],
34
+ [:predicate, [:nil?, [[:input, 17]]]],
35
35
  [:predicate, [:gt?, [[:num, 18], [:input, 17]]]]]
36
36
  ]
37
37
  )
@@ -40,7 +40,7 @@ RSpec.describe Operations::Or do
40
40
  it 'returns failure result ast' do
41
41
  expect(operation.with(id: :age).(17).to_ast).to eql(
42
42
  [:failure, [:age, [:or, [
43
- [:predicate, [:none?, [[:input, 17]]]],
43
+ [:predicate, [:nil?, [[:input, 17]]]],
44
44
  [:predicate, [:gt?, [[:num, 18], [:input, 17]]]]]
45
45
  ]]]
46
46
  )
@@ -67,7 +67,7 @@ RSpec.describe Operations::Or do
67
67
 
68
68
  describe '#to_s' do
69
69
  it 'returns string representation' do
70
- expect(operation.to_s).to eql('none? OR gt?(18)')
70
+ expect(operation.to_s).to eql('nil? OR gt?(18)')
71
71
  end
72
72
  end
73
73
  end
@@ -1,8 +1,8 @@
1
1
  require 'dry/logic/predicates'
2
2
 
3
3
  RSpec.describe Dry::Logic::Predicates do
4
- describe '#none?' do
5
- let(:predicate_name) { :none? }
4
+ describe '#nil?' do
5
+ let(:predicate_name) { :nil? }
6
6
 
7
7
  context 'when value is nil' do
8
8
  let(:arguments_list) { [[nil]] }
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.2
4
+ version: 0.5.0
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-09-15 00:00:00.000000000 Z
11
+ date: 2019-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-core
@@ -119,6 +119,7 @@ files:
119
119
  - LICENSE
120
120
  - README.md
121
121
  - Rakefile
122
+ - bin/console
122
123
  - dry-logic.gemspec
123
124
  - examples/basic.rb
124
125
  - lib/dry-logic.rb
@@ -220,8 +221,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
220
221
  - !ruby/object:Gem::Version
221
222
  version: '0'
222
223
  requirements: []
223
- rubyforge_project:
224
- rubygems_version: 2.6.11
224
+ rubygems_version: 3.0.2
225
225
  signing_key:
226
226
  specification_version: 4
227
227
  summary: Predicate logic with rule composition