Class: ExpressTemplates::Components::ForEach

Inherits:
Container show all
Defined in:
lib/express_templates/components/for_each.rb

Instance Attribute Summary collapse

Attributes inherited from Expander

#handlers, #locals, #stack

Instance Method Summary collapse

Methods included from Capabilities::Parenting

included

Methods inherited from Base

inherited

Methods included from Capabilities::Iterating

included

Methods included from Capabilities::Wrapping

included

Methods included from Capabilities::Rendering

included

Methods included from Capabilities::Templating

included

Methods included from Macro

included

Methods inherited from Expander

#expand, #initialize_expander, #method_missing, #process_children!, register_macros_for

Constructor Details

#initialize(*args) ⇒ ForEach

Returns a new instance of ForEach.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/express_templates/components/for_each.rb', line 6

def initialize(*args)
  iterator = args.shift
  options = args.first.kind_of?(Hash) ? args.shift : {}
  expander = args.shift
  @collection, @member = nil, (options[:as]||"item")
  if iterator.kind_of?(Symbol)
    @collection = iterator.to_s
    @member = collection.sub(/^@/, '').singularize
  elsif iterator.kind_of?(Proc)
    @collection = "(#{iterator.source}.call)"
  elsif iterator.kind_of?(String)
    @collection = "(#{iterator}.call)"
  else
    raise "ForEach unknown iterator: #{iterator}"
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ExpressTemplates::Expander

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



4
5
6
# File 'lib/express_templates/components/for_each.rb', line 4

def collection
  @collection
end

Instance Method Details

#compileObject



23
24
25
# File 'lib/express_templates/components/for_each.rb', line 23

def compile
  %Q((#{@collection}.each_with_index.map do |#{@member}, #{@member}_index|#{compile_children}\nend).join)
end