Permalink
Browse files

Added the employee example with Madeleine storage

  • Loading branch information...
1 parent 8ee1009 commit f61d0ca96940dd0df55bce94962804f635442998 @adomokos committed Dec 3, 2009
View
@@ -0,0 +1 @@
+Command/employees/*
View
@@ -0,0 +1,10 @@
+class AddEmployee
+ def initialize(employee)
+ @employee = employee
+ end
+
+ def execute(system)
+ system.add_employee(@employee)
+ end
+end
+
View
@@ -0,0 +1,11 @@
+class ChangeAddress
+ def initialize(number, address)
+ @number = number
+ @address = address
+ end
+
+ def execute(system)
+ system.change_address(@number, @address)
+ end
+end
+
@@ -4,18 +4,23 @@ class CompositeCommand < Command
def initialize
@commands = []
end
-
+
def add_command(command)
@commands << command
end
-
+
def execute
@commands.each{|cmd| cmd.execute }
end
-
+
+ def unexecute
+ @commands.reverse.each{ |cmd| cmd.unexecute }
+ end
+
def description
description = ''
@commands.each{ |cmd| description += cmd.description + "\n" }
description
end
-end
+end
+
View
@@ -1,11 +1,18 @@
+require 'fileutils'
+
class CopyFile < Command
def initialize(source, target)
super("Copy file: #{source} to #{target}")
@source = source
@target = target
end
-
+
def execute
FileUtils.copy(@source, @target)
end
-end
+
+ def unexecute
+ File.delete(@target)
+ end
+end
+
View
@@ -4,10 +4,15 @@ def initialize(path, contents)
@path = path
@contents = contents
end
-
- def execute
+
+ def execute
f = File.open(@path, "w")
f.write(@contents)
f.close
end
-end
+
+ def unexecute
+ File.delete(@path)
+ end
+end
+
View
@@ -0,0 +1,10 @@
+class DeleteEmployee
+ def initialize(number)
+ @number = number
+ end
+
+ def execute(system)
+ @number = number
+ end
+end
+
View
@@ -3,8 +3,21 @@ def initialize(path)
super("Delete file: #{path}")
@path = path
end
-
+
def execute
- File.delete(@path)
+ if File.exists?(@path)
+ @contents = File.read(@path)
+ end
+ f = File.delete(@path)
end
-end
+
+ def unexecute
+ if @contents
+ puts @contents
+ f = File.open(@path, "w")
+ f.write(@contents)
+ f.close
+ end
+ end
+end
+
View
@@ -0,0 +1,17 @@
+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
+
@@ -0,0 +1,24 @@
+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
+
@@ -10,4 +10,8 @@
cmds.add_command(DeleteFile.new('file1.txt'))
cmds.execute
-puts cmds.description
+cmds.unexecute
+
+# ucomment this if you want to unexecute the commands
+# puts cmds.description
+
@@ -0,0 +1,31 @@
+require 'rubygems'
+require 'madeleine'
+require 'employee_manager'
+require 'employee'
+require 'add_employee'
+require 'find_employee'
+require 'change_address'
+
+store = SnapshotMadeleine.new('employees') {EmployeeManager.new}
+
+Thread.new do
+ while true
+ sleep(20)
+ madeleine.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))
+
+# with Tom and Harry safely in the Madeleine store, we can run some queries
+puts(store.execute_command(FindEmployee.new('1001')))
+puts(store.execute_command(FindEmployee.new('1002')))
+
+# we can even change Tom's address
+store.execute_command(ChangeAddress.new('1001', '555 Main Stree'))
+
View
@@ -0,0 +1,10 @@
+class FindEmployee
+ def initialize(number)
+ @number = number
+ end
+
+ def execute(system)
+ system.find_employee(@number)
+ end
+end
+

0 comments on commit f61d0ca

Please sign in to comment.