Sha256: efaf647f3ecd454bbbbc7cf106160eeb92c0209ddef83f3119ecb5d389aec17f

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

require 'active_support/core_ext/class/attribute'
require 'lou/transformer/step'

module Lou
  module Transformer
    # never raise this...
    class NeverError < StandardError; end

    def self.extended(base)
      base.class_eval do
        class_attribute(:steps)
        self.steps = []
        class_attribute(:error_class)
        self.error_class = Lou::Transformer::NeverError
      end
    end

    def reverse_on(error)
      self.error_class = error
    end

    def step
      Step.new.tap do |t|
        steps << t
      end
    end

    def apply(input, total_steps = steps.count)
      applied_steps = 0
      begin
        steps.last(total_steps).each do |t|
          input = t.apply(input)
          applied_steps += 1
        end
      rescue error_class => e
        reverse(input, applied_steps) if total_steps == steps.count
        raise e
      end
      input
    end

    def reverse(output, total_steps = steps.count)
      reversed_steps = 0
      begin
        steps.first(total_steps).reverse_each do |t|
          output = t.reverse(output)
          reversed_steps += 1
        end
      rescue error_class => e
        apply(output, reversed_steps) if total_steps == steps.count
        raise e
      end
      output
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lou-0.2.2 lib/lou/transformer.rb