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

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

Direct Known Subclasses

Internal::PersonDetails

Defined Under Namespace

Classes: MissingId

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, #initial_doc, #initialize, #original_doc, passthrough, #print_pretty, #replace_doc!, #replace_original_doc!, #reset!, #to_json

Methods included from Common::BaseClass

#class_resolver, #redef_without_warning, #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.



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

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.

Raises:

  • MisssingId if the id or alt_id is missing.



53
54
55
56
57
58
# File 'lib/ecoportal/api/v1/person_details.rb', line 53

def []=(id, value)
  msg = "details[#{id.inspect}] is missing. Did you forget to load the schema?"
  raise MissingId, msg unless (field = get_field(id))

  field.value = value
end

#as_jsonObject



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

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

#changed?(id, doc = :original) ⇒ Boolean

Returns true if id exists and value has changed, false otherwise.

Returns:

  • (Boolean)

    true if id exists and value has changed, false otherwise



69
70
71
72
73
# File 'lib/ecoportal/api/v1/person_details.rb', line 69

def changed?(id, doc = :original)
  return false unless (field = get_field(id))

  field.as_update.key?("value")
end

#fieldsArray<SchemaFieldValue>

Gets all the fields of the PersonDetails.

Returns:



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

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:



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

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

#key?(id) ⇒ Boolean

Checks if an id or alt_id exists

Parameters:

  • id (String)

    the id or the alt_id of the target field.

Returns:

  • (Boolean)

    true if it exists, false otherwise



63
64
65
66
# File 'lib/ecoportal/api/v1/person_details.rb', line 63

def key?(id)
  @fields_by_id or index_fields
  @fields_by_id.key?(id) || @fields_by_alt_id.key?(id)
end

#original_value(id) ⇒ Object



75
76
77
78
79
# File 'lib/ecoportal/api/v1/person_details.rb', line 75

def original_value(id)
  return nil unless (field = get_field(id))

  field.original_doc["value"]
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.



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

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