Class: Ruco::History
Instance Attribute Summary collapse
-
#position ⇒ Object
readonly
Returns the value of attribute position.
-
#stack ⇒ Object
readonly
Returns the value of attribute stack.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #add(state) ⇒ Object
-
#initialize(options) ⇒ History
constructor
A new instance of History.
- #redo ⇒ Object
- #state ⇒ Object
- #undo ⇒ Object
Constructor Details
#initialize(options) ⇒ History
Returns a new instance of History.
6 7 8 9 10 11 12 13 |
# File 'lib/ruco/history.rb', line 6 def initialize() @options = @options[:entries] ||= 100 @timeout = .delete(:timeout) || 0 @stack = [{:mutable => false, :created_at => 0, :type => :initial, :state => @options.delete(:state)}] @position = 0 end |
Instance Attribute Details
#position ⇒ Object (readonly)
Returns the value of attribute position.
4 5 6 |
# File 'lib/ruco/history.rb', line 4 def position @position end |
#stack ⇒ Object (readonly)
Returns the value of attribute stack.
4 5 6 |
# File 'lib/ruco/history.rb', line 4 def stack @stack end |
#timeout ⇒ Object
Returns the value of attribute timeout.
3 4 5 |
# File 'lib/ruco/history.rb', line 3 def timeout @timeout end |
Instance Method Details
#add(state) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/ruco/history.rb', line 19 def add(state) return unless tracked_field_changes?(state) remove_undone_states unless merge? state # can no longer modify previous states @stack[@position][:mutable] = false state_type = type(state) @position += 1 @stack[@position] = {:mutable => true, :type => state_type, :created_at => Time.now.to_f} end @stack[@position][:state] = state limit_stack end |
#redo ⇒ Object
38 39 40 |
# File 'lib/ruco/history.rb', line 38 def redo @position = [@position + 1, @stack.size - 1].min end |
#state ⇒ Object
15 16 17 |
# File 'lib/ruco/history.rb', line 15 def state @stack[@position][:state] end |
#undo ⇒ Object
34 35 36 |
# File 'lib/ruco/history.rb', line 34 def undo @position = [@position - 1, 0].max end |