Class: Noteikumi::State
- Inherits:
-
Object
- Object
- Noteikumi::State
- Defined in:
- lib/noteikumi/state.rb
Overview
The state a rule will process, this is not a state machine but rather can be thought of like a scope
Instance Attribute Summary collapse
-
#engine ⇒ Engine
readonly
private
The engine this state is associated with.
-
#logger ⇒ Logger
readonly
private
The logger used.
-
#mutable ⇒ Boolean
writeonly
Set the state mutable or not.
-
#processed_by ⇒ Array<Rule>
readonly
A list of rules that acted on this state.
-
#results ⇒ Array<Result>
readonly
The list of result obtained from each rule.
Instance Method Summary collapse
-
#add(item, value) ⇒ Object
Adds an item.
-
#add_result(result) ⇒ Result
Adds a result to the list of results.
-
#allow_mutation ⇒ void
Allow the state to be modified.
-
#delete(item) ⇒ Object
Deletes an item.
-
#each_result {|result| ... } ⇒ void
Yields each recorded result.
-
#get(item) ⇒ Object?
(also: #[])
Retrieves a item from the state.
-
#had_failures? ⇒ Boolean
Checks all results for failures.
-
#has_item_of_type?(type) ⇒ Boolean
Determines if any item in the State has a certain type.
-
#include?(item) ⇒ Boolean
(also: #has?)
Checks if a item is in the state.
-
#initialize(engine, logger) ⇒ State
constructor
Creates a new state.
-
#items_by_type(type) ⇒ Hash
Selects any item that has a certain ruby class type.
-
#meets_requirement?(requirement) ⇒ Array<Boolean,String>
Checks if a given requirement is matched by this state.
-
#mutable? ⇒ Boolean
Determines if the state can be mutated.
-
#prevent_mutation ⇒ void
Prevent the state from being modified.
-
#process_rule(rule) ⇒ Result?
Process a rule with the state.
-
#processed_by?(rule) ⇒ Boolean
Determines if a rule with a specific name acted on this state.
-
#record_rule(rule, result) ⇒ void
Records a rule having acted on this state.
-
#set(item, value) ⇒ Object
(also: #[]=)
sets the value of an item without checking if it’s already set.
Constructor Details
#initialize(engine, logger) ⇒ State
Creates a new state
32 33 34 35 36 37 38 39 |
# File 'lib/noteikumi/state.rb', line 32 def initialize(engine, logger) @items = {} @results = [] @processed_by = [] @engine = engine @logger = logger @mutable = true end |
Instance Attribute Details
#engine ⇒ Engine (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The engine this state is associated with
16 17 18 |
# File 'lib/noteikumi/state.rb', line 16 def engine @engine end |
#logger ⇒ Logger (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The logger used
21 22 23 |
# File 'lib/noteikumi/state.rb', line 21 def logger @logger end |
#mutable=(value) ⇒ Boolean (writeonly)
Set the state mutable or not
25 26 27 |
# File 'lib/noteikumi/state.rb', line 25 def mutable=(value) @mutable = value end |
#processed_by ⇒ Array<Rule> (readonly)
A list of rules that acted on this state
11 12 13 |
# File 'lib/noteikumi/state.rb', line 11 def processed_by @processed_by end |
#results ⇒ Array<Result> (readonly)
The list of result obtained from each rule
7 8 9 |
# File 'lib/noteikumi/state.rb', line 7 def results @results end |
Instance Method Details
#add(item, value) ⇒ Object
Adds an item
See #set for a version of this that does not error if the item is already on the state
217 218 219 220 221 222 |
# File 'lib/noteikumi/state.rb', line 217 def add(item, value) raise("State is not mustable") unless mutable? raise("Already have item %s" % item) if has?(item) set(item, value) end |
#add_result(result) ⇒ Result
Adds a result to the list of results
82 83 84 |
# File 'lib/noteikumi/state.rb', line 82 def add_result(result) @results << result if result end |
#allow_mutation ⇒ void
This method returns an undefined value.
Allow the state to be modified
44 45 46 |
# File 'lib/noteikumi/state.rb', line 44 def allow_mutation @mutable = true end |
#delete(item) ⇒ Object
Deletes an item
201 202 203 204 205 |
# File 'lib/noteikumi/state.rb', line 201 def delete(item) raise("State is not mustable") unless mutable? @items.delete(item) end |
#each_result {|result| ... } ⇒ void
This method returns an undefined value.
Yields each recorded result
72 73 74 75 76 |
# File 'lib/noteikumi/state.rb', line 72 def each_result @results.each do |result| yield(result) end end |
#get(item) ⇒ Object? Also known as: []
Retrieves a item from the state
228 229 230 |
# File 'lib/noteikumi/state.rb', line 228 def get(item) @items[item] end |
#had_failures? ⇒ Boolean
Checks all results for failures
89 90 91 |
# File 'lib/noteikumi/state.rb', line 89 def had_failures? @results.map(&:error?).include?(true) end |
#has_item_of_type?(type) ⇒ Boolean
Determines if any item in the State has a certain type
149 150 151 |
# File 'lib/noteikumi/state.rb', line 149 def has_item_of_type?(type) !items_by_type(type).empty? end |
#include?(item) ⇒ Boolean Also known as: has?
Checks if a item is in the state
179 180 181 |
# File 'lib/noteikumi/state.rb', line 179 def include?(item) @items.include?(item) end |
#items_by_type(type) ⇒ Hash
Selects any item that has a certain ruby class type
109 110 111 |
# File 'lib/noteikumi/state.rb', line 109 def items_by_type(type) @items.select {|_, v| v.is_a?(type)} || [] end |
#meets_requirement?(requirement) ⇒ Array<Boolean,String>
Checks if a given requirement is matched by this state
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/noteikumi/state.rb', line 127 def meets_requirement?(requirement) key, klass = requirement if key return([false, "State has no item %s" % key]) unless include?(key) unless self[key].is_a?(klass) return [false, "State item %s is not a %s" % [key, klass]] end end unless has_item_of_type?(klass) return [false, "State has no items of class %s" % klass] end [true, "Valid state found"] end |
#mutable? ⇒ Boolean
Determines if the state can be mutated
154 155 156 |
# File 'lib/noteikumi/state.rb', line 154 def mutable? !!@mutable end |
#prevent_mutation ⇒ void
This method returns an undefined value.
Prevent the state from being modified
51 52 53 |
# File 'lib/noteikumi/state.rb', line 51 def prevent_mutation @mutable = false end |
#process_rule(rule) ⇒ Result?
Process a rule with the state
58 59 60 61 62 63 64 65 66 |
# File 'lib/noteikumi/state.rb', line 58 def process_rule(rule) rule.concurrent_safe? ? prevent_mutation : allow_mutation result = rule.process(self) allow_mutation record_rule(rule, result) end |
#processed_by?(rule) ⇒ Boolean
Determines if a rule with a specific name acted on this state
97 98 99 100 101 102 103 |
# File 'lib/noteikumi/state.rb', line 97 def processed_by?(rule) if rule.is_a?(Rule) @processed_by.include?(rule) else @processed_by.map(&:name).include?(rule) end end |
#record_rule(rule, result) ⇒ void
This method returns an undefined value.
Records a rule having acted on this state
If the result is not nil the actor will be record and the result stored, else it’s a noop
166 167 168 169 170 171 172 173 |
# File 'lib/noteikumi/state.rb', line 166 def record_rule(rule, result) if result @processed_by << rule @results << result end result end |
#set(item, value) ⇒ Object Also known as: []=
sets the value of an item without checking if it’s already set
190 191 192 193 194 |
# File 'lib/noteikumi/state.rb', line 190 def set(item, value) raise("State is not mustable") unless mutable? @items[item] = value end |