ruy 1.0.0 → 2.0.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 +13 -5
- data/README.md +37 -24
- data/lib/ruy/conditions.rb +3 -0
- data/lib/ruy/conditions/all.rb +2 -1
- data/lib/ruy/conditions/any.rb +7 -6
- data/lib/ruy/conditions/assert.rb +16 -11
- data/lib/ruy/conditions/between.rb +33 -19
- data/lib/ruy/conditions/compound_condition.rb +26 -6
- data/lib/ruy/conditions/cond.rb +11 -6
- data/lib/ruy/conditions/condition.rb +38 -0
- data/lib/ruy/conditions/day_of_week.rb +30 -20
- data/lib/ruy/conditions/dig.rb +42 -0
- data/lib/ruy/conditions/eq.rb +20 -13
- data/lib/ruy/conditions/every.rb +35 -0
- data/lib/ruy/conditions/except.rb +5 -12
- data/lib/ruy/conditions/greater_than.rb +20 -14
- data/lib/ruy/conditions/greater_than_or_equal.rb +21 -14
- data/lib/ruy/conditions/in.rb +19 -14
- data/lib/ruy/conditions/in_cyclic_order.rb +21 -11
- data/lib/ruy/conditions/include.rb +21 -14
- data/lib/ruy/conditions/less_than.rb +19 -13
- data/lib/ruy/conditions/less_than_or_equal.rb +21 -13
- data/lib/ruy/conditions/some.rb +32 -0
- data/lib/ruy/conditions/tz.rb +33 -41
- data/lib/ruy/context.rb +43 -14
- data/lib/ruy/dsl.rb +60 -28
- data/lib/ruy/outcome.rb +11 -11
- data/lib/ruy/rule.rb +26 -20
- data/lib/ruy/time_pattern.rb +6 -23
- data/lib/ruy/utils/naming.rb +3 -3
- data/lib/ruy/utils/rules.rb +3 -3
- metadata +12 -10
- data/lib/ruy/conditions/included.rb +0 -28
@@ -0,0 +1,42 @@
|
|
1
|
+
module Ruy
|
2
|
+
module Conditions
|
3
|
+
|
4
|
+
# Navigates into a Hash allowing to define conditions over nested attributes
|
5
|
+
#
|
6
|
+
class Dig < CompoundCondition
|
7
|
+
attr_reader :chain
|
8
|
+
|
9
|
+
# @param chain [Array<Object>] Sequence of keys to navigate the hash hierarchy
|
10
|
+
# @example navigate key -> sub_key -> sub_sub_key
|
11
|
+
# Dig.new(:key, :sub_key, :sub_sub_key)
|
12
|
+
def initialize(*chain)
|
13
|
+
super
|
14
|
+
@chain = chain
|
15
|
+
end
|
16
|
+
|
17
|
+
def ==(o)
|
18
|
+
super &&
|
19
|
+
o.chain == @chain
|
20
|
+
end
|
21
|
+
|
22
|
+
protected
|
23
|
+
|
24
|
+
# @see CompoundCondition#evaluate
|
25
|
+
def evaluate(ctx)
|
26
|
+
ctx && Ruy::Utils::Rules.evaluate_conditions(conditions, ctx)
|
27
|
+
end
|
28
|
+
|
29
|
+
# @see CompoundCondition#resolve
|
30
|
+
def resolve(ctx)
|
31
|
+
@chain.reduce(ctx) do |currentctx, key|
|
32
|
+
if currentctx.include?(key)
|
33
|
+
Ruy::Context.new(currentctx.resolve(key))
|
34
|
+
else
|
35
|
+
return false
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/ruy/conditions/eq.rb
CHANGED
@@ -1,27 +1,34 @@
|
|
1
1
|
module Ruy
|
2
2
|
module Conditions
|
3
3
|
|
4
|
-
# Expects that a context attribute will be equal to a given value
|
4
|
+
# Expects that a context attribute will be equal to a given value
|
5
|
+
#
|
5
6
|
class Eq < Condition
|
6
|
-
attr_reader :
|
7
|
+
attr_reader :obj
|
7
8
|
|
8
|
-
# @param
|
9
|
-
# @param
|
10
|
-
|
9
|
+
# @param obj
|
10
|
+
# @param key
|
11
|
+
# @example evaluate that :key is 5
|
12
|
+
# Eq.new(5, :key)
|
13
|
+
def initialize(obj, *key)
|
11
14
|
super
|
12
|
-
@
|
13
|
-
@
|
14
|
-
end
|
15
|
-
|
16
|
-
def call(ctx)
|
17
|
-
@value == ctx.resolve(@attr)
|
15
|
+
@obj = obj
|
16
|
+
@key = key.first if key.any?
|
18
17
|
end
|
19
18
|
|
20
19
|
def ==(o)
|
21
20
|
o.kind_of?(Eq) &&
|
22
|
-
|
23
|
-
|
21
|
+
o.obj == @obj &&
|
22
|
+
o.key == @key
|
23
|
+
end
|
24
|
+
|
25
|
+
protected
|
26
|
+
|
27
|
+
# @see Condition#evaluate
|
28
|
+
def evaluate(value)
|
29
|
+
@obj == value
|
24
30
|
end
|
31
|
+
|
25
32
|
end
|
26
33
|
end
|
27
34
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Ruy
|
2
|
+
module Conditions
|
3
|
+
|
4
|
+
# Iterates over an Enumerable evaluating that every value
|
5
|
+
# matches the set of sub-conditions
|
6
|
+
#
|
7
|
+
class Every < CompoundCondition
|
8
|
+
|
9
|
+
# @param key
|
10
|
+
# @example check that every element of :array matches the sub-conditions
|
11
|
+
# Every.new(:array)
|
12
|
+
def initialize(*key)
|
13
|
+
super
|
14
|
+
@key = key.first if key.any?
|
15
|
+
end
|
16
|
+
|
17
|
+
def ==(o)
|
18
|
+
super &&
|
19
|
+
o.key == @key
|
20
|
+
end
|
21
|
+
|
22
|
+
protected
|
23
|
+
|
24
|
+
# @see CompoundCondition#evaluate
|
25
|
+
def evaluate(enum)
|
26
|
+
enum.all? do |ctx|
|
27
|
+
newctx = Ruy::Context.new(ctx)
|
28
|
+
|
29
|
+
Ruy::Utils::Rules.evaluate_conditions(conditions, newctx)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -1,29 +1,22 @@
|
|
1
1
|
module Ruy
|
2
2
|
module Conditions
|
3
3
|
|
4
|
-
# Expects that
|
5
|
-
#
|
6
|
-
# When a sub-rule is given, Except will expect an unsuccessful evaluation of that sub-rule.
|
7
|
-
# When a sub-rule is not given, Except will expect a context attribute is not equal to a given
|
8
|
-
# value.
|
4
|
+
# Expects that the sub-condition is not satisfied
|
9
5
|
class Except < CompoundCondition
|
10
6
|
|
11
|
-
# @
|
12
|
-
#
|
7
|
+
# @example check that :key does not contain a truthy value
|
8
|
+
# Except.new { assert :key }
|
13
9
|
# @yield a block in the context of the current rule
|
14
10
|
def initialize(&block)
|
15
11
|
super
|
16
12
|
instance_exec(&block) if block_given?
|
17
13
|
end
|
18
14
|
|
19
|
-
|
15
|
+
# @see CompoundCondition#evaluate
|
16
|
+
def evaluate(ctx)
|
20
17
|
!super
|
21
18
|
end
|
22
19
|
|
23
|
-
def ==(o)
|
24
|
-
o.kind_of?(Except) &&
|
25
|
-
@conditions == o.conditions
|
26
|
-
end
|
27
20
|
end
|
28
21
|
end
|
29
22
|
end
|
@@ -1,28 +1,34 @@
|
|
1
1
|
module Ruy
|
2
2
|
module Conditions
|
3
3
|
|
4
|
-
# Expects that a context attribute will be greater than the given value
|
4
|
+
# Expects that a context attribute will be greater than the given value
|
5
|
+
#
|
5
6
|
class GreaterThan < Condition
|
6
|
-
attr_reader :
|
7
|
+
attr_reader :obj
|
7
8
|
|
8
|
-
# @param
|
9
|
-
# @param
|
10
|
-
# @
|
11
|
-
|
9
|
+
# @param obj
|
10
|
+
# @param key
|
11
|
+
# @example expects that :key is greater than 5
|
12
|
+
# GreaterThan.new(5, :key)
|
13
|
+
def initialize(obj, *key)
|
12
14
|
super
|
13
|
-
@
|
14
|
-
@
|
15
|
-
end
|
16
|
-
|
17
|
-
def call(var_ctx)
|
18
|
-
@value < var_ctx.resolve(@attr)
|
15
|
+
@obj = obj
|
16
|
+
@key = key.first if key.any?
|
19
17
|
end
|
20
18
|
|
21
19
|
def ==(o)
|
22
20
|
o.kind_of?(GreaterThan) &&
|
23
|
-
|
24
|
-
|
21
|
+
o.obj == @obj &&
|
22
|
+
o.key == @key
|
23
|
+
end
|
24
|
+
|
25
|
+
protected
|
26
|
+
|
27
|
+
# @see Condition#evaluate
|
28
|
+
def evaluate(value)
|
29
|
+
@obj < value
|
25
30
|
end
|
31
|
+
|
26
32
|
end
|
27
33
|
end
|
28
34
|
end
|
@@ -1,28 +1,35 @@
|
|
1
1
|
module Ruy
|
2
2
|
module Conditions
|
3
3
|
|
4
|
-
# Expects that a context attribute will be greater than or
|
4
|
+
# Expects that a context attribute will be greater than or
|
5
|
+
# equal to the given value
|
6
|
+
#
|
5
7
|
class GreaterThanOrEqual < Condition
|
6
|
-
attr_reader :
|
8
|
+
attr_reader :obj
|
7
9
|
|
8
|
-
# @param
|
9
|
-
# @param
|
10
|
-
# @
|
11
|
-
|
10
|
+
# @param obj
|
11
|
+
# @param key
|
12
|
+
# @example check that :key is greater than or equal to 5
|
13
|
+
# GreaterThanOrEqual.new(5, :key)
|
14
|
+
def initialize(obj, *key)
|
12
15
|
super
|
13
|
-
@
|
14
|
-
@
|
15
|
-
end
|
16
|
-
|
17
|
-
def call(ctx)
|
18
|
-
@value <= ctx.resolve(@attr)
|
16
|
+
@obj = obj
|
17
|
+
@key = key.first if key.any?
|
19
18
|
end
|
20
19
|
|
21
20
|
def ==(o)
|
22
21
|
o.kind_of?(GreaterThanOrEqual) &&
|
23
|
-
|
24
|
-
|
22
|
+
o.obj == @obj &&
|
23
|
+
o.key == @key
|
24
|
+
end
|
25
|
+
|
26
|
+
protected
|
27
|
+
|
28
|
+
# @see Condition#evaluate
|
29
|
+
def evaluate(value)
|
30
|
+
@obj <= value
|
25
31
|
end
|
32
|
+
|
26
33
|
end
|
27
34
|
end
|
28
35
|
end
|
data/lib/ruy/conditions/in.rb
CHANGED
@@ -1,27 +1,32 @@
|
|
1
1
|
module Ruy
|
2
2
|
module Conditions
|
3
3
|
|
4
|
-
# Expects that a context attribute is included in a set of values
|
4
|
+
# Expects that a context attribute is included in a set of values
|
5
|
+
#
|
5
6
|
class In < Condition
|
6
|
-
attr_reader :
|
7
|
+
attr_reader :ary
|
7
8
|
|
8
|
-
# @param
|
9
|
-
# @param
|
10
|
-
# @
|
11
|
-
|
9
|
+
# @param ary
|
10
|
+
# @param key
|
11
|
+
# @example check that :key is included in [1,3,5]
|
12
|
+
# In.new([1,3,5], :key)
|
13
|
+
def initialize(ary, *key)
|
12
14
|
super
|
13
|
-
@
|
14
|
-
@
|
15
|
-
end
|
16
|
-
|
17
|
-
def call(var_ctx)
|
18
|
-
self.values.include?(var_ctx.resolve(self.attr))
|
15
|
+
@ary = ary
|
16
|
+
@key = key.first if key.any?
|
19
17
|
end
|
20
18
|
|
21
19
|
def ==(o)
|
22
20
|
o.kind_of?(In) &&
|
23
|
-
|
24
|
-
|
21
|
+
o.ary == @ary &&
|
22
|
+
o.key == @key
|
23
|
+
end
|
24
|
+
|
25
|
+
protected
|
26
|
+
|
27
|
+
# @see Condition#evaluate
|
28
|
+
def evaluate(value)
|
29
|
+
@ary.include?(value)
|
25
30
|
end
|
26
31
|
end
|
27
32
|
|
@@ -1,18 +1,34 @@
|
|
1
1
|
module Ruy
|
2
2
|
module Conditions
|
3
|
+
|
4
|
+
# Expects that a value is contained in a cyclic order
|
5
|
+
#
|
3
6
|
class InCyclicOrder < Condition
|
4
|
-
attr_reader :from, :to
|
7
|
+
attr_reader :from, :to
|
5
8
|
|
6
|
-
|
9
|
+
# @param from left bound
|
10
|
+
# @param to right bound
|
11
|
+
# @param key
|
12
|
+
# @example check that :key is included in a cycle defined between 100 and -100
|
13
|
+
# InCyclicOrder.new(100, -100, :key)
|
14
|
+
def initialize(from, to, *key)
|
7
15
|
super
|
8
16
|
@from = from
|
9
17
|
@to = to
|
10
|
-
@
|
18
|
+
@key = key.first if key.any?
|
19
|
+
end
|
20
|
+
|
21
|
+
def ==(o)
|
22
|
+
o.kind_of?(self.class) &&
|
23
|
+
o.from == @from &&
|
24
|
+
o.to == @to &&
|
25
|
+
o.key == @key
|
11
26
|
end
|
12
27
|
|
13
|
-
|
14
|
-
value = ctx.resolve(@attr)
|
28
|
+
protected
|
15
29
|
|
30
|
+
# @see Condition#evaluate
|
31
|
+
def evaluate(value)
|
16
32
|
if @from > @to
|
17
33
|
(@from <= value || @to >= value)
|
18
34
|
else
|
@@ -20,12 +36,6 @@ module Ruy
|
|
20
36
|
end
|
21
37
|
end
|
22
38
|
|
23
|
-
def ==(o)
|
24
|
-
o.kind_of?(self.class) &&
|
25
|
-
@from == o.from &&
|
26
|
-
@to == o.to &&
|
27
|
-
@attr == o.attr
|
28
|
-
end
|
29
39
|
end
|
30
40
|
end
|
31
41
|
end
|
@@ -1,28 +1,35 @@
|
|
1
1
|
module Ruy
|
2
2
|
module Conditions
|
3
3
|
|
4
|
-
# Expects that a value is included in a set of values
|
4
|
+
# Expects that a value is included in a set of values
|
5
|
+
# from the context attribute
|
6
|
+
#
|
5
7
|
class Include < Condition
|
6
|
-
attr_reader :
|
8
|
+
attr_reader :obj
|
7
9
|
|
8
|
-
# @param
|
9
|
-
# @param
|
10
|
-
# @
|
11
|
-
|
10
|
+
# @param obj
|
11
|
+
# @param key
|
12
|
+
# @example check that :key includes 5
|
13
|
+
# Include.new(5, :key)
|
14
|
+
def initialize(obj, *key)
|
12
15
|
super
|
13
|
-
@
|
14
|
-
@
|
15
|
-
end
|
16
|
-
|
17
|
-
def call(ctx)
|
18
|
-
ctx.resolve(self.attr).include?(self.value)
|
16
|
+
@obj = obj
|
17
|
+
@key = key.first if key.any?
|
19
18
|
end
|
20
19
|
|
21
20
|
def ==(o)
|
22
21
|
o.kind_of?(Include) &&
|
23
|
-
|
24
|
-
|
22
|
+
o.obj == @obj &&
|
23
|
+
o.key == @key
|
24
|
+
end
|
25
|
+
|
26
|
+
protected
|
27
|
+
|
28
|
+
# @see Condition#evaluate
|
29
|
+
def evaluate(enum)
|
30
|
+
enum.include?(@obj)
|
25
31
|
end
|
32
|
+
|
26
33
|
end
|
27
34
|
end
|
28
35
|
end
|
@@ -1,25 +1,31 @@
|
|
1
1
|
module Ruy
|
2
2
|
module Conditions
|
3
3
|
|
4
|
-
# Expects that a context attribute will be less than given value
|
4
|
+
# Expects that a context attribute will be less than the given value
|
5
|
+
#
|
5
6
|
class LessThan < Condition
|
6
|
-
attr_reader :
|
7
|
+
attr_reader :obj
|
7
8
|
|
8
|
-
# @param
|
9
|
-
# @param
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
def call(ctx)
|
16
|
-
@value > ctx.resolve(@attr)
|
9
|
+
# @param obj
|
10
|
+
# @param key
|
11
|
+
# @example check that :key is less than 5
|
12
|
+
# LessThan.new(5, :key)
|
13
|
+
def initialize(obj, *key)
|
14
|
+
@obj = obj
|
15
|
+
@key = key.first if key.any?
|
17
16
|
end
|
18
17
|
|
19
18
|
def ==(o)
|
20
19
|
o.kind_of?(LessThan) &&
|
21
|
-
|
22
|
-
|
20
|
+
o.obj == @obj &&
|
21
|
+
o.key == @key
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
|
26
|
+
# @see Condition#evaluate
|
27
|
+
def evaluate(value)
|
28
|
+
@obj > value
|
23
29
|
end
|
24
30
|
end
|
25
31
|
end
|