dry-logic 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 35900556fff5ca088cf3f66f319d770c4e15ba1f91a2298a1db096498ea01fb4
4
- data.tar.gz: 942707a00d2f4424d2cd737e042877af2b15cf8a621564b463c254b98a72f9df
3
+ metadata.gz: 1e3f807ddbab8319c1c039978115800a4f8c6d41b337815cdaf6226563716a3d
4
+ data.tar.gz: 79cb10c28f23ebc523c15f4cd3da9657aeeb9923f7ed778bfaaa10c5dd59824c
5
5
  SHA512:
6
- metadata.gz: 37f3b82b384a0b34b51f966665e5b74c7b9834e25a02ca6f096d7bfac4b0de100d023341aeba212ddee1c8b9a1e5a2281c25db6ba66058b9884cc926f95520e1
7
- data.tar.gz: 174ec949de041f18c8a8b95b2e8db5c483e949e30be69da09f02032a29d2ee7ff3f823d44269e63b45c8ac201f75c4c1b520a02f2c6fbf6435dd725c729eba72
6
+ metadata.gz: ad9c5d83eb742610eddb8ea47be7a31cffcfee956223d08a1f595c90b02289d16b78e0dfe4b7ff48cf3fec7b79a586aab148a8e00afc67a9b8e0a6600f8687cc
7
+ data.tar.gz: 1df8067e98f4b90ff921a1d0eccaef3e29aa1b819337110cb2b20deb2e60215433118d2ba30bd54f41e5dec634b76a65d52c18fd924992c62ba4735a5fbd97a1
data/CHANGELOG.md CHANGED
@@ -1,8 +1,32 @@
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
+
13
+ ## 1.1.1 2021-04-14
14
+
15
+
16
+ ### Fixed
17
+
18
+ - Fixed a crash under jruby caused by arg splatting in Binary operations (@flash-gordon)
19
+
20
+
21
+ [Compare v1.1.0...v1.1.1](https://github.com/dry-rb/dry-logic/compare/v1.1.0...v1.1.1)
22
+
3
23
  ## 1.1.0 2020-12-26
4
24
 
5
25
 
26
+ ### Fixed
27
+
28
+ - Nested `Check` operations no longer crash under MRI 3.0 (@oleander)
29
+
6
30
  ### Changed
7
31
 
8
32
  - Switched to equalizer from dry-core (@solnic)
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2015-2020 dry-rb team
3
+ Copyright (c) 2015-2021 dry-rb team
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of
6
6
  this software and associated documentation files (the "Software"), to deal in
@@ -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
@@ -10,9 +10,10 @@ module Dry
10
10
 
11
11
  attr_reader :right
12
12
 
13
- def initialize(*rules, **options)
13
+ def initialize(left, right, **options)
14
14
  super
15
- @left, @right = rules
15
+ @left = left
16
+ @right = right
16
17
  end
17
18
 
18
19
  def ast(input = Undefined)
@@ -12,7 +12,7 @@ module Dry
12
12
 
13
13
  def self.new(rule, **options)
14
14
  if options[:evaluator]
15
- super(rule, options)
15
+ super(rule, **options)
16
16
  else
17
17
  keys = options.fetch(:keys)
18
18
  evaluator = Evaluator::Set.new(keys)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Dry
4
4
  module Logic
5
- VERSION = "1.1.0"
5
+ VERSION = "1.2.0"
6
6
  end
7
7
  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: 1.1.0
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: 2020-12-26 00:00:00.000000000 Z
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
@@ -146,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
147
  - !ruby/object:Gem::Version
147
148
  version: '0'
148
149
  requirements: []
149
- rubygems_version: 3.1.4
150
+ rubygems_version: 3.1.6
150
151
  signing_key:
151
152
  specification_version: 4
152
153
  summary: Predicate logic with rule composition