fhir_models
fhir_models copied to clipboard
Allow initialization of single values when quantity is 0..1
Single (non-Array-wrapped) values can be assigned to elements, but not at initialization. Consider this code sample:
# Initializing an Array-wrapped value
range = FHIR::Range.new(low: [FHIR::Quantity.new(value: 18)])
range.valid? # => true
# Setting a value
range.low = FHIR::Quantity.new(value: 18)
range.valid? # => true
puts range.to_json # => ... (valid FHIR JSON)
# Initializing plain value
range = FHIR::Range.new(low: FHIR::Quantity.new(value: 18))
# => Error: NoMethodError: undefined method `[]' for #<FHIR::Quantity:0x0055e8f174e6d8 @id=nil, @extension=[], @value=18, @comparator=nil, @unit=nil, @system=nil, @code=nil>
# => from /home/vagrant/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/fhir_models-3.0.2/lib/fhir_models/bootstrap/model.rb:58:in `method_missing'
I'd love to be able to use nil
or present
(or similar), rather than have to check to see if the object is nil
, []
, or [Object]
.
Second possibility
All non-initialized elements with qty 0..1
could be initialized to []
instead of nil
. That way, at least we can access something with [0]
and see if it's nil
, rather than doing .try(:[], 0)
, which is a little goofy.
Thanks for reporting this issue/suggestion and sorry for the delay in responding to this!
I think the suggestion makes sense and is something we should support in the library (without having to resort to wrapping it in an Array or jumping through other hoops).
Currently the library will "inflate" these complex elements if they are provided as a simple Hash. e.g.
range = FHIR::Range.new(low: {value: 18})
range.valid? # => true
Wrapping an element which should have a cardinality of 0..1 in an Array is undesirable and as such I'm not too keen on the second possibility.