ruy 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
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 The attribute's value.
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 Attribute's name
29
- # @param value Attribute's 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 or fallback depending on whether
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] context
37
+ # @param context [Hash]
38
38
  #
39
- # @return [Object] outcome or fallback
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
- # TODO document
52
- def fallback(value)
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
- # The value will be resolved upon the context during evaluation. Let is lazy evaluated, it is
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
- # TODO document
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?
@@ -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] pattern String representing a Ruy's
12
+ # @param pattern [String] String representing a Ruy's
13
13
  # well-formed timestamp pattern
14
- # @param [String] tz_identifier String representing IANA's
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
- # representing the current TimePattern object.
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
- # Overrides Ruby's method missing call to redirect calls
108
- # to the stored Time object in case it responds to the missing method.
109
- # Will call to super in case it doesn't.
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
@@ -7,7 +7,7 @@ module Ruy
7
7
  # @example
8
8
  # simple_module_name(Net::HTTP) # => 'HTTP'
9
9
  #
10
- # @param [Module] mod
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] s
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] rule
43
+ # @param rule [Ruy::Rule]
44
44
  #
45
45
  # @return [String]
46
46
  def self.rule_name(rule)
@@ -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>] conditions
10
- # @param [Hash] ctx
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] ctx
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: 1.0.0
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: 2014-07-22 00:00:00.000000000 Z
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.4.5
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