Sha256: ef0f10e16c914c6a7d78907078e948a232d5b680892f111623d66e5d7c17c8bf

Contents?: true

Size: 1.31 KB

Versions: 11

Compression:

Stored size: 1.31 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
          else
            raise "details[#{id.inspect}] is missing. Did you forget to load the schema?"
          end
        end

        private

        def schema_field_value_class
          V1::SchemaFieldValue
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
ecoportal-api-0.3.8 lib/ecoportal/api/v1/person_details.rb
ecoportal-api-0.3.6 lib/ecoportal/api/v1/person_details.rb
ecoportal-api-0.3.5 lib/ecoportal/api/v1/person_details.rb
ecoportal-api-0.3.4 lib/ecoportal/api/v1/person_details.rb
ecoportal-api-0.3.3 lib/ecoportal/api/v1/person_details.rb
ecoportal-api-0.3.1 lib/ecoportal/api/v1/person_details.rb
ecoportal-api-0.3.0 lib/ecoportal/api/v1/person_details.rb
ecoportal-api-0.3.0.pre1 lib/ecoportal/api/v1/person_details.rb
ecoportal-api-0.2.2 lib/ecoportal/api/v1/person_details.rb
ecoportal-api-0.2.1 lib/ecoportal/api/v1/person_details.rb
ecoportal-api-0.2.0 lib/ecoportal/api/v1/person_details.rb