Sha256: 3fb5660912015f58ff567ed506f7abba802f1836fa67a79e72b3246bdbaff1b4

Contents?: true

Size: 887 Bytes

Versions: 6

Compression:

Stored size: 887 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

6 entries across 6 versions & 2 rubygems

Version Path
express_admin-1.2.1 vendor/gems/express_templates/lib/express_templates/components/for_each.rb
express_admin-1.2.0 vendor/gems/express_templates/lib/express_templates/components/for_each.rb
express_templates-0.5.0 lib/express_templates/components/for_each.rb
express_templates-0.4.2 lib/express_templates/components/for_each.rb
express_templates-0.4.1 lib/express_templates/components/for_each.rb
express_templates-0.4.0 lib/express_templates/components/for_each.rb