Class: Shapeshifter::ShiftChain

Inherits:
Object
  • Object
show all
Defined in:
lib/shapeshifter/shift_chain.rb

Instance Method Summary collapse

Constructor Details

#initialize(first) ⇒ ShiftChain

Returns a new instance of ShiftChain.



3
4
5
# File 'lib/shapeshifter/shift_chain.rb', line 3

def initialize(first)
  @shifters = [first]
end

Instance Method Details

#chain(shifter) ⇒ Object



7
8
9
10
# File 'lib/shapeshifter/shift_chain.rb', line 7

def chain(shifter)
  shifters << shifter
  self
end

#revert(source_object, target_object) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/shapeshifter/shift_chain.rb', line 22

def revert(source_object, target_object)
  can_be_dupped = source_object.respond_to?(:dup)
  shifters.reverse.each do |shifter_class|
    so = can_be_dupped ? source_object.dup : source_object
    shifter = shifter_class.new(so)
    target_object = shifter.revert(target_object)
  end
  target_object
end

#shift(source_object, target_object) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/shapeshifter/shift_chain.rb', line 12

def shift(source_object, target_object)
  can_be_dupped = source_object.respond_to?(:dup)
  shifters.each do |shifter_class|
    so = can_be_dupped ? source_object.dup : source_object
    shifter = shifter_class.new(so)
    target_object = shifter.shift(target_object)
  end
  target_object
end