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

#_key, #_parent

Instance Method Summary collapse

Methods inherited from Common::BaseModel

#as_update, #consolidate!, #dirty?, #doc, embeds_one, #initialize, #original_doc, passthrough, #print, #reset!, #to_json

Methods included from Common::BaseClass

#class_resolver, #resolve_class

Constructor Details

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

Instance Method Details

#[](id) ⇒ String, ...

Gets the value of one specific field of the PersonDetails.

Parameters:

  • id (String)

    the id or the alt_id of the target field.

Returns:

  • (String, Array<String>, Boolean, Array<Boolean>, Date, Array<Date>, Numberic, Array<Numeric>)

    the value of field or nil if missing.



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

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

#[]=(id, value) ⇒ void

This method returns an undefined value.

Sets the value to one specific field of the PersonDetails.

Parameters:

  • id (String)

    the id or the alt_id of the target field.



48
49
50
51
52
53
54
# 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_jsonObject



9
10
11
# File 'lib/ecoportal/api/v1/person_details.rb', line 9

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

#fieldsArray<SchemaFieldValue>

Gets all the fields of the PersonDetails.

Returns:



23
24
25
26
27
28
# 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) ⇒ nil, SchemaFieldValue

Gets one specific field of the PersonDetails.

Parameters:

  • id (String)

    the id or the alt_id of the target field.

Returns:



33
34
35
36
# 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) ⇒ Object

Note:

unless the new id is nil, this does not reset the fields.

Sets the id of the PersonDetails.

Parameters:

  • value (nil, String)

    the id of the schema.



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

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