Sha256: a5882a923dec0b3a69ebd6a3cbfad3c57c26adb7542e92908e234b6bab89a608

Contents?: true

Size: 1.11 KB

Versions: 10

Compression:

Stored size: 1.11 KB

Contents

module Trestle
  class Toolbar
    require_relative "toolbar/builder"
    require_relative "toolbar/context"
    require_relative "toolbar/item"
    require_relative "toolbar/menu"

    def initialize(builder=Builder)
      @builder = builder
      clear!
    end

    def clear!
      @blocks = []
    end

    def groups(template, *args)
      Enumerator.new do |y|
        @blocks.each do |block|
          builder = @builder.new(template, *args)
          block.evaluate(builder, template, y, *args)
        end
      end
    end

    def append(&block)
      @blocks.push(Block.new(&block))
    end

    def prepend(&block)
      @blocks.unshift(Block.new(&block))
    end

    # Wraps a toolbar block to provide evaluation within the context of a template and enumerator
    class Block
      def initialize(&block)
        @block = block
      end

      def evaluate(builder, template, enumerator, *args)
        context = Context.new(builder, enumerator, *args)
        result = template.capture { template.instance_exec(context, *args, &@block) }
        enumerator << [result] if result.present?
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
trestle-0.10.1 lib/trestle/toolbar.rb
trestle-0.10.0 lib/trestle/toolbar.rb
trestle-0.10.0.pre2 lib/trestle/toolbar.rb
trestle-0.10.0.pre lib/trestle/toolbar.rb
trestle-0.9.8 lib/trestle/toolbar.rb
trestle-0.9.7 lib/trestle/toolbar.rb
trestle-0.9.6 lib/trestle/toolbar.rb
trestle-0.9.5 lib/trestle/toolbar.rb
trestle-0.9.4 lib/trestle/toolbar.rb
trestle-0.9.3 lib/trestle/toolbar.rb