Sha256: 1fe516945fa8f8db2d12efe2bbc5434569bce64393ba51178b0377f5208770ae

Contents?: true

Size: 1.21 KB

Versions: 31

Compression:

Stored size: 1.21 KB

Contents

module RubiGen

  # Manifest captures the actions a generator performs.  Instantiate
  # a manifest with an optional target object, hammer it with actions,
  # then replay or rewind on the object of your choice.
  #
  # Example:
  #   manifest = Manifest.new { |m|
  #     m.make_directory '/foo'
  #     m.create_file '/foo/bar.txt'
  #   }
  #   manifest.replay(creator)
  #   manifest.rewind(destroyer)
  class Manifest
    attr_reader :target

    # Take a default action target.  Yield self if block given.
    def initialize(target = nil)
      @target, @actions = target, []
      yield self if block_given?
    end

    # Record an action.
    def method_missing(action, *args, &block)
      @actions << [action, args, block]
    end

    # Replay recorded actions.
    def replay(target = nil)
      send_actions(target || @target, @actions)
    end

    # Rewind recorded actions.
    def rewind(target = nil)
      send_actions(target || @target, @actions.reverse)
    end

    # Erase recorded actions.
    def erase
      @actions = []
    end

    private
      def send_actions(target, actions)
        actions.each do |method, args, block|
          target.send(method, *args, &block)
        end
      end
  end

end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
rubigen-1.5.8 lib/rubigen/manifest.rb
rubigen-1.5.7 lib/rubigen/manifest.rb
rubigen-1.5.6 lib/rubigen/manifest.rb
rubigen-1.5.5 lib/rubigen/manifest.rb
rubigen-1.5.4 lib/rubigen/manifest.rb
rubigen-1.5.3 lib/rubigen/manifest.rb
rubigen-1.0.4 lib/rubigen/manifest.rb
rubigen-1.0.0 lib/rubigen/manifest.rb
rubigen-1.0.3 lib/rubigen/manifest.rb
rubigen-1.0.1 lib/rubigen/manifest.rb
rubigen-1.0.2 lib/rubigen/manifest.rb
rubigen-1.2.2 lib/rubigen/manifest.rb
rubigen-1.2.1 lib/rubigen/manifest.rb
rubigen-1.1.0 lib/rubigen/manifest.rb
rubigen-1.2.4 lib/rubigen/manifest.rb
rubigen-1.3.0 lib/rubigen/manifest.rb
rubigen-1.2.0 lib/rubigen/manifest.rb
rubigen-1.3.2 lib/rubigen/manifest.rb
rubigen-1.3.1 lib/rubigen/manifest.rb
rubigen-1.3.3 lib/rubigen/manifest.rb