Class: Ecoportal::API::V1::PersonDetails
- Inherits:
-
Common::BaseModel
- Object
- Common::BaseModel
- Ecoportal::API::V1::PersonDetails
- Defined in:
- lib/ecoportal/api/v1/person_details.rb
Direct Known Subclasses
Defined Under Namespace
Classes: MissingId
Instance Attribute Summary
Attributes inherited from Common::BaseModel
Instance Method Summary collapse
-
#[](id) ⇒ String, ...
Gets the value of one specific field of the PersonDetails.
-
#[]=(id, value) ⇒ void
Sets the value to one specific field of the PersonDetails.
- #as_json ⇒ Object
-
#changed?(id, doc = :original) ⇒ Boolean
true
ifid
exists andvalue
has changed,false
otherwise. -
#fields ⇒ Array<SchemaFieldValue>
Gets all the fields of the PersonDetails.
-
#get_field(id) ⇒ nil, SchemaFieldValue
Gets one specific field of the PersonDetails.
-
#key?(id) ⇒ Boolean
Checks if an
id
oralt_id
exists. - #original_value(id) ⇒ Object
-
#schema_id=(value) ⇒ Object
Sets the
id
of the PersonDetails.
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.
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.
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_json ⇒ Object
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.
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 |
#fields ⇒ Array<SchemaFieldValue>
Gets all the fields of the PersonDetails.
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.
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
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
unless the new id
is nil
, this does not reset the fields
.
Sets the id
of the PersonDetails.
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 |