Instantly share code, notes, and snippets.

Embed
What would you like to do?
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