Sha256: 8cb9d765e9032af651f30ed56337d69bb87b8d332b35eb49483b0da5185c36dd

Contents?: true

Size: 1.89 KB

Versions: 13

Compression:

Stored size: 1.89 KB

Contents

module Blocks
  class BlockDefinition < OptionsSet
    attr_accessor :options_set,
                  :skip_content,
                  :skip_completely,
                  :anonymous,
                  *HookDefinition::HOOKS.map {|hook| "#{hook}_hooks" }


    def skip(completely=false)
      self.skip_content = true
      self.skip_completely = completely
    end

    def skip_content?
      !!skip_content
    end

    def skip_completely?
      !!skip_completely
    end

    def hooks_for(hook_name)
      self.send("#{hook_name}_hooks")
    end

    HookDefinition::HOOKS.each do |hook|
      class_eval <<-RUBY, __FILE__, __LINE__ + 1
        def #{hook}_hooks
          @#{hook}_hooks ||= []
        end
      RUBY

      define_method(hook) do |*args, &block|
        HookDefinition.new(self, hook, *args, &block).tap do |definition|
          hooks_for(hook) << definition
        end
      end
    end

    def to_s
      description = []
      description << super
      options = [
        runtime_options,
        standard_options,
        default_options
      ].detect(&:render_strategy)

      strategy = options.try(:render_strategy)
      render_strategy_name = if strategy == HashWithRenderStrategy::RENDER_WITH_PROXY
        caller_id = options.callers[HashWithRenderStrategy::RENDER_WITH_PROXY]
        "proxy block \"#{options[strategy]}\""
      elsif strategy == HashWithRenderStrategy::RENDER_WITH_BLOCK
        caller_id = options.callers[HashWithRenderStrategy::RENDER_WITH_BLOCK]
        "block defined at #{options[strategy].source_location}"
      elsif strategy == HashWithRenderStrategy::RENDER_WITH_PARTIAL
        caller_id = options.callers[HashWithRenderStrategy::RENDER_WITH_PARTIAL]
        "partial \"#{options[strategy]}\""
      end
      if render_strategy_name
        description << "Renders with #{render_strategy_name} [#{caller_id}]"
      end

      description.join("\n")
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
blocks-3.0.4 lib/blocks/builders/block_definition.rb
blocks-3.0.3 lib/blocks/builders/block_definition.rb
blocks-3.0.2 lib/blocks/builders/block_definition.rb
blocks-3.0.1 lib/blocks/builders/block_definition.rb
blocks-3.0.0 lib/blocks/builders/block_definition.rb
blocks-3.0.0.rc9 lib/blocks/builders/block_definition.rb
blocks-3.0.0.rc8 lib/blocks/builders/block_definition.rb
blocks-3.0.0.rc7 lib/blocks/builders/block_definition.rb
blocks-3.0.0.rc6 lib/blocks/builders/block_definition.rb
blocks-3.0.0.rc5 lib/blocks/builders/block_definition.rb
blocks-3.0.0.rc4 lib/blocks/builders/block_definition.rb
blocks-3.0.0.rc3 lib/blocks/builders/block_definition.rb
blocks-3.0.0.rc2 lib/blocks/builders/block_definition.rb