File: operations.html.md

package info (click to toggle)
ruby-dry-logic 1.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 728 kB
  • sloc: ruby: 4,929; makefile: 6
file content (207 lines) | stat: -rw-r--r-- 4,292 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
---
title: Operations
layout: gem-single
name: dry-logic
---

Operations work on one or more predicates (see predicates) and can be invoked in conjunction with other operations. Use `Dry::Logic::Builder` to evaluate operations & predicates.

``` ruby
extend Dry::Logic::Builder

is_zero = build { eql?(0) }
is_zero.call(10).success?
```

### Or (`|`, `or`)

> Returns true if one of its arguments is true. Similar to Ruby's `||` operator

Argument 1 | Argument 2 | Result
--- | --- | ---
true | true | true
true | false | true
false | true | true
false | false | false

``` ruby
is_number = build do
  int? | float? | number?
end

is_number.call(1).success? # => true
is_number.call(2.0).success? # => true
is_number.call('3').success? # => false
is_number.call('four').success? # => false
```

### Implication (`>`, `then`, `implication`)

> Similar to Ruby's `if then` expression

Argument 1 | Argument 2 | Result
--- | --- | ---
true | true | true
true | false | false
false | true | false
false | false | false

``` ruby
is_empty = build do
  (attr?(:empty) > empty?) | nil?
end

is_empty.call("").success? # => true
is_empty.call([]).success? # => true
is_empty.call({}).success? # => true
is_empty.call(nil).success? # => true

is_empty.call("string").success? # => false
is_empty.call(["array"]).success? # => false
is_empty.call({key: "value"}).success? # => false
```

### Exclusive or (`^`, `xor`)

> Returns true if at most one of its arguments are valid; otherwise, false

Argument 1 | Argument 2 | Result
--- | --- | ---
true | true | false
true | false | true
false | true | true
false | false | false

``` ruby
is_not_zero = build do
  lt?(0) ^ gt?(0)
end

is_not_zero.call(1).success? # => true
is_not_zero.call(0).success? # => false
is_not_zero.call(-1).success? # => true
```

### And (`&`, `and`)

> Returns true if both of its arguments are true. Similar to Ruby's `&&` operator

Argument 1 | Argument 2 | Result
--- | --- | ---
true | true | true
true | false | false
false | true | false
false | false | false

``` ruby
is_middle_aged = build do
  gt?(30) & lt?(50)
end

is_child.call(20).success? # => false
is_child.call(40).success? # => true
is_child.call(60).success? # => true
```

### Attribute (`attr`)

``` ruby
is_middle_aged = build do
  attr name: :age do
    gt?(30) & lt?(50)
  end
end

Person = Struct.new(:age)

is_middle_aged.call(Person.new(20)).success? # => false
is_middle_aged.call(Person.new(40)).success? # => true
is_middle_aged.call(Person.new(60)).success? # => false
```

### Each (`each`)

> True when any of the inputs is true when applied to the predicate. Similar to `Array#any?`

``` ruby
is_only_odd = build do
  each { odd? }
end

is_only_odd.call([1, 3, 5]).success? # => true
is_only_odd.call([4, 6, 8]).success? # => false
```

### Set (`set`)

> Applies input to an array of predicates. Returns true if all predicates yield true. Similar to `Array#all?`

``` ruby
is_natrual_and_odd = build do
  set(int?, odd?, gt?(1))
end

is_natrual_and_odd.call('5').success? # => false
is_natrual_and_odd.call(5).success? # => true
is_natrual_and_odd.call(-1).success? # => false
```

### Negation (`negation`, `not`)

> Returns true when predicate returns false. Similar to Ruby's `!` operator

``` ruby
is_present = build do
  negation(empty?)
end

is_present.call([1]).success? # => true
is_present.call([]).success? # => false
is_present.call("A").success? # => true
is_present.call("").success? # => false
```

### Key (`key`)

``` ruby
is_named = build do
  key name: [:user, :name] do
    str? & negation(empty?)
  end
end

is_named.call({ user: { name: "John" } }).success? # => true
is_named.call({ user: { name: nil } }).success? # => false
```

### Check (`check`)

``` ruby
is_allowed_to_drive = build do
  check keys: [:age] do
    gt?(18)
  end
end

is_allowed_to_drive.call({ age: 30 }).success? # => true
is_allowed_to_drive.call({ age: 10 }).success? # => false
```

Or when the predicate allows for more than one argument.

``` ruby
is_speeding = build do
  check keys: [:speed, :limit] do
    lt?
  end
end

is_speeding.call({ speed: 100, limit: 50 }).success? # => true
is_speeding.call({ speed: 40, limit: 50 }).success? # => false
```

Which is the same as this

``` ruby
input[:limit].lt?(*input.values_at(:speed))
```