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
data/lib/ruy/rule.rb
CHANGED
@@ -14,61 +14,67 @@ module Ruy
|
|
14
14
|
@outcomes = []
|
15
15
|
end
|
16
16
|
|
17
|
-
# Gets attribute's value from the given name
|
17
|
+
# Gets attribute's value from the given name
|
18
18
|
#
|
19
19
|
# @param name
|
20
20
|
#
|
21
|
-
# @return
|
21
|
+
# @return the attribute's value
|
22
22
|
def get(name)
|
23
23
|
@attrs[name]
|
24
24
|
end
|
25
25
|
|
26
|
-
# Sets a custom attribute
|
26
|
+
# Sets a custom attribute
|
27
27
|
#
|
28
|
-
# @param name
|
29
|
-
# @param value
|
28
|
+
# @param name
|
29
|
+
# @param value
|
30
30
|
def set(name, value)
|
31
31
|
@attrs[name] = value
|
32
32
|
end
|
33
33
|
|
34
|
-
# Evaluates rule conditions return the corresponding outcome
|
35
|
-
# the conditions matched or not
|
34
|
+
# Evaluates that rule conditions return the corresponding outcome
|
35
|
+
# or fallback depending on whether the conditions matched or not
|
36
36
|
#
|
37
|
-
# @param [Hash]
|
37
|
+
# @param context [Hash]
|
38
38
|
#
|
39
|
-
# @return
|
39
|
+
# @return outcome or fallback values
|
40
40
|
def call(context)
|
41
41
|
ctx = Context.new(context, @lets)
|
42
42
|
|
43
43
|
if Ruy::Utils::Rules.evaluate_conditions(@conditions, ctx)
|
44
44
|
Ruy::Utils::Rules.compute_outcome(@outcomes, ctx)
|
45
|
-
|
46
45
|
else
|
47
46
|
@fallback
|
48
47
|
end
|
49
48
|
end
|
50
49
|
|
51
|
-
#
|
52
|
-
|
53
|
-
@fallback = value
|
54
|
-
end
|
55
|
-
|
56
|
-
# Defines a memoized value.
|
50
|
+
# Stores the name of a lazy variable whose block will be defined
|
51
|
+
# in the evaluated context
|
57
52
|
#
|
58
|
-
#
|
59
|
-
# not evaluated until the first condition referencing it is invoked. Once evaluated, its value
|
60
|
-
# is stored, so subsequent invocations during the same evaluation will resolve again its value.
|
53
|
+
# @param name [Symbol]
|
61
54
|
def let(name)
|
62
55
|
@lets << name
|
63
56
|
end
|
64
57
|
|
65
|
-
#
|
58
|
+
# Defines the fallback value to be returned when conditions are not met
|
59
|
+
#
|
60
|
+
# @param value
|
61
|
+
def fallback(value)
|
62
|
+
@fallback = value
|
63
|
+
end
|
64
|
+
|
65
|
+
# Defines the outcome value to be returned when conditions are met
|
66
|
+
#
|
67
|
+
# @param value
|
68
|
+
# @yield value
|
66
69
|
def outcome(value, &block)
|
67
70
|
outcome = Outcome.new(value)
|
68
71
|
outcome.instance_exec(&block) if block_given?
|
69
72
|
@outcomes << outcome
|
70
73
|
end
|
71
74
|
|
75
|
+
# Outputs a DSL-compliant description of the rule
|
76
|
+
#
|
77
|
+
# @return [String]
|
72
78
|
def to_s
|
73
79
|
s = @attrs.map { |name, value| "set #{name.inspect}, #{value.inspect}" }.join("\n")
|
74
80
|
s << "\n\n" if @attrs.any?
|
data/lib/ruy/time_pattern.rb
CHANGED
@@ -9,9 +9,9 @@ module Ruy
|
|
9
9
|
attr_reader :year, :month, :day, :hour, :min, :sec, :time_zone, :tz, :local,
|
10
10
|
:utc, :utc_offset
|
11
11
|
|
12
|
-
# @param [String]
|
12
|
+
# @param pattern [String] String representing a Ruy's
|
13
13
|
# well-formed timestamp pattern
|
14
|
-
# @param [String]
|
14
|
+
# @param tz_identifier [String] String representing IANA's
|
15
15
|
# time zone identifier. Defaults to UTC if none passed.
|
16
16
|
def initialize(pattern, tz_identifier = 'UTC')
|
17
17
|
unless match_data = pattern.match(WELL_FORMED_TS_EXP)
|
@@ -39,12 +39,6 @@ module Ruy
|
|
39
39
|
@utc_offset = @tz.current_period.utc_total_offset
|
40
40
|
end
|
41
41
|
|
42
|
-
# Implements Ruby's spaceship operator to work with Time objects.
|
43
|
-
# Uses Object's spaceship operator if passed object
|
44
|
-
# does not respond to #to_time.
|
45
|
-
#
|
46
|
-
# @param [#to_time]
|
47
|
-
# @return [Fixnum]
|
48
42
|
def <=>(o)
|
49
43
|
if o.respond_to?(:to_time)
|
50
44
|
time = o.to_time
|
@@ -67,12 +61,6 @@ module Ruy
|
|
67
61
|
end
|
68
62
|
end
|
69
63
|
|
70
|
-
# Overrides Object equal operator and uses Comparable equality
|
71
|
-
# for Time objects.
|
72
|
-
# Uses identity comparison if passed object does not respond to #to_time.
|
73
|
-
#
|
74
|
-
# @param [#to_time]
|
75
|
-
# @return [Boolean]
|
76
64
|
def ==(o)
|
77
65
|
if o.is_a?(self.class)
|
78
66
|
return year == o.year &&
|
@@ -90,25 +78,20 @@ module Ruy
|
|
90
78
|
end
|
91
79
|
|
92
80
|
# Returns a well-formed Ruy timestamp with IANA time zone identifier
|
93
|
-
#
|
81
|
+
# representing the current TimePattern object.
|
94
82
|
#
|
95
|
-
# @return [String]
|
96
83
|
def to_s
|
97
84
|
@pattern
|
98
85
|
end
|
99
86
|
|
100
|
-
# Returns a representation of the time pattern
|
101
|
-
#
|
102
|
-
# @return [String]
|
103
87
|
def inspect
|
104
88
|
@pattern.inspect
|
105
89
|
end
|
106
90
|
|
107
|
-
|
108
|
-
|
109
|
-
#
|
91
|
+
protected
|
92
|
+
|
93
|
+
# Redirects missing methods to the UTC Time object stored in the instance
|
110
94
|
#
|
111
|
-
# @param (see BasicObject#method_missing)
|
112
95
|
def method_missing(method, *args)
|
113
96
|
@utc.respond_to?(method) ? @utc.send(method, *args) : super
|
114
97
|
end
|
data/lib/ruy/utils/naming.rb
CHANGED
@@ -7,7 +7,7 @@ module Ruy
|
|
7
7
|
# @example
|
8
8
|
# simple_module_name(Net::HTTP) # => 'HTTP'
|
9
9
|
#
|
10
|
-
# @param [Module]
|
10
|
+
# @param mod [Module]
|
11
11
|
#
|
12
12
|
# @return [String]
|
13
13
|
# @return [nil] if given module is an anonymous one.
|
@@ -26,7 +26,7 @@ module Ruy
|
|
26
26
|
# - all caps
|
27
27
|
# - camel case mixed with all caps
|
28
28
|
#
|
29
|
-
# @param [String]
|
29
|
+
# @param s [String]
|
30
30
|
#
|
31
31
|
# @return [String]
|
32
32
|
def self.snakecase(s)
|
@@ -40,7 +40,7 @@ module Ruy
|
|
40
40
|
# @example
|
41
41
|
# rule_name(Ruy::Eq.new) # => 'eq'
|
42
42
|
#
|
43
|
-
# @param [Ruy::Rule]
|
43
|
+
# @param rule [Ruy::Rule]
|
44
44
|
#
|
45
45
|
# @return [String]
|
46
46
|
def self.rule_name(rule)
|
data/lib/ruy/utils/rules.rb
CHANGED
@@ -6,8 +6,8 @@ module Ruy
|
|
6
6
|
#
|
7
7
|
# Returns true when all the conditions match, false otherwise.
|
8
8
|
#
|
9
|
-
# @param [Array<Condition>]
|
10
|
-
# @param [
|
9
|
+
# @param conditions [Array<Condition>]
|
10
|
+
# @param ctx [Context]
|
11
11
|
#
|
12
12
|
# @return [Boolean]
|
13
13
|
def self.evaluate_conditions(conditions, ctx)
|
@@ -20,7 +20,7 @@ module Ruy
|
|
20
20
|
|
21
21
|
# Returns the value of the first outcome that matches
|
22
22
|
#
|
23
|
-
# @param [Ruy::Context]
|
23
|
+
# @param ctx [Ruy::Context]
|
24
24
|
#
|
25
25
|
# @return [Object] The value of the first matching outcome
|
26
26
|
# @return [nil] when no outcome matches
|
metadata
CHANGED
@@ -1,41 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Moove-IT
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-07-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: tzinfo
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '3.1'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '3.1'
|
41
41
|
- !ruby/object:Gem::Dependency
|
@@ -71,16 +71,18 @@ files:
|
|
71
71
|
- lib/ruy/conditions/cond.rb
|
72
72
|
- lib/ruy/conditions/condition.rb
|
73
73
|
- lib/ruy/conditions/day_of_week.rb
|
74
|
+
- lib/ruy/conditions/dig.rb
|
74
75
|
- lib/ruy/conditions/eq.rb
|
76
|
+
- lib/ruy/conditions/every.rb
|
75
77
|
- lib/ruy/conditions/except.rb
|
76
78
|
- lib/ruy/conditions/greater_than.rb
|
77
79
|
- lib/ruy/conditions/greater_than_or_equal.rb
|
78
80
|
- lib/ruy/conditions/in.rb
|
79
81
|
- lib/ruy/conditions/in_cyclic_order.rb
|
80
82
|
- lib/ruy/conditions/include.rb
|
81
|
-
- lib/ruy/conditions/included.rb
|
82
83
|
- lib/ruy/conditions/less_than.rb
|
83
84
|
- lib/ruy/conditions/less_than_or_equal.rb
|
85
|
+
- lib/ruy/conditions/some.rb
|
84
86
|
- lib/ruy/conditions/tz.rb
|
85
87
|
- lib/ruy/context.rb
|
86
88
|
- lib/ruy/dsl.rb
|
@@ -101,17 +103,17 @@ require_paths:
|
|
101
103
|
- lib
|
102
104
|
required_ruby_version: !ruby/object:Gem::Requirement
|
103
105
|
requirements:
|
104
|
-
- -
|
106
|
+
- - ! '>='
|
105
107
|
- !ruby/object:Gem::Version
|
106
108
|
version: '0'
|
107
109
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
110
|
requirements:
|
109
|
-
- -
|
111
|
+
- - ! '>='
|
110
112
|
- !ruby/object:Gem::Version
|
111
113
|
version: '0'
|
112
114
|
requirements: []
|
113
115
|
rubyforge_project:
|
114
|
-
rubygems_version: 2.
|
116
|
+
rubygems_version: 2.6.2
|
115
117
|
signing_key:
|
116
118
|
specification_version: 4
|
117
119
|
summary: Rules Engine for Ruby
|
@@ -1,28 +0,0 @@
|
|
1
|
-
module Ruy
|
2
|
-
module Conditions
|
3
|
-
|
4
|
-
# Expects that a value is included in a set of values from the context attribute.
|
5
|
-
class Included < Condition
|
6
|
-
attr_reader :attr, :value
|
7
|
-
|
8
|
-
# @param attr Context attribute's name
|
9
|
-
# @param value Expected set of values
|
10
|
-
# @yield a block in the context of the current rule
|
11
|
-
def initialize(attr, value, &block)
|
12
|
-
super
|
13
|
-
@attr = attr
|
14
|
-
@value = value
|
15
|
-
end
|
16
|
-
|
17
|
-
def call(ctx)
|
18
|
-
ctx.resolve(self.attr).include?(self.value)
|
19
|
-
end
|
20
|
-
|
21
|
-
def ==(o)
|
22
|
-
o.kind_of?(Included) &&
|
23
|
-
self.attr == o.attr &&
|
24
|
-
self.value == o.value
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|