lib/ecoportal/api/v1/person_details.rb in ecoportal-api-0.8.4 vs lib/ecoportal/api/v1/person_details.rb in ecoportal-api-0.8.5
- old
+ new
@@ -1,9 +1,12 @@
module Ecoportal
module API
class V1
class PersonDetails < Common::BaseModel
+ class MissingId < StandardError
+ end
+
passthrough :schema_id
class_resolver :schema_field_value_class, "Ecoportal::API::V1::SchemaFieldValue"
def as_json
@@ -41,17 +44,25 @@
def [](id)
get_field(id)&.value
end
# Sets the value to one specific field of the PersonDetails.
+ # @raise MisssingId if the `id` or `alt_id` is missing.
# @param id [String] the `id` or the `alt_id` of the target field.
# @return [void]
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?"
+ raise MissingId.new("details[#{id.inspect}] is missing. Did you forget to load the schema?")
end
+ end
+
+ # Checks if an `id` or `alt_id` exists
+ # @param id [String] the `id` or the `alt_id` of the target field.
+ # @return [Boolean] `true` if it exists, `false` otherwise
+ def key?(id)
+ @fields_by_id.key?(id) || @fields_by_alt_id.key?(id)
end
protected
# Rebuilds the internal `id` and `alt_id` references to the fields.