Skip to content

Instantly share code, notes, and snippets.

@ifyouseewendy
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ifyouseewendy/c0a3ec5da222779885f0 to your computer and use it in GitHub Desktop.
Save ifyouseewendy/c0a3ec5da222779885f0 to your computer and use it in GitHub Desktop.
example with Madeleine https://github.com/ghostganz/madeleine **Don't know how to restore from the snapshots yet**
require 'rubygems'
require 'madeleine'
class Employee
attr_accessor :name, :number, :address
def initialize(name, number, address)
@name = name
@number = number
@address = address
end
def to_s
"Employee: name: #{name} num: #{number} addr: #{address}"
end
end
class EmployeeManager
def initialize
@employees = {}
end
def add_employee(e)
@employees[e.number] = e
end
def change_address(number, address)
employee = @employees[number]
raise "No such employee" if not employee
employee.address = address
end
def delete_employee(number)
@employees.remove(number)
end
def find_employee(number)
@employees[number]
end
end
class AddEmployee
def initialize(employee)
@employee = employee
end
def execute(system)
system.add_employee(@employee)
end
end
class FindEmployee
def initialize(number)
@number = number
end
def execute(system)
system.find_employee(@number)
end
end
store = SnapshotMadeleine.new('employees') {EmployeeManager.new}
Thread.new do
while true
sleep(20)
store.take_snapshot
end
end
tom = Employee.new('tom','1001','1 Division Street')
harry = Employee.new('harry','1002','3435 Sunnyside Ave')
store.execute_command(AddEmployee.new(tom))
store.execute_command(AddEmployee.new(harry))
puts(store.execute_command(FindEmployee.new('1001')))
puts(store.execute_command(FindEmployee.new('1002')))
# Employee: name: tom num: 1001 addr: 1 Division Street
# Employee: name: harry num: 1002 addr: 3435 Sunnyside Ave
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment