Class: Arbre::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/arbre/patches.rb

Defined Under Namespace

Modules: BuilderMethods

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Implements the method lookup chain. When you call a method that doesn’t exist, we:

1. Try to call the method on the current DOM context
2. Return an assigned variable of the same name
3. Call the method on the helper object
4. Call super


46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/arbre/patches.rb', line 46

def method_missing(name, *args, &block)
  if current_arbre_element.respond_to?(name)
    current_arbre_element.send name, *args, &block
  elsif assigns && assigns.has_key?(name)
    assigns[name]
  elsif helpers.respond_to?(name)
    helper_method(name, *args, &block)
  elsif route_proxy = possible_route_proxy(name)
    route_proxy.send(name, *args, &block)
  else
    super
  end
end

Instance Method Details

#helper_method(name, *args, &block) ⇒ Object

In order to not pollute our templates with helpers. prefixed everywhere we want to try to distinguish helpers that are almost always used as parameters to other methods such as path helpers and not add them as elements



64
65
66
67
68
69
70
71
72
# File 'lib/arbre/patches.rb', line 64

def helper_method(name, *args, &block)
  if name.match /_path$/
    helpers.send(name, *args, &block)
  elsif (const_get([name, 'engine'].join('/').classify) rescue nil)
    helpers.send(name, *args, &block)
  else
    current_arbre_element.add_child helpers.send(name, *args, &block)
  end
end

#possible_route_proxy(name) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/arbre/patches.rb', line 28

def possible_route_proxy(name)
  if helpers.controller.class.parent &&
    helpers.respond_to?(namespace = helpers.controller.class.parent.to_s.underscore)
    if (route_proxy = helpers.send(namespace)).respond_to?(name)
      return route_proxy
    end
  end
  return nil
end