class Ecoportal::API::V1::PersonDetails

Public Instance Methods

[](id) click to toggle source

Gets the value of one specific field of the PersonDetails. @param id [String] the `id` or the `alt_id` of the target field. @return [String, Array<String>, Boolean, Array<Boolean>, Date, Array<Date>, Numberic, Array<Numeric>] the value of field or `nil` if missing.

# File lib/ecoportal/api/v1/person_details.rb, line 41
def [](id)
  get_field(id)&.value
end
[]=(id, value) click to toggle source

Sets the value to one specific field of the PersonDetails. @param id [String] the `id` or the `alt_id` of the target field. @return [void]

# File lib/ecoportal/api/v1/person_details.rb, line 48
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_json() click to toggle source
# File lib/ecoportal/api/v1/person_details.rb, line 9
def as_json
  super.merge "fields" => fields.map(&:as_json)
end
fields() click to toggle source

Gets all the fields of the PersonDetails. @return [Array<SchemaFieldValue>] the array of fields of the schema.

# File lib/ecoportal/api/v1/person_details.rb, line 23
def fields
  return @fields if defined?(@fields)
  @fields = (doc["fields"] || []).each_with_index.map do |field, i|
    schema_field_value_class.new(field, parent: self, key: ["fields", i])
  end
end
get_field(id) click to toggle source

Gets one specific field of the PersonDetails. @param id [String] the `id` or the `alt_id` of the target field. @return [nil, SchemaFieldValue] the field or `nil` if missing.

# File lib/ecoportal/api/v1/person_details.rb, line 33
def get_field(id)
  @fields_by_id or index_fields
  @fields_by_id[id] || @fields_by_alt_id[id]
end
schema_id=(value) click to toggle source

Sets the `id` of the PersonDetails. @note unless the new `id` is `nil`, this does not reset the `fields`. @param value [nil, String] the id of the schema.

# File lib/ecoportal/api/v1/person_details.rb, line 16
def schema_id=(value)
  @fields          = [] if value.nil?
  doc["schema_id"] = value
end

Protected Instance Methods

index_fields() click to toggle source

Rebuilds the internal `id` and `alt_id` references to the fields.

# File lib/ecoportal/api/v1/person_details.rb, line 59
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