t6d / smart_properties

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

Adding writable as an option on property. #68

Closed fulrich closed 5 years ago

fulrich commented 5 years ago

By default property creates both an accessor and a reader for the property. Currently if you do not want a public accessor you have to do something similar to the following:

class TestClass
  property! :id
  protected :id=
end

This proposed change allows the ability to make a property read only:

class TestClass
  property! :id, read_only: true
end

When using the read_only flag as true an accessor will not be created for that property. If no read_only flag is provided the current default behaviour of creating an accessor will continue.

t6d commented 5 years ago

(I don't have strong opinions on either.)

fulrich commented 5 years ago

I liked the change to writable and have updated this PR to include it. I decided to keep the setters as not defined instead of protected simply because the naming doesn't convey this as strongly to me. writable states whether it is able to be written to or not. It doesn't convey that is can be written to but only by the class itself or inherited classes.