Sha256: f225b5078e8be2f1d8daf2e2165351e839075df476a7fd3f8a720d4160b7d160

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

module Lite
  module Component
    class Collection

      attr_reader :collection, :component, :spacer_template

      def initialize(collection, component:, spacer_template: nil)
        @collection = collection
        @component = component
        @spacer_template = spacer_template
      end

      class << self

        def render(collection, component)
          klass = new(collection, component)
          klass.render
        end

      end

      def render
        component.context.safe_join(iterated_collection)
      end

      private

      def collection_size
        @collection_size ||= collection.size
      end

      # rubocop:disable Metrics/AbcSize
      def iterated_collection
        collection.each_with_object([]).with_index do |(object, array), index|
          component.iteration = Lite::Component::Iteration.new(collection_size, index)
          component.options.deep_merge!(locals: { object: object, iteration: component.iteration })

          array << component.render_content
          next unless spacer_template && !component.iteration.last?

          array << component.context.render(spacer_template)
        end
      end
      # rubocop:enable Metrics/AbcSize

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lite-component-1.0.4 lib/lite/component/collection.rb
lite-component-1.0.3 lib/lite/component/collection.rb
lite-component-1.0.2 lib/lite/component/collection.rb