Module: Stamina::Markable
- Included in:
- Automaton, Automaton::Edge, Automaton::State
- Defined in:
- lib/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
-
#[](key) ⇒ Object
Returns user-value associated to key, nil if no such key in user-data.
-
#[]=(key, value) ⇒ Object
Associates value to key in user-data.
-
#data ⇒ Object
Extracts the copy of attributes which can subsequently be modified.
-
#remove_mark(key) ⇒ Object
Removes a mark.
Instance Method Details
#[](key) ⇒ Object
Returns user-value associated to key, nil if no such key in user-data.
17 |
# File 'lib/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.
23 24 25 26 27 |
# File 'lib/stamina/markable.rb', line 23 def []=(key,value) oldvalue = @data[key] @data[key] = value state_changed(:loaded_pair, [key,oldvalue,value]) if self.respond_to? :state_changed end |
#data ⇒ Object
Extracts the copy of attributes which can subsequently be modified.
37 38 39 |
# File 'lib/stamina/markable.rb', line 37 def data @data.nil? ? {} : @data.dup end |
#remove_mark(key) ⇒ Object
Removes a mark
30 31 32 33 34 |
# File 'lib/stamina/markable.rb', line 30 def remove_mark(key) oldvalue = @data[key] @data.delete(key) state_changed(:loaded_pair, [key,oldvalue,nil]) if self.respond_to? :state_changed end |