Sha256: 7467452fa9282557b1c75dddebd489dace6945f66defbf2fada8148410716582

Contents?: true

Size: 1.06 KB

Versions: 5

Compression:

Stored size: 1.06 KB

Contents

module JsonbAccessor
  class FieldsMap
    attr_accessor :nested_fields, :typed_fields

    def initialize(value_fields, typed_and_nested_fields)
      grouped_fields = extract_typed_and_nested_fields(typed_and_nested_fields)
      nested_fields, typed_fields = grouped_fields.values_at(:nested, :typed)

      self.typed_fields = implicitly_typed_fields(value_fields).merge(typed_fields)
      self.nested_fields = nested_fields
    end

    def names
      typed_fields.keys + nested_fields.keys
    end

    private

    def implicitly_typed_fields(value_fields)
      value_fields.each_with_object({}) do |field_name, implicitly_typed_fields|
        implicitly_typed_fields[field_name] = :value
      end
    end

    def extract_typed_and_nested_fields(typed_and_nested_fields)
      typed_and_nested_fields.each_with_object(nested: {}, typed: {}) do |(attribute_name, type_or_nested), grouped_attributes|
        group = type_or_nested.is_a?(Hash) ? grouped_attributes[:nested] : grouped_attributes[:typed]
        group[attribute_name] = type_or_nested
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
jsonb_accessor-0.4.0.beta lib/jsonb_accessor/fields_map.rb
jsonb_accessor-0.3.3 lib/jsonb_accessor/fields_map.rb
jsonb_accessor-0.3.2 lib/jsonb_accessor/fields_map.rb
jsonb_accessor-0.3.1 lib/jsonb_accessor/fields_map.rb
jsonb_accessor-0.3.0 lib/jsonb_accessor/fields_map.rb