Class: ExpressTemplates::Components::ForEach
- Defined in:
- lib/express_templates/components/for_each.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
Attributes inherited from Expander
Instance Method Summary collapse
- #compile ⇒ Object
-
#initialize(*args) ⇒ ForEach
constructor
A new instance of ForEach.
Methods included from Capabilities::Parenting
Methods inherited from Base
Methods included from Capabilities::Iterating
Methods included from Capabilities::Wrapping
Methods included from Capabilities::Rendering
Methods included from Capabilities::Templating
Methods included from Macro
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 = args.first.kind_of?(Hash) ? args.shift : {} = args.shift @collection, @member = nil, ([: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
#collection ⇒ Object (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
#compile ⇒ Object
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 |