Sha256: 56d1f42c19f1a21923021814366fa9fa8e47c138b896dc40d49c6be2b6f217a9

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

module Ecoportal
  module API
    class V1
      class PersonDetails < Common::BaseModel
        passthrough :schema_id, to: :doc

        def as_json
          super.merge "fields" => fields.map(&:as_json)
        end

        def schema_id=(value)
          @fields          = [] if value.nil?
          doc["schema_id"] = value
        end

        def fields
          return @fields if defined?(@fields)
          @fields = (doc["fields"] || []).map do |field|
            schema_field_value_class.new(field)
          end
        end

        def index_fields
          @fields_by_id     = {}
          @fields_by_alt_id = {}
          fields.each do |wrapped|
            @fields_by_id[wrapped.id] = wrapped
            @fields_by_id[wrapped.alt_id] = wrapped
          end
        end

        def [](id)
          @fields_by_id or index_fields
          (@fields_by_id[id] || @fields_by_alt_id[id])&.value
        end
        def []=(id, value)
          @fields_by_id or index_fields
          if field = (@fields_by_id[id] || @fields_by_alt_id[id])
            field.value = value
          end
        end

        private

        def schema_field_value_class
          V1::SchemaFieldValue
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ecoportal-api-0.1.3 lib/ecoportal/api/v1/person_details.rb
ecoportal-api-0.1.2 lib/ecoportal/api/v1/person_details.rb