Class: Blocks::RuntimeContext

Inherits:
HashWithRenderStrategy show all
Defined in:
lib/blocks/renderers/runtime_context.rb

Constant Summary collapse

CONTROL_VARIABLES =
{
  wrap_all: [],
  wrap_each: [:outer_wrapper],
  wrap_with: [:wrap, :wrapper, :inner_wrapper],
  collection: [],
  as: []
}
PROTECTED_OPTIONS =
RENDERING_STRATEGIES +
(CONTROL_VARIABLES.keys + CONTROL_VARIABLES.values).flatten.compact

Constants inherited from HashWithRenderStrategy

HashWithRenderStrategy::RENDERING_STRATEGIES, HashWithRenderStrategy::RENDER_WITH_BLOCK, HashWithRenderStrategy::RENDER_WITH_PARTIAL, HashWithRenderStrategy::RENDER_WITH_PROXY

Instance Attribute Summary

Attributes inherited from HashWithRenderStrategy

#render_strategy

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HashWithRenderStrategy

#except, #extractable_options?, #initialize, #nested_under_indifferent_access, #render_strategy_and_item, #render_strategy_item, #renders_with_proxy?, #reverse_merge!, #slice, #to_s

Constructor Details

This class inherits a constructor from Blocks::HashWithRenderStrategy

Class Method Details

.build(builder, *runtime_args, &runtime_block) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/blocks/renderers/runtime_context.rb', line 36

def self.build(builder, *runtime_args, &runtime_block)
  new.tap do |runtime_context|
    runtime_context.builder = builder
    runtime_context.runtime_block = runtime_block
    runtime_context.compute(*runtime_args)
  end
end

Instance Method Details

#compute(*runtime_args) ⇒ Object

TODO: change the method signature of this method to def compute(block_identifier, options={}, &runtime_block)

Get rid of most uses of the *


46
47
48
49
50
51
52
# File 'lib/blocks/renderers/runtime_context.rb', line 46

def compute(*runtime_args)
  render_options = runtime_args.extract_options!
  build_block_context(runtime_args.shift, render_options)
  # TODO: runtime args should be specified as a reserved keyword in the hash
  self.runtime_args = runtime_args
  extract_control_options
end

#extend_from_definition(definition, options = {}, &runtime_block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/blocks/renderers/runtime_context.rb', line 54

def extend_from_definition(definition, options={}, &runtime_block)
  RuntimeContext.build(
    builder,
    definition,
    # TODO: don't pass runtime args here?
    *runtime_args,
    options.merge(parent_runtime_context: self),
    &runtime_block
  )
end

#hooks_for(hook_name) ⇒ Object



101
102
103
# File 'lib/blocks/renderers/runtime_context.rb', line 101

def hooks_for(hook_name)
  hooks[hook_name] if hooks.try(:key?, hook_name)
end

#hooks_or_wrappers_present?Boolean

TODO: this method needs to be rewritten to output a proper hash def to_s

description = []
if block_name
  block_name = self.block_name.to_s
  if block_name.include?(" ")
    block_name = ":\"#{block_name}\""
  else
    block_name = ":#{block_name}"
  end
  description << "Block Name: #{block_name}"
end

if render_item.is_a?(String)
  description << "Renders with partial \"#{render_item}\""
elsif render_item.is_a?(Proc)
  description << "Renders with block defined at #{render_item.source_location}"
elsif render_item.is_a?(Method)
  description << "Renders with method defined at #{render_item.source_location}"
end

CONTROL_VARIABLES.each do |control_variable, *|
  if value = send(control_variable)
    # description << "#{control_variable}: #{value} [#{callers[control_variable]}]"
  end
end

description << super
description.join("\n")

end

Returns:

  • (Boolean)


97
98
99
# File 'lib/blocks/renderers/runtime_context.rb', line 97

def hooks_or_wrappers_present?
  hooks.present? || wrap_all || wrap_each || wrap_with || collection.present?
end

#to_hashObject



105
106
107
108
109
110
111
112
# File 'lib/blocks/renderers/runtime_context.rb', line 105

def to_hash
  hash = super
  if collection_item_index
    object_name = as || :object
    hash.merge!(object_name => collection_item, current_index: collection_item_index)
  end
  hash
end