Invariable

An Invariable bundles a number of read-only attributes. It can be used like a Hash as well as an Array. It supports subclassing and pattern matching.

An Invariable can be created explicitly as a Class like a Struct. Or existing classes can easily be extended to an Invariable.

Sample

require 'invariable'

class Person
  include Invariable
  attributes :name, :last_name
  attribute address: Invariable.new(:city, :zip, :street)

  def full_name
    "#{name} #{last_name}"
  end
end
...
john = Person.new(name: 'John', last_name: 'Doe')
john.full_name #=> "John Doe"
john.address.city #=> nil
john = john.update(
  address: { street: '123 Main St', city: 'Anytown', zip: '45678' }
)
john.dig(:address, :city) #=> "Anytown"

For more samples see the samples dir

Installation

Use Bundler to use Invariiable in your own project:

Include in your Gemfile:

gem 'invariable'

and install it by running Bundler:

bundle

To install the gem globally use:

gem install invariable

After that you need only a single line of code in your project to have all tools on board:

require 'invariable'