Sha256: 35bf815b733d57e29d6b4f461d9b40863f460b147b1911c3c8447d1ee1532e0e

Contents?: true

Size: 902 Bytes

Versions: 1

Compression:

Stored size: 902 Bytes

Contents

module Stamp
  module Emitters
    class Composite
      include Enumerable

      attr_reader :emitters

      def initialize(emitters=[])
        @emitters = emitters
      end

      def format(target)
        # NOTE using #each to build string because benchmarking shows
        # that it's ~20% faster than .map.join('')
        result = ''
        emitters.each { |e| result << e.format(target).to_s }
        result
      end

      def <<(emitter)    
        Array(emitter).each { |e| emitters << e }
      end

      def each(&block)
        emitters.each(&block)
      end

      def -(others)
        emitters - Array(others)
      end

      # Replace each element as we iterate with the result of the given block.
      def replace_each!
        emitters.each_with_index do |emitter, index|
          emitters[index] = yield(emitter)
        end

        self
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stamp-0.6.0 lib/stamp/emitters/composite.rb