Use Ruby's initialize_copy for clone/dup

Ruby provides a method for implementing deep-copy on
objects called initialize_copy. This gets called after
a clone or dup, and is the more idiomatic way to handle
the sort of thing being done in the previously-defined
clone method.
This commit is contained in:
Ernie Miller 2012-06-27 08:44:48 -04:00
parent 949bb42a4d
commit 129f2da60a
1 changed files with 6 additions and 7 deletions

View File

@ -3,11 +3,11 @@ module AASM
def self.[](clazz)
(@machines ||= {})[clazz.to_s]
end
def self.[]=(clazz, machine)
(@machines ||= {})[clazz.to_s] = machine
end
attr_accessor :states, :events, :initial_state, :config
attr_reader :name
@ -19,11 +19,10 @@ module AASM
@config = OpenStruct.new
end
def clone
klone = super
klone.states = states.clone
klone.events = events.clone
klone
def initialize_copy(orig)
super
@states = @states.dup
@events = @events.dup
end
def create_state(name, options)