Module: Stamina::Markable

Included in:
Automaton, Automaton::Edge, Automaton::State
Defined in:
lib/stamina-core/stamina/markable.rb

Overview

Allows any object to be markable with user-data.

This module is expected to be included by classes that want to implement the Markable design pattern. Moreover, if the instances of the including class respond to state_changed, this method is automatically invoked when marks change. This method is used by automaton in order to make it possible to track changes and check modified automata for consistency.

Detailed API

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object

Returns user-value associated to key, nil if no such key in user-data.



17
18
19
# File 'lib/stamina-core/stamina/markable.rb', line 17

def [](key)
  @data[key]
end

#[]=(key, value) ⇒ Object

Associates value to key in user-data. Overrides previous value if present.



25
26
27
28
29
# File 'lib/stamina-core/stamina/markable.rb', line 25

def []=(key,value)
  oldvalue = @data[key]
  @data[key] = value
  state_changed(:loaded_pair, [key,oldvalue,value]) if self.respond_to? :state_changed
end

#dataObject

Extracts the copy of attributes which can subsequently be modified.



50
51
52
# File 'lib/stamina-core/stamina/markable.rb', line 50

def data
  @data.nil? ? {} : @data.dup
end

#marks(*keys) ⇒ Object

Returns the values mapped to ‘keys` as an array



39
40
41
# File 'lib/stamina-core/stamina/markable.rb', line 39

def marks(*keys)
  raw_data.values_at(*keys)
end

#raw_dataObject

Returns RAW data, that is without duplicating it. Returns result should not be changed.



45
46
47
# File 'lib/stamina-core/stamina/markable.rb', line 45

def raw_data
  @data ||= {}
end

#remove_mark(key) ⇒ Object

Removes a mark



32
33
34
35
36
# File 'lib/stamina-core/stamina/markable.rb', line 32

def remove_mark(key)
  oldvalue = @data[key]
  @data.delete(key)
  state_changed(:loaded_pair, [key,oldvalue,nil]) if self.respond_to? :state_changed
end