Sha256: 706bdf04ad6ec02fa5e48173e44aef6cd14b742d7b5e79f1260b509f1ad20550

Contents?: true

Size: 892 Bytes

Versions: 2

Compression:

Stored size: 892 Bytes

Contents

module Shapeshifter
  class ShiftChain
    def initialize(first)
      @shifters = [first]
    end

    def chain(shifter)
      shifters << shifter
      self
    end

    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

    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

    private

    attr_reader :shifters
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shapeshifter-0.0.2 lib/shapeshifter/shift_chain.rb
shapeshifter-0.0.1 lib/shapeshifter/shift_chain.rb