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: []
}

Constants inherited from HashWithRenderStrategy

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

Instance Attribute Summary collapse

Attributes inherited from HashWithRenderStrategy

#render_strategy

Attributes inherited from HashWithCaller

#callers

Instance Method Summary collapse

Methods inherited from HashWithRenderStrategy

#add_options, #initialize_copy, #render_strategy_and_item, #reverse_merge

Methods inherited from HashWithCaller

#add_options, #nested_under_indifferent_access

Constructor Details

#initialize(builder, *runtime_args, &runtime_block) ⇒ RuntimeContext



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/blocks/renderers/runtime_context.rb', line 33

def initialize(builder, *runtime_args, &runtime_block)
  super(&nil)

  self.builder = builder
  self.runtime_block = runtime_block
  self.proxy_options_set = OptionsSet.new("Proxy Options Set")

  render_options = runtime_args.extract_options!
  block_name = runtime_args.shift

  convert_render_options(render_options)
  identify_block(block_name)

  self.runtime_args = runtime_args
  merge_options_and_identify_render_item
  extract_control_options
end

Instance Attribute Details

#block_nameObject

Returns the value of attribute block_name.



12
13
14
# File 'lib/blocks/renderers/runtime_context.rb', line 12

def block_name
  @block_name
end

#block_options_setObject

Returns the value of attribute block_options_set.



12
13
14
# File 'lib/blocks/renderers/runtime_context.rb', line 12

def block_options_set
  @block_options_set
end

#builderObject

Returns the value of attribute builder.



12
13
14
# File 'lib/blocks/renderers/runtime_context.rb', line 12

def builder
  @builder
end

#parent_runtime_contextObject

Returns the value of attribute parent_runtime_context.



12
13
14
# File 'lib/blocks/renderers/runtime_context.rb', line 12

def parent_runtime_context
  @parent_runtime_context
end

#proxy_options_setObject

Returns the value of attribute proxy_options_set.



12
13
14
# File 'lib/blocks/renderers/runtime_context.rb', line 12

def proxy_options_set
  @proxy_options_set
end

#render_itemObject

Returns the value of attribute render_item.



12
13
14
# File 'lib/blocks/renderers/runtime_context.rb', line 12

def render_item
  @render_item
end

#render_options_setObject

Returns the value of attribute render_options_set.



12
13
14
# File 'lib/blocks/renderers/runtime_context.rb', line 12

def render_options_set
  @render_options_set
end

#runtime_argsObject

Returns the value of attribute runtime_args.



12
13
14
# File 'lib/blocks/renderers/runtime_context.rb', line 12

def runtime_args
  @runtime_args
end

#runtime_blockObject

Returns the value of attribute runtime_block.



12
13
14
# File 'lib/blocks/renderers/runtime_context.rb', line 12

def runtime_block
  @runtime_block
end

Instance Method Details

#extend_to_block_definition(block_definition) ⇒ Object

TODO: this method needs to clone without context, i.e. with render_strategy, item, etc



52
53
54
55
56
# File 'lib/blocks/renderers/runtime_context.rb', line 52

def extend_to_block_definition(block_definition)
  RuntimeContext.new(builder, block_definition, parent_runtime_context: self).tap do |rc|
    rc.runtime_args = self.runtime_args
  end
end

#to_sObject

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/blocks/renderers/runtime_context.rb', line 59

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}"
  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