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


73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/arbre/patches.rb', line 73

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.to_sym)
    assigns[name.to_sym]
  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



91
92
93
94
95
96
97
98
99
# File 'lib/arbre/patches.rb', line 91

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



55
56
57
58
59
60
61
62
63
# File 'lib/arbre/patches.rb', line 55

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