Class: ExpressTemplates::Expander

Inherits:
Object
  • Object
show all
Defined in:
lib/express_templates/expander.rb

Direct Known Subclasses

Components::Base

Defined Under Namespace

Classes: Stack

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Expander

Returns a new instance of Expander.



9
10
11
# File 'lib/express_templates/expander.rb', line 9

def initialize(*args)
  initialize_expander(*args)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



78
79
80
81
82
83
84
85
86
87
# File 'lib/express_templates/expander.rb', line 78

def method_missing(name, *args, &block)
  return locals[name] if locals.keys.include?(name)

  if handlers.keys.include?(name)
    stack << handlers[name].send(name, *args, &block)
  else
    stack << ExpressTemplates::Markup::Wrapper.new(name.to_s, *args, &block)
  end
  nil
end

Instance Attribute Details

#handlersObject

Returns the value of attribute handlers.



7
8
9
# File 'lib/express_templates/expander.rb', line 7

def handlers
  @handlers
end

#localsObject

Returns the value of attribute locals.



7
8
9
# File 'lib/express_templates/expander.rb', line 7

def locals
  @locals
end

#stackObject

Returns the value of attribute stack.



7
8
9
# File 'lib/express_templates/expander.rb', line 7

def stack
  @stack
end

Class Method Details

.register_macros_for(*components) ⇒ Object

define a “macro” method for a component these methods accept args which are passed to the initializer for the component blocks supplied are evaluated and children added to the “stack” are added as children to the component



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/express_templates/expander.rb', line 52

def self.register_macros_for(*components)
  components.each do |component|
    define_method(component.macro_name.to_sym) do |*args, &block|
        new_component = nil
        # this is a code smell here.
        if component.ancestors.include?(Components::Capabilities::Building)
          new_component = component.new(*(args.push(self)), &block)
        else
          new_component = component.new(*(args.push(self)))
          process_children!(new_component, &block) unless block.nil?
        end
        stack << new_component
    end
  end
end

Instance Method Details

#expand(source = nil, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/express_templates/expander.rb', line 21

def expand(source=nil, &block)
  case
  when block.nil? && source
    modified = _wrap_instance_vars( _replace_yield_with_yielder(source) )
    instance_eval(modified, @template.inspect)
  when block
    instance_exec &block
  else
    raise ArgumentError
  end
  stack.current
end

#initialize_expander(template, handlers = {}, locals = {}) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/express_templates/expander.rb', line 13

def initialize_expander(template, handlers = {}, locals = {})
  @template = template
  @stack = Stack.new
  @handlers = handlers
  @locals = locals
  self
end

#process_children!(parent, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/express_templates/expander.rb', line 34

def process_children!(parent, &block)
  begin
    stack.descend!
    result = instance_exec &block
    if stack.current.empty? && result.is_a?(String)
      stack << result
    end
    parent.children += stack.current
    stack.ascend!
  end
  stack.current
end