Class: Ecoportal::API::V1::PersonDetails

Inherits:
Common::BaseModel show all
Defined in:
lib/ecoportal/api/v1/person_details.rb

Direct Known Subclasses

Internal::PersonDetails

Instance Attribute Summary

Attributes inherited from Common::BaseModel

#doc

Instance Method Summary collapse

Methods inherited from Common::BaseModel

#as_update, build, #initialize, passthrough, #print, #to_json

Constructor Details

This class inherits a constructor from Ecoportal::API::Common::BaseModel

Instance Method Details

#[](id) ⇒ Object



37
38
39
# File 'lib/ecoportal/api/v1/person_details.rb', line 37

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

#[]=(id, value) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/ecoportal/api/v1/person_details.rb', line 41

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

#as_jsonObject



7
8
9
# File 'lib/ecoportal/api/v1/person_details.rb', line 7

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

#fieldsObject



16
17
18
19
20
21
# File 'lib/ecoportal/api/v1/person_details.rb', line 16

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

#get_field(id) ⇒ Object



32
33
34
35
# File 'lib/ecoportal/api/v1/person_details.rb', line 32

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

#index_fieldsObject



23
24
25
26
27
28
29
30
# File 'lib/ecoportal/api/v1/person_details.rb', line 23

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

#schema_id=(value) ⇒ Object



11
12
13
14
# File 'lib/ecoportal/api/v1/person_details.rb', line 11

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