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, ref = :last) ⇒ Boolean
trueifidexists andvaluehas changed,falseotherwise. -
#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
idoralt_idexists. - #original_value(id) ⇒ Object
-
#schema_id=(value) ⇒ Object
Sets the
idof 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.
46 47 48 |
# File 'lib/ecoportal/api/v1/person_details.rb', line 46 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.
54 55 56 57 58 59 |
# File 'lib/ecoportal/api/v1/person_details.rb', line 54 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, ref = :last) ⇒ Boolean
Returns true if id exists and value has changed, false otherwise.
70 71 72 73 74 |
# File 'lib/ecoportal/api/v1/person_details.rb', line 70 def changed?(id, ref = :last) return false unless (field = get_field(id)) field.as_update(ref).key?('value') end |
#fields ⇒ Array<SchemaFieldValue>
Gets all the fields of the PersonDetails.
26 27 28 29 30 31 32 |
# 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.
37 38 39 40 |
# File 'lib/ecoportal/api/v1/person_details.rb', line 37 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
64 65 66 67 |
# File 'lib/ecoportal/api/v1/person_details.rb', line 64 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
76 77 78 79 80 |
# File 'lib/ecoportal/api/v1/person_details.rb', line 76 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 |