Class: Cedar::Context

Inherits:
Arbre::Context
  • Object
show all
Defined in:
lib/cedar/context.rb

Overview

Inject a component’s instance variables into arbre’s default context as well as provide implicit handling of snake-cased component names as tags

Instance Method Summary collapse

Constructor Details

#initialize(component) ⇒ Context

Returns a new instance of Context.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cedar/context.rb', line 10

def initialize(component)
  super({}, component)

  @component = component
  @component_wardens = ComponentWardens.new(@component)

  # Copy all of the instance variables from the component to the context,
  # so we have access to them when rendering
  @component.instance_variables.each do |iv|
    instance_variable_set(iv, @component.instance_variable_get(iv))
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &content_block) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/cedar/context.rb', line 27

def method_missing(method, *args, &content_block)
  if @component.respond_to?(method)
    @component.send(method, *args, &content_block)
  elsif @component_wardens[method].exists?
    render @component_wardens[method].new(*args, &content_block)
  else
    super
  end
end

Instance Method Details

#respond_to_missing?(method, include_all) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/cedar/context.rb', line 23

def respond_to_missing?(method, include_all)
  @component.respond_to?(method) || @component_wardens[method].exists? || super
end