lib/aversion.rb in txus-aversion-0.0.1 vs lib/aversion.rb in txus-aversion-0.0.2

- old
+ new

@@ -48,25 +48,25 @@ # Public: Initializes a mutable instance. def self.new_mutable(*args) allocate.tap do |instance| instance.send :initialize, *args instance.instance_eval do - @transformations = [] - @initial_args = args + self.history = [] + @__initial_args__ = args end end end end end # Public: Returns a mutable version of the object, in case anyone needs it. We # do need it internally to perform transformations. def mutable - self.class.new_mutable(*@initial_args).tap do |mutable| + self.class.new_mutable(*initial_args).tap do |mutable| instance_variables.each do |ivar| mutable.instance_variable_set(ivar, instance_variable_get(ivar)) - mutable.instance_variable_set(:@transformations, @transformations.dup) + mutable.history = history.dup end end end # Public: The only way to transform state. @@ -80,11 +80,11 @@ # Public: Rolls back to a previous version of the state. # # Returns a new, immutable copy with the previous state. def rollback - self.class.new_mutable(*@initial_args).tap do |instance| + self.class.new_mutable(*initial_args).tap do |instance| instance.replay(history[0..-2]) end.freeze end # Public: Replays an array of transformations (procs). @@ -101,17 +101,17 @@ end.freeze end # Internal: Returns the history of this object. def history - @transformations + @__transformations__ end # Internal: Sets the history of this object to a specific array fo # transformations. def history=(transformations) - @transformations = transformations + @__transformations__ = transformations end # Public: Returns the difference between two versioned objects, which is an # array of the transformations one lacks from the other. def -(other) @@ -123,9 +123,14 @@ # Public: Returns whether two versionable objects are equal. # # They will be equal as long as they have the same initial args with they were # constructed with and their history is the same. def ==(other) - @initial_args == other.instance_variable_get(:@initial_args) && - history == other.history + initial_args == other.initial_args && history == other.history + end + + # Public: Exposes the initial arguments passed to the constructor, for + # comparison purposes. + def initial_args + @__initial_args__ end end