Class: Philiprehberger::StateMachine::History
- Inherits:
-
Object
- Object
- Philiprehberger::StateMachine::History
- Defined in:
- lib/philiprehberger/state_machine/history.rb
Overview
Tracks state history with timestamps.
Defined Under Namespace
Classes: Entry
Instance Attribute Summary collapse
-
#max_size ⇒ Object
readonly
Returns the value of attribute max_size.
Instance Method Summary collapse
-
#entries ⇒ Array<Hash>
Return all history entries as an array of hashes.
-
#initialize(initial_state, max_size: 100) ⇒ History
constructor
A new instance of History.
-
#previous_state ⇒ Symbol?
Return the previous state (before the current one).
-
#record(state) ⇒ Object
Record a new state entry.
-
#size ⇒ Integer
Number of recorded entries.
Constructor Details
Instance Attribute Details
#max_size ⇒ Object (readonly)
Returns the value of attribute max_size.
9 10 11 |
# File 'lib/philiprehberger/state_machine/history.rb', line 9 def max_size @max_size end |
Instance Method Details
#entries ⇒ Array<Hash>
Return all history entries as an array of hashes.
29 30 31 |
# File 'lib/philiprehberger/state_machine/history.rb', line 29 def entries @entries.map { |e| { state: e.state, entered_at: e.entered_at } } end |
#previous_state ⇒ Symbol?
Return the previous state (before the current one).
36 37 38 39 40 |
# File 'lib/philiprehberger/state_machine/history.rb', line 36 def previous_state return nil if @entries.size < 2 @entries[-2].state end |
#record(state) ⇒ Object
Record a new state entry.
21 22 23 24 |
# File 'lib/philiprehberger/state_machine/history.rb', line 21 def record(state) @entries << Entry.new(state: state, entered_at: Time.now) @entries.shift if @entries.size > @max_size end |
#size ⇒ Integer
Number of recorded entries.
45 46 47 |
# File 'lib/philiprehberger/state_machine/history.rb', line 45 def size @entries.size end |