Home

Awesome

AsReadonly

Expose your collections as readonly objects without fear.

Example

  ara = AsReadonly::Array.new([1,2])
  ara[0]    # => 1
  ara.clear # raises NoMethodError

Installation

Add this line to your application's Gemfile:

gem 'as_readonly'

And then execute:

$ bundle

Or install it yourself as:

$ gem install as_readonly

Require

Usage

require 'as_readonly/array'

class MyClass
  def initialize
    @collection = []
  end

  def collection
    @read_collection ||= AsReadonly::Array.new(@collection)
  end
end

Note

This gem does not prevent calling mutating methods on collection elements. Obviously.

class MyClass
  def initialize
    @collection = ["asd"]
  end

  def collection
    @read_collection ||= AsReadonly::Array.new(@collection)
  end
end

c = MyClass.new
c.collection[0].upcase

Inspirations

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request