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

Attribute's default_value can be modified #69

Closed
natesalisbury opened this issue Jun 13, 2017 · 7 comments
Closed

Attribute's default_value can be modified #69

natesalisbury opened this issue Jun 13, 2017 · 7 comments

Comments

@natesalisbury
Copy link
Contributor

It's possible to modify an attribute's default_value and have those modifications carry over to new instances of the model.

Example:

class MyModel
  include Aws::Record

  map_attr :things, default_value: {}
end
> MyModel.new.things[:foo] = :bar
=> :bar
> MyModel.new.things
=> {:foo=>:bar}
natesalisbury added a commit to natesalisbury/aws-sdk-ruby-record that referenced this issue Jun 13, 2017
@IbottaBaier
Copy link

IbottaBaier commented Jun 13, 2017

That is because attribute configuration is stored at the class level.

Also, freeze the default value that way this doesn't happen.

@IbottaBaier
Copy link

You aren't really using the default value as it is intended (or at least implemented); default value is what to read if the attribute is not present -- you are treating it more like Hash.of_hashes "default value" mechanism (spawning an empty hash when nothing is present).

@natesalisbury
Copy link
Contributor Author

natesalisbury commented Jun 13, 2017

Can you give me an example of when and how default_value should be used? I've read the description in the code

# @option opts [Object] :default_value Optional attribute used to
#   define a "default value" to be used if the attribute's value on an
#   item is nil or not set at persistence time.

But that still sounds to me like I could use this option to specify that I want an attribute initialized to something.

If you're interested, I've opened a pull request that prevents the default value from getting modified: #70

@IbottaBaier
Copy link

The default value is not like MySQL default that adds the value to the record if it is not set -- this is a read-only default value.

class MyRecord
  include Aws::DynamoDB::Record
  integer_attr :id, hash_key: true
  map_attr :the_best, default_value: { cheese: "is great" }
end

record = MyRecord.new(id: 1)
record.the_best #=> { cheese: "is great" }
record.save!
record = MyRecord.find(id: 1)
record.the_best #=> nil

natesalisbury added a commit to natesalisbury/aws-sdk-ruby-record that referenced this issue Jun 13, 2017
@awood45 awood45 added the bug label Jun 13, 2017
@awood45
Copy link
Contributor

awood45 commented Jun 13, 2017

If we have to freeze the value, it's a bug. Reviewing the PR's suitability as a bug fix.

natesalisbury added a commit to natesalisbury/aws-sdk-ruby-record that referenced this issue Jun 13, 2017
@awood45
Copy link
Contributor

awood45 commented Jun 15, 2017

Fixed by #71

@awood45 awood45 closed this as completed Jun 15, 2017
@awood45
Copy link
Contributor

awood45 commented Jun 16, 2017

Released this update in version 1.1.1

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

No branches or pull requests

3 participants