Sha256: 81534a1bf2a91c06e4495fedc3746f4164c2fb918fdbe62c4c548a35899635e4

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

module EmberCLI
  SKIP_CAPTURE = ["", ""].freeze

  class Capture
    def initialize(sprockets:, &block)
      @sprockets = sprockets
      @block = block
    end

    def capture
      if block.present?
        capture_content
      else
        SKIP_CAPTURE
      end
    end

    private

    attr_reader :block, :sprockets

    def capture_content
      if block.arity > 0
        block.call(*block_arguments)
      end

      [head.content, body.content]
    end

    def block_arguments
      [head, body].first(block.arity)
    end

    def body
      @body ||= Block.new(sprockets)
    end

    def head
      @head ||= begin
        if block.arity < 1
          BlockWithoutArguments.new(sprockets, &block)
        else
          Block.new(sprockets)
        end
      end
    end

    class BlockWithoutArguments
      def initialize(sprockets, &block)
        @sprockets = sprockets
        @block = block
      end

      def content
        @sprockets.with_output_buffer(&@block)
      end
    end
    private_constant :BlockWithoutArguments

    class Block
      def initialize(sprockets)
        @sprockets = sprockets
        @content = []
      end

      def append(&block)
        @content.push(@sprockets.with_output_buffer(&block))
        nil
      end

      def content
        @content.join
      end
    end
    private_constant :Block
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ember-cli-rails-0.4.1 lib/ember-cli/capture.rb