Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/delonnewman/hash_delegator
Thread-safe immutable objects that provide delegation and basic validation to hashes.
https://github.com/delonnewman/hash_delegator
data-oriented data-oriented-programming data-structures fp functional-programming hash hashmap immutable immutable-datastructures object-oriented-programming oop ruby ruby-gems
Last synced: about 2 months ago
JSON representation
Thread-safe immutable objects that provide delegation and basic validation to hashes.
- Host: GitHub
- URL: https://github.com/delonnewman/hash_delegator
- Owner: delonnewman
- License: mit
- Created: 2021-02-03T22:35:06.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-06-17T00:19:17.000Z (over 3 years ago)
- Last Synced: 2024-05-01T12:35:02.757Z (9 months ago)
- Topics: data-oriented, data-oriented-programming, data-structures, fp, functional-programming, hash, hashmap, immutable, immutable-datastructures, object-oriented-programming, oop, ruby, ruby-gems
- Language: Ruby
- Homepage:
- Size: 28.3 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.com/delonnewman/hash_delegator.svg?branch=master)](https://travis-ci.com/delonnewman/hash_delegator)
# HashDelegator
Thread-safe immutable objects that provide delegation and basic validation to hashes.
## Synopsis
```ruby
class Person < HashDelegator
required :first_name, :last_name
transform_keys(&:to_sym)def name
"#{first_name} #{last_name}"
end
endperson = 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") # => ## respects inheritance
class Employee < Person
required :employee_id
endEmployee.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) # => #
```## Why?
Because generality...
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'hash_delegator'
```And then execute:
$ bundle install
Or install it yourself as:
$ gem install hash_delegator
## See Also
- [Dry Struct](https://dry-rb.org/gems/dry-struct)
- [Clojure Records](https://clojure.org/reference/datatypes#_deftype_and_defrecord)
- [Delegator](https://rubyapi.org/3.0/o/delegator)## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).