Skip to content

Conversation

@ErickFabian
Copy link
Contributor

Properties set with a non nil value would not be overwritten using the default value

class Test < Couch::Base
  property :roles, class_name: Hash, default: {teacher: {}, student: {}}
end
 t3 = Test.find 'test/558d163d26141e5b770516292aa5afec'
=> #<Test:0x00007f9c59b8fc98 @doc={:_id=>"test/558d163d26141e5b770516292aa5afec", :_rev=>"2-8166172731f239617071bf3fe0ef7046", :roles=>{}}, @roles={}>
[27] pry(main)> t3.roles
=> {}
[28] pry(main)> t3.roles[:teacher]
=> nil

@alejandrodevs
Copy link
Contributor

alejandrodevs commented Jan 20, 2022

@ErickFabian This way we are using defaults kind of weird, Ex.

test = Test.new
test.roles #=> { teacher: {}, student: {}}
test.roles = {}
test.roles #=> { teacher: {}, student: {}}
test.roles = nil
test.roles #=> { teacher: {}, student: {}}

Default values should be only returned when the attributes has never been set. Ex.

test = Test.new
test.roles #=> { teacher: {}, student: {}}
test.roles = {}
test.roles #=> {}
test.roles = nil
test.roles #=> nil

We need some consistency and not unexpected behaviors. We should see how other libraries for class attributes work, like ActiveModel::Attributes, dry-types, etc

@ErickFabian
Copy link
Contributor Author

@alejandrodevs That was sort of the error, the school_contract property was set to {} but it should always at least have { teacher: {}, students: {} } the behaviour you are proposing is what is happening now


def cast_value(value)
return set_default if value.nil?
return set_default if empty_value?(value)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of doing this, use blank?

@ErickFabian
Copy link
Contributor Author

I'm not exactly sure if this bevahiour change is the right thing to do, i wanted to get feedback on it too, i'm not super set on if this should happen or not

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants