t6d / smart_properties

Ruby accessors on steroids
MIT License
177 stars 20 forks source link

Explicitly passing nil default property fails required check #63

Open sambostock opened 7 years ago

sambostock commented 7 years ago

Given a property defined as follows:

class Foo
  include SmartProperties
  property :bar, required: true, default: true
end

The following code works, as expected:

Foo.new             # => #<Thing @bar=true>
Foo.new(bar: true)  # => #<Thing @bar=true>
Foo.new(bar: false) # => #<Thing @bar=false>

However, explicitly providing the bar property as nil fails:

Foo.new(bar: nil)
# SmartProperties::MissingValueError: Foo requires the property bar to be set

Is this desired behaviour? It would seem logical that explicitly passing in nil should fall through to the default, which would satisfy the required constraint.

t6d commented 3 years ago

This is indeed specified behaviour: https://github.com/t6d/smart_properties/blob/master/spec/required_values_spec.rb. That said, I'm currently working towards a 2.0 release and will investigate whether making the behaviour configurable is an option.