Sha256: 467c217777a7364a5472273eb2831d429eaaed536bd01e025581181a1b01ddb9

Contents?: true

Size: 1004 Bytes

Versions: 10

Compression:

Stored size: 1004 Bytes

Contents

# frozen_string_literal: true

module Phlexi
  module Field
    module Structure
      class FieldCollection
        include Enumerable

        class Builder
          include Phlex::Helpers

          attr_reader :key, :index

          def initialize(key, field, index)
            @key = key.to_s
            @field = field
            @index = index
          end

          def field(**)
            @field.class.new(key, **, parent: @field).tap do |field|
              yield field if block_given?
            end
          end
        end

        def initialize(field:, collection:, &)
          @field = field
          @collection = build_collection(collection)
          each(&) if block_given?
        end

        def each(&)
          @collection.each.with_index do |item, index|
            yield self.class::Builder.new(item, @field, index)
          end
        end

        private

        def build_collection(collection)
          collection
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
phlexi-field-0.0.11 lib/phlexi/field/structure/field_collection.rb
phlexi-field-0.0.10 lib/phlexi/field/structure/field_collection.rb
phlexi-field-0.0.9 lib/phlexi/field/structure/field_collection.rb
phlexi-field-0.0.8 lib/phlexi/field/structure/field_collection.rb
phlexi-field-0.0.7 lib/phlexi/field/structure/field_collection.rb
phlexi-field-0.0.6 lib/phlexi/field/structure/field_collection.rb
phlexi-field-0.0.5 lib/phlexi/field/structure/field_collection.rb
phlexi-field-0.0.4 lib/phlexi/field/structure/field_collection.rb
phlexi-field-0.0.3 lib/phlexi/field/structure/field_collection.rb
phlexi-field-0.0.2 lib/phlexi/field/structure/field_collection.rb