Class: KhoinguyenEhTodoList::Memento

Inherits:
Object
  • Object
show all
Defined in:
lib/khoinguyen_eh_todo_list/memento.rb

Overview

Represents a memento for the undo and redo history

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, data) ⇒ Memento

Returns a new instance of Memento.

Parameters:

  • action (Symbol) —

    The type of action (e.g., :add, :remove)

  • data (Hash) —

    The details of the change



12
13
14
15
# File 'lib/khoinguyen_eh_todo_list/memento.rb', line 12

def initialize(action, data)
  @action = action
  @data = data
end

Instance Attribute Details

#action ⇒ Object (readonly)

Returns the value of attribute action.



8
9
10
# File 'lib/khoinguyen_eh_todo_list/memento.rb', line 8

def action
  @action
end

#data ⇒ Object (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/khoinguyen_eh_todo_list/memento.rb', line 8

def data
  @data
end

Class Method Details

.from_yaml(yaml) ⇒ Memento

Deserializes memento from YAML

Parameters:

  • yaml (String)

Returns:



26
27
28
29
# File 'lib/khoinguyen_eh_todo_list/memento.rb', line 26

def self.from_yaml(yaml)
  state = YAML.safe_load(yaml)
  new(state[:action], state[:data])
end

Instance Method Details

#to_yaml ⇒ String

Serializes the memento to YAML

Returns:

  • (String)


19
20
21
# File 'lib/khoinguyen_eh_todo_list/memento.rb', line 19

def to_yaml
  YAML.dump({ action: @action, data: @data })
end