Sha256: deaf6f16b2db5d5fe673e050c21d7e070ef4c52a8d61873a1ba02be2e407a4c9
Contents?: true
Size: 1.82 KB
Versions: 1
Compression:
Stored size: 1.82 KB
Contents
# attr_readonly ## Author Romain GEORGES <romain@ultragreen.net> Ultragreen Software ## Copyright Ultragreen Software (c) 2013 BSD-2 License ## Description Provide a new macro like attr_accessor to make real read_only accessors to frozen dup of the attributs given in parameters ## Version current : 1.0.0 ## Installation Add this line to your application's Gemfile: gem 'attr_readonly' And then execute: $ bundle Or install it yourself as: $ gem install attr_readonly ## Usage This gem add the macro : attr_readonly to class definition You could create reader accessors, in Ruby like : class Test attr_reader :foo def initialize(foo: '') @foo = foo end end if you try to do a direct affectation you raise an exception NoMethodError : Test::new.foo= 'bar' # => undefined method `foo=' for #<Test:0x00000801932598 @foo=""> (NoMethodError) But if you try to use a method on this accessor (for exemple <<), you could modify it ! test = Test::new p test.foo << 'bar' # => "bar" p test # => <Test:0x000008019323b8 @foo="bar"> if you want to secure an API, or prevent inapriopriate usage of a componant you want to diffuse, this a problem. So, use this gem, replace the macro attr_reader by attr_readonly : require 'attr_readonly' class Test attr_readonly :foo def initialize(foo: '') @foo = foo end end Test::new.foo << 'bar' # => can't modify frozen String (RuntimeError) No fear, it's just a frozen copy (dup), your real attribut is not frozen ## Contributing 1. Fork it 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create new Pull Request
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
attr_readonly-1.0.0 | README.md |