Class: Noteikumi::State

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(engine, logger) ⇒ State

Creates a new state

Parameters:

  • engine (Engine)
  • logger (Logger)


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

#engineEngine (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

Returns:



16
17
18
# File 'lib/noteikumi/state.rb', line 16

def engine
  @engine
end

#loggerLogger (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

Returns:

  • (Logger)


21
22
23
# File 'lib/noteikumi/state.rb', line 21

def logger
  @logger
end

#mutable=(value) ⇒ Boolean (writeonly)

Set the state mutable or not

Returns:

  • (Boolean)


25
26
27
# File 'lib/noteikumi/state.rb', line 25

def mutable=(value)
  @mutable = value
end

#processed_byArray<Rule> (readonly)

A list of rules that acted on this state

Returns:



11
12
13
# File 'lib/noteikumi/state.rb', line 11

def processed_by
  @processed_by
end

#resultsArray<Result> (readonly)

The list of result obtained from each rule

Returns:



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

Parameters:

  • item (Symbol)

    item to delete

  • value (Object)

    item to store

Returns:

  • (Object)

    the value set

Raises:

  • (StandardError)

    when the state is not mutable

  • (StandardError)

    when the item is already in 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

Parameters:

Returns:



82
83
84
# File 'lib/noteikumi/state.rb', line 82

def add_result(result)
  @results << result if result
end

#allow_mutationvoid

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

Parameters:

  • item (Symbol)

    item to delete

Raises:

  • (StandardError)

    when the state is not mutable



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

Yield Parameters:

  • result (Result)

    for every rule that ran



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

Parameters:

  • item (Symbol)

    the item name

Returns:

  • (Object, nil)

    the value stored



228
229
230
# File 'lib/noteikumi/state.rb', line 228

def get(item)
  @items[item]
end

#had_failures?Boolean

Checks all results for failures

Returns:

  • (Boolean)

    true when there were 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

Parameters:

  • type (Class)

    a ruby class to look for

Returns:

  • (Boolean)


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

Parameters:

  • item (Symbol)

    item name

Returns:

  • (Boolean)


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

Parameters:

  • type (Class)

    the type to search for

Returns:

  • (Hash)

    items found



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

Examples:


state[:one] = "hello world"

state.meets_requirements?(:one, String) => [true, "reason"]
state.meets_requirements?(nil, String) => [true, "reason"]
state.meets_requirements?(nil, Fixnum) => [false, "State has no items of class Fixnum"]
state.meets_requirements?(:one, Fixnum) => [false, "State item :one is not a Fixnum"]
state.meets_requirements?(:not_set, Fixnum) => [false, "State has no item not_set"]

Parameters:

  • requirement (Array<key,type>)

Returns:

  • (Array<Boolean,String>)


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

Returns:

  • (Boolean)


154
155
156
# File 'lib/noteikumi/state.rb', line 154

def mutable?
  !!@mutable
end

#prevent_mutationvoid

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

Returns:

  • (Result, nil)

    nil when the rule did not run



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

Parameters:

  • rule (Rule, Symbol)

    the rule name or a rule

Returns:

  • (Boolean)


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

Parameters:



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

Parameters:

  • item (Symbol)

    the name of the item being stored

  • value (Object)

    the item to store

Returns:

  • (Object)

    the item being stored

Raises:

  • (StandardError)

    when the state is not mutable



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