Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow initialization of single values when quantity is 0..1 #47

Open
andrewcsmith opened this issue Jun 6, 2018 · 1 comment
Open

Comments

@andrewcsmith
Copy link

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.

@radamson
Copy link
Contributor

radamson commented Sep 3, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants