Class: ExpressTemplates::Expander
- Inherits:
-
Object
- Object
- ExpressTemplates::Expander
- Defined in:
- lib/express_templates/expander.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Stack
Instance Attribute Summary collapse
-
#handlers ⇒ Object
Returns the value of attribute handlers.
-
#locals ⇒ Object
Returns the value of attribute locals.
-
#stack ⇒ Object
Returns the value of attribute stack.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
- #expand(source = nil, &block) ⇒ Object
-
#initialize(*args) ⇒ Expander
constructor
A new instance of Expander.
- #initialize_expander(template, handlers = {}, locals = {}) ⇒ Object
- #method_missing(name, *args, &block) ⇒ Object
- #process_children!(parent, &block) ⇒ Object
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) (*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
#handlers ⇒ Object
Returns the value of attribute handlers.
7 8 9 |
# File 'lib/express_templates/expander.rb', line 7 def handlers @handlers end |
#locals ⇒ Object
Returns the value of attribute locals.
7 8 9 |
# File 'lib/express_templates/expander.rb', line 7 def locals @locals end |
#stack ⇒ Object
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 (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 (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 |