Sha256: 92be704e38aaedb51ed93db3b2b5f4829a482c19e0ee3214d5c44a87031e41ce

Contents?: true

Size: 1.21 KB

Versions: 9

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_alt_id[wrapped.alt_id] = wrapped
          end
        end

        def get_field(id)
          @fields_by_id or index_fields
          @fields_by_id[id] || @fields_by_alt_id[id]
        end

        def [](id)
          get_field(id)&.value
        end

        def []=(id, value)
          if field = get_field(id)
            field.value = value
          end
        end

        private

        def schema_field_value_class
          V1::SchemaFieldValue
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ecoportal-api-0.1.12 lib/ecoportal/api/v1/person_details.rb
ecoportal-api-0.1.11 lib/ecoportal/api/v1/person_details.rb
ecoportal-api-0.1.10 lib/ecoportal/api/v1/person_details.rb
ecoportal-api-0.1.9 lib/ecoportal/api/v1/person_details.rb
ecoportal-api-0.1.8 lib/ecoportal/api/v1/person_details.rb
ecoportal-api-0.1.7 lib/ecoportal/api/v1/person_details.rb
ecoportal-api-0.1.6 lib/ecoportal/api/v1/person_details.rb
ecoportal-api-0.1.5 lib/ecoportal/api/v1/person_details.rb
ecoportal-api-0.1.4 lib/ecoportal/api/v1/person_details.rb