SuperclassDelegatingAccessor
A Ruby utility that defines both class and instance accessors for class attributes. Creates private _attr and _attr= methods that can still be used if the public methods are overridden.
Installation
Add this line to your application's Gemfile:
gem 'superclass_delegating_accessor', '~> 0.0.1'
And then execute:
$ bundle
Or install it yourself as:
$ gem install superclass_delegating_accessor
Usage
class Person
superclass_delegating_accessor :hair_colors
end
Person.hair_colors = [:brown, :black, :blonde, :red]
Person.hair_colors # => [:brown, :black, :blonde, :red]
Person.new.hair_colors # => [:brown, :black, :blonde, :red]
If a subclass changes the value then that would also change the value for parent class. Similarly if parent class changes the value then that would change the value of subclasses too.
class Male < Person
end
Male.hair_colors << :blue
Person.hair_colors # => [:brown, :black, :blonde, :red, :blue]
To opt out of the instance reader method, pass instance_reader: false
.
class Person
cattr_accessor :hair_colors, instance_writer: false, instance_reader: false
end
Person.new.hair_colors = [:brown] # => NoMethodError
Person.new.hair_colors # => NoMethodError
Or pass instance_accessor: false
, to opt out both instance methods.
class Person
cattr_accessor :hair_colors, instance_accessor: false
end
Person.new.hair_colors = [:brown] # => NoMethodError
Person.new.hair_colors # => NoMethodError
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request