Class: Xavier::Observer Private
- Inherits:
-
Object
- Object
- Xavier::Observer
- Defined in:
- lib/xavier/observer.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Observes an object for state mutations.
Instance Method Summary collapse
-
#initialize(mutator = Mutator.new, states = States.new) ⇒ Observer
constructor
private
Creates an instance of
Observer
. -
#observe(observable) { ... } ⇒ Integer
private
Observes an object, yields a block and then reverts the observed object’s class and instance variables.
Constructor Details
#initialize(mutator = Mutator.new, states = States.new) ⇒ Observer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates an instance of Observer
.
15 16 17 18 |
# File 'lib/xavier/observer.rb', line 15 def initialize(mutator = Mutator.new, states = States.new) @states = states @mutator = mutator end |
Instance Method Details
#observe(observable) { ... } ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Observes an object, yields a block and then reverts the observed object’s class and instance variables.
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/xavier/observer.rb', line 79 def observe(observable) raise ArgumentError, 'This method expects a block. Without a block it is useless.' unless block_given? raise AlreadyObserved, 'Objects can only be observed once per block.' if being_observed?(observable) strategies = mutator.mutation_strategies_for(observable) original_state = mutator.create_state_from(observable, strategies: strategies) save_state(original_state) yield mutator.apply_state(from: original_state, to: observable, strategies: strategies) delete_state(original_state) end |