dry-logic 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/lib/dry/logic/builder.rb +91 -0
- data/lib/dry/logic/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e3f807ddbab8319c1c039978115800a4f8c6d41b337815cdaf6226563716a3d
|
4
|
+
data.tar.gz: 79cb10c28f23ebc523c15f4cd3da9657aeeb9923f7ed778bfaaa10c5dd59824c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad9c5d83eb742610eddb8ea47be7a31cffcfee956223d08a1f595c90b02289d16b78e0dfe4b7ff48cf3fec7b79a586aab148a8e00afc67a9b8e0a6600f8687cc
|
7
|
+
data.tar.gz: 1df8067e98f4b90ff921a1d0eccaef3e29aa1b819337110cb2b20deb2e60215433118d2ba30bd54f41e5dec634b76a65d52c18fd924992c62ba4735a5fbd97a1
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,15 @@
|
|
1
1
|
<!--- DO NOT EDIT THIS FILE - IT'S AUTOMATICALLY GENERATED VIA DEVTOOLS --->
|
2
2
|
|
3
|
+
## 1.2.0 2021-04-26
|
4
|
+
|
5
|
+
|
6
|
+
### Added
|
7
|
+
|
8
|
+
- Add predicate and operation builder DSL (@oleander)
|
9
|
+
|
10
|
+
|
11
|
+
[Compare v1.1.1...v1.2.0](https://github.com/dry-rb/dry-logic/compare/v1.1.1...v1.2.0)
|
12
|
+
|
3
13
|
## 1.1.1 2021-04-14
|
4
14
|
|
5
15
|
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "dry/logic"
|
4
|
+
require "singleton"
|
5
|
+
require "delegate"
|
6
|
+
|
7
|
+
module Dry
|
8
|
+
module Logic
|
9
|
+
autoload :Operations, "dry/logic/operations"
|
10
|
+
autoload :Predicates, "dry/logic/predicates"
|
11
|
+
module Builder
|
12
|
+
IGNORED_OPERATIONS = %i[
|
13
|
+
Abstract
|
14
|
+
Binary
|
15
|
+
Unary
|
16
|
+
].freeze
|
17
|
+
|
18
|
+
IGNORED_PREDICATES = [
|
19
|
+
:predicate
|
20
|
+
].freeze
|
21
|
+
|
22
|
+
# Predicate and operation builder
|
23
|
+
#
|
24
|
+
# @block [Proc]
|
25
|
+
# @return [Builder::Result]
|
26
|
+
# @example Check if input is zero
|
27
|
+
# is_zero = Dry::Logic::Builder.call do
|
28
|
+
# negation { lt?(0) ^ gt?(0) }
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# p is_zero.call(1) # => false
|
32
|
+
# p is_zero.call(0) # => true
|
33
|
+
# p is_zero.call(-1) # => false
|
34
|
+
def call(&context)
|
35
|
+
Context.instance.call(&context)
|
36
|
+
end
|
37
|
+
module_function :call
|
38
|
+
alias_method :build, :call
|
39
|
+
public :call, :build
|
40
|
+
|
41
|
+
class Context
|
42
|
+
include Dry::Logic
|
43
|
+
include Singleton
|
44
|
+
|
45
|
+
module Predicates
|
46
|
+
include Logic::Predicates
|
47
|
+
end
|
48
|
+
|
49
|
+
# @see Builder#call
|
50
|
+
def call(&context)
|
51
|
+
instance_eval(&context)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Defines custom predicate
|
55
|
+
#
|
56
|
+
# @name [Symbol] Name of predicate
|
57
|
+
# @Context [Proc]
|
58
|
+
def predicate(name, &context)
|
59
|
+
if singleton_class.method_defined?(name)
|
60
|
+
singleton_class.undef_method(name)
|
61
|
+
end
|
62
|
+
|
63
|
+
prerdicate = Rule::Predicate.new(context)
|
64
|
+
|
65
|
+
define_singleton_method(name) do |*args|
|
66
|
+
prerdicate.curry(*args)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# Defines methods for operations and predicates
|
71
|
+
def initialize
|
72
|
+
Operations.constants(false).each do |name|
|
73
|
+
next if IGNORED_OPERATIONS.include?(name)
|
74
|
+
|
75
|
+
operation = Operations.const_get(name)
|
76
|
+
|
77
|
+
define_singleton_method(name.downcase) do |*args, **kwargs, &block|
|
78
|
+
operation.new(*call(&block), *args, **kwargs)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
Predicates::Methods.instance_methods(false).each do |name|
|
83
|
+
unless IGNORED_PREDICATES.include?(name)
|
84
|
+
predicate(name, &Predicates[name])
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
data/lib/dry/logic/version.rb
CHANGED
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: 1.
|
4
|
+
version: 1.2.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: 2021-04-
|
11
|
+
date: 2021-04-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|
@@ -100,6 +100,7 @@ files:
|
|
100
100
|
- lib/dry-logic.rb
|
101
101
|
- lib/dry/logic.rb
|
102
102
|
- lib/dry/logic/appliable.rb
|
103
|
+
- lib/dry/logic/builder.rb
|
103
104
|
- lib/dry/logic/evaluator.rb
|
104
105
|
- lib/dry/logic/operations.rb
|
105
106
|
- lib/dry/logic/operations/abstract.rb
|