-
Notifications
You must be signed in to change notification settings - Fork 18
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
Immutability #24
Comments
hey, thanks for feedback! class User
include ShallowAttributes
attribute :name, String
attribute :age, Integer
attribute :birthday, DateTime
end
user = User.new
user.name = 'Anton' # => okay
class ImmutableUser
include ImmutableShallowAttributes
attribute :name, String
attribute :age, Integer
attribute :birthday, DateTime
end
user = ImmutableUser.new
user.name = 'Anton' # => raise error WDYT? |
Sounds like a good option. What do you think about per-attribute? attribute :name, String, writer: false # true/false/:protected And for any such non-true attribute, it would also be excluded from mass assignment via |
so, I think it can be hard for implementing. Also, usually you need to use full immutable object raise than immutable attribute (only my experience) 🤔 |
Definitely agree that usually you want full-immutable. I've seen the exception recently where sometimes you have a persistable object and you want to assign Whether it's per-attribute, or full-immutable, your code is really well designed to accommodate it. I was able to get like 80% there by simply overwriting def initialize_setter(name, *)
super.tap { private name }
end This achieved the effect that you couldn't call a setter from outside, but |
Great lib, love how small and efficient it is. Would be great if it could produce immutable objects, either frozen by default, or with skipped attribute writers. Curious to hear your thoughts.
The text was updated successfully, but these errors were encountered: