ruy 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -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 :attr, :value
7
+ attr_reader :obj
7
8
 
8
- # @param value Expected value
9
- # @param attr Context attribute's name
10
- def initialize(value, attr)
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
- @value = value
13
- @attr = attr
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
- attr == o.attr &&
23
- value == o.value
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 a condition is not met.
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
- # @param value Non-expected value
12
- # @param attr Context attribute's name
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
- def call(ctx)
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 :attr, :value
7
+ attr_reader :obj
7
8
 
8
- # @param value
9
- # @param attr Context attribute's name
10
- # @yield a block in the context of the current rule
11
- def initialize(value, attr, &block)
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
- @value = value
14
- @attr = attr
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
- attr == o.attr &&
24
- value == o.value
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 equal to the given value.
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 :attr, :value
8
+ attr_reader :obj
7
9
 
8
- # @param value
9
- # @param attr Context attribute's name
10
- # @yield a block in the context of the current rule
11
- def initialize(value, attr, &block)
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
- @value = value
14
- @attr = attr
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
- attr == o.attr &&
24
- value == o.value
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
@@ -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 :attr, :values
7
+ attr_reader :ary
7
8
 
8
- # @param values Expected set of values
9
- # @param attr Context attribute's name
10
- # @yield a block in the context of the current rule
11
- def initialize(values, attr, &block)
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
- @values = values
14
- @attr = attr
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
- self.attr == o.attr &&
24
- self.values == o.values
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, :attr
7
+ attr_reader :from, :to
5
8
 
6
- def initialize(from, to, attr, &block)
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
- @attr = attr
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
- def call(ctx)
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 from the context attribute.
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 :attr, :value
8
+ attr_reader :obj
7
9
 
8
- # @param value Expected set of values
9
- # @param attr Context attribute's name
10
- # @yield a block in the context of the current rule
11
- def initialize(value, attr, &block)
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
- @value = value
14
- @attr = attr
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
- self.attr == o.attr &&
24
- self.value == o.value
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 :attr, :value
7
+ attr_reader :obj
7
8
 
8
- # @param value
9
- # @param attr Context attribute's name
10
- def initialize(value, attr)
11
- @value = value
12
- @attr = attr
13
- end
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
- attr == o.attr &&
22
- value == o.value
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