Sha256: ae07ab42b819607ce0daea5086a75b47799c9ad9baeeb695eb8658d32cadddc5

Contents?: true

Size: 885 Bytes

Versions: 4

Compression:

Stored size: 885 Bytes

Contents

module ExpressTemplates
  module Components
    class ForEach < Components::Container
      attr :collection, :member

      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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
express_templates-0.3.6 lib/express_templates/components/for_each.rb
express_templates-0.3.5 lib/express_templates/components/for_each.rb
express_templates-0.3.4 lib/express_templates/components/for_each.rb
express_templates-0.3.2 lib/express_templates/components/for_each.rb