Class: Ilex::Context

Inherits:
Arbre::Context
  • Object
show all
Defined in:
lib/ilex/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, &blk) ⇒ Context

Returns a new instance of Context.



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

def initialize(component, &blk)
  @component = 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

  super({}, component, &blk)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



59
60
61
62
63
64
65
66
67
# File 'lib/ilex/context.rb', line 59

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

#component_wardensObject



27
28
29
# File 'lib/ilex/context.rb', line 27

def component_wardens
  @component_wardens ||= ComponentWardens.new(@component)
end

#contentObject



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

def content
  @component.send :content
end

#render(*args) ⇒ Object

This is overriding arbre::rails::rendering It performs the same actions, but returns html_safe on a passed block

Not 100% sure this is needed, but view_components won’t render their contents without it, when rendered in an arbre tree



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ilex/context.rb', line 36

def render(*args)
  rendered = helpers.render(*args) do
    if block_given?
      contents = yield
      if contents.is_a? Array
        contents.join.html_safe
      else
        contents.html_safe
      end
    end
  end
  case rendered
  when Arbre::Context
    current_arbre_element.add_child rendered
  else
    text_node rendered
  end
end

#respond_to_missing?(method, include_all) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/ilex/context.rb', line 55

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