Skip to content

Thread-safe immutable objects that provide delegation and basic validation to hashes.

License

Notifications You must be signed in to change notification settings

delonnewman/hash_delegator

Repository files navigation

Build Status

HashDelegator

Thread-safe immutable objects that provide delegation and basic validation to hashes.

Synopsis

class Person < HashDelegator
  required :first_name, :last_name
  transform_keys(&:to_sym)

  def name
    "#{first_name} #{last_name}"
  end
end

person = Person.new(first_name: "Mary", last_name: "Lamb", age: 32)
person.age # => 32
person.name # => "Mary Lamb"

# it supports all non-mutating methods of Hash
person.merge!(favorite_food: "Thai") # => NoMethodError
person.merge(favorite_food: "Thai") # => #<Person { first_name: "Mary", last_name: "Lamb", age: 32 }>

# respects inheritance
class Employee < Person
  required :employee_id
end

Employee.new(age: 32, employee_id: 1234) # => Error, first_name attribute is required
Employee.new(first_name: "John", last_name: "Smith", age: 23, employee_id: 3456) # => #<Employee ...>

Why?

Because generality...

Installation

Add this line to your application's Gemfile:

gem 'hash_delegator'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install hash_delegator

See Also

License

The gem is available as open source under the terms of the MIT License.