0.0
No commit activity in last 3 years
No release in over 3 years
Thread-safe immutable objects that provide delegation and basic validation to hashes.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

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.