dry-logic 0.1.1 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9042bd42bf10c567eb88dc6e830ebface1daf974
4
- data.tar.gz: dc92016f7c8a14ddd0db49f33d0818130cc9cada
3
+ metadata.gz: 42c06d53e8a80811c9bb63f5bc9538c87d9de6a6
4
+ data.tar.gz: 24d21638462c0dc1ce8cf1c685df35133d8d4c06
5
5
  SHA512:
6
- metadata.gz: 049784897429ec2b7064da4217cd508d6557bdf4a3c9208e716a608d43c03b9f449a782cef01df03a1786ecb11216f2208ed576eaf1ef8ff93cd92a66ce347af
7
- data.tar.gz: 64b6dbcd4d3e5b9e06f6b50fb1c1bfd6bd7b856d01b67f2357e46f93a0f716f600bce868b0aec0bd83d9a0e5a8141583aaf18a01a5a119766044f792c93a7467
6
+ metadata.gz: 6e554a728aa438a6293611ef53f7ac404932114a4ec91acee86295808d0ed39ce92e9e5afa2d1bdd5e1892bdddb949a3b6cb78b8bf61017457738e1ba4374465
7
+ data.tar.gz: d7294c09046932acc67f0778dacfa9f3e86d6bb3d37b864fb10e7e52c0630fff9a9e2095f287704c5ca7cdd636c05856b281a453ac09af334941954d2a5f0c43
@@ -1,9 +1,16 @@
1
+ # v0.1.2 2016-01-19
2
+
3
+ ### Fixed
4
+
5
+ * `xor` returns wrapped results when used against another result-rule (solnic)
6
+
1
7
  # v0.1.1 2016-01-18
2
8
 
3
9
  ### Added
4
10
 
5
11
  * `Rule::Attr` which can be applied to a data object with attr readers (SunnyMagadan)
6
12
  * `Rule::Result` which can be applied to a result object (solnic)
13
+ * `true?` and `false?` predicates (solnic)
7
14
 
8
15
  [Compare v0.1.0...v0.1.1](https://github.com/dryrb/dry-logic/compare/v0.1.0...v0.1.1)
9
16
 
@@ -2,9 +2,12 @@ module Dry
2
2
  module Logic
3
3
  def self.Result(input, value, rule)
4
4
  case value
5
- when Result then value.class.new(value.input, value.success?, rule)
6
- when Array then Result::Set.new(input, value, rule)
7
- else Result::Value.new(input, value, rule)
5
+ when Result
6
+ value.class.new(value.input, value.success?, rule)
7
+ when Array
8
+ Result::Set.new(input, value, rule)
9
+ else
10
+ Result::Value.new(input, value, rule)
8
11
  end
9
12
  end
10
13
 
@@ -72,6 +75,10 @@ module Dry
72
75
  [:input, [name, nil, [rule.to_ary]]]
73
76
  end
74
77
  alias_method :to_a, :to_ary
78
+
79
+ def wrapped?
80
+ true
81
+ end
75
82
  end
76
83
 
77
84
  def initialize(input, value, rule)
@@ -122,7 +129,14 @@ module Dry
122
129
  end
123
130
 
124
131
  def xor(other)
125
- Logic.Result(input, success? ^ other.(input).success?, rule)
132
+ other_result = other.(input)
133
+ value = success? ^ other_result.success?
134
+
135
+ if other_result.wrapped?
136
+ Result::Wrapped.new(input, value, rule)
137
+ else
138
+ Logic.Result(other_result.input, value, rule)
139
+ end
126
140
  end
127
141
 
128
142
  def success?
@@ -132,6 +146,10 @@ module Dry
132
146
  def failure?
133
147
  ! success?
134
148
  end
149
+
150
+ def wrapped?
151
+ false
152
+ end
135
153
  end
136
154
  end
137
155
  end
@@ -3,9 +3,13 @@ module Dry
3
3
  class Rule::Result < Rule
4
4
  def call(input)
5
5
  result = input[name]
6
- return result unless result.success?
7
6
  result_input = result.input
8
- Result::Wrapped.new(input, predicate.(result_input), self)
7
+
8
+ if result.success?
9
+ Result::Wrapped.new(input, predicate.(result_input), self)
10
+ else
11
+ result
12
+ end
9
13
  end
10
14
 
11
15
  def type
@@ -1,5 +1,5 @@
1
1
  module Dry
2
2
  module Logic
3
- VERSION = '0.1.1'.freeze
3
+ VERSION = '0.1.2'.freeze
4
4
  end
5
5
  end
@@ -7,6 +7,7 @@ RSpec.describe Dry::Logic::Rule::Result do
7
7
 
8
8
  let(:is_str) { Dry::Logic::Rule::Value.new(:name, str?) }
9
9
  let(:is_jane) { Dry::Logic::Rule::Result.new(:name, eql?.curry('jane')) }
10
+ let(:is_john) { Dry::Logic::Rule::Result.new(:name, eql?.curry('john')) }
10
11
 
11
12
  describe '#call' do
12
13
  it 'returns result of a predicate' do
@@ -15,19 +16,76 @@ RSpec.describe Dry::Logic::Rule::Result do
15
16
  expect(rule.(name: is_str.(nil))).to be_failure
16
17
  end
17
18
 
18
- it 'evaluates input for the ast' do
19
+ it 'evaluates successful input for the ast' do
19
20
  expect(rule.(name: is_str.('jane')).to_ary).to eql([
20
21
  :input, [
21
22
  :name, nil, [[:res, [:name, [:predicate, [:min_size?, [4]]]]]]
22
23
  ]
23
24
  ])
24
25
  end
26
+
27
+ it 'evaluates failed input for the ast' do
28
+ expect(rule.(name: is_str.(:john)).to_ary).to eql([
29
+ :input, [
30
+ :name, :john, [[:val, [:name, [:predicate, [:str?, []]]]]]
31
+ ]
32
+ ])
33
+ end
25
34
  end
26
35
 
27
36
  describe '#and' do
37
+ let(:conjunction) { rule.and(is_jane) }
38
+
28
39
  it 'returns result of a conjunction' do
29
- expect(rule.and(is_jane).(name: is_str.('jane'))).to be_success
30
- expect(rule.and(is_jane).(name: is_str.('john'))).to be_failure
40
+ expect(conjunction.(name: is_str.('jane'))).to be_success
41
+ expect(conjunction.(name: is_str.('john'))).to be_failure
42
+ end
43
+
44
+ it 'evaluates input for the ast' do
45
+ expect(conjunction.(name: is_str.('john')).to_ary).to eql([
46
+ :input, [
47
+ :name, nil, [[:res, [:name, [:predicate, [:eql?, ["jane"]]]]]]
48
+ ]
49
+ ])
50
+ end
51
+ end
52
+
53
+ describe '#xor' do
54
+ let(:xor) { rule.xor(is_john) }
55
+
56
+ it 'returns result of an exclusive disjunction' do
57
+ expect(xor.(name: is_str.('jane'))).to be_success
58
+ expect(xor.(name: is_str.('john'))).to be_failure
59
+ end
60
+
61
+ it 'evaluates input for the ast' do
62
+ result = xor.(name: is_str.('john'))
63
+
64
+ expect(result.to_ary).to eql([
65
+ :input, [
66
+ :name, nil, [[:res, [:name, [:predicate, [:min_size?, [4]]]]]]
67
+ ]
68
+ ])
69
+ end
70
+ end
71
+
72
+ describe '#then' do
73
+ let(:implication) { rule.then(is_jane) }
74
+
75
+ it 'returns result of an exclusive disjunction' do
76
+ expect(implication.(name: is_str.('jane'))).to be_success
77
+ expect(implication.(name: is_str.(nil))).to be_success
78
+ expect(implication.(name: is_str.('john'))).to be_failure
79
+ end
80
+
81
+ it 'evaluates input for the ast' do
82
+ result = implication.(name: is_str.('john'))
83
+
84
+ expect(result.to_ary).to eql([
85
+ :input, [
86
+ :name, nil, [[:res, [:name, [:predicate, [:eql?, ['jane']]]]]]
87
+ ]
88
+ ])
31
89
  end
32
90
  end
33
91
  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.1.1
4
+ version: 0.1.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: 2016-01-18 00:00:00.000000000 Z
11
+ date: 2016-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-container