https://github.com/ilyasgaraev/delegate_key
Create methods which return hash value
https://github.com/ilyasgaraev/delegate_key
delegate hash key ruby
Last synced: 6 months ago
JSON representation
Create methods which return hash value
- Host: GitHub
- URL: https://github.com/ilyasgaraev/delegate_key
- Owner: ilyasgaraev
- License: mit
- Created: 2019-09-22T23:56:41.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-06-21T21:20:25.000Z (over 2 years ago)
- Last Synced: 2025-08-09T20:21:37.024Z (7 months ago)
- Topics: delegate, hash, key, ruby
- Language: Ruby
- Homepage:
- Size: 17.6 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# DelegateKey
Provides a `delegate_key` class method to easily create methods which return hash value by key.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'delegate_key'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install delegate_key
## Usage
### Options
- `:to` - specifies the target object;
- `:prefix` - prefixes the new method with the target name or a custom prefix;
- `:private` - if set to true, changes method visibility to private.
`delegate_key` method receives one or more method names (specified as symbols or strings) and the name of the target object via the `:to` option (also a symbol or string).
```ruby
class Foo
delegate_key :key, to: :hash
def hash
{ key: "value" }
end
end
Foo.new.key # => "value"
```
If hash key is a string, use string as argument:
```ruby
class Foo
delegate_key "key", to: :hash
def hash
{ "key" => "value" }
end
end
Foo.new.key # => "value"
class Foo
delegate_key :key, to: :hash
def hash
{ "key" => "value" }
end
end
Foo.new.key # => nil
```
Multiple delegates to the same target are allowed:
```ruby
class Foo
delegate_key :bar, :baz, to: :hash
def hash
{ bar: "value for bar", baz: "value for baz" }
end
end
Foo.new.bar # => "value for bar"
Foo.new.baz # => "value for baz"
```
Delegates can optionally be prefixed using the `:prefix` option. If the value is `true`, the delegate methods are prefixed with the name of the object being delegated to.
```ruby
class Foo
delegate_key :key, to: :hash, prefix: true
def hash
{ key: "value" }
end
end
Foo.new.hash_key # => "value"
Foo.new.key # => NoMethodError: undefined method `key' for #
```
It is also possible to supply a custom prefix:
```ruby
class Foo
delegate_key :key, to: :hash, prefix: :custom
def hash
{ key: "value" }
end
end
Foo.new.custom_key # => "value"
```
The delegated methods are public by default. Pass `private: true` to change that.
```ruby
class Foo
delegate_key :key, to: :hash, private: true
def hash
{ key: "value" }
end
end
Foo.new.key # => NoMethodError: private method `key' called for #
```
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/ilyasgaraev/delegate_key. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Code of Conduct
Everyone interacting in the DelegateKey project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/delegate_key/blob/master/CODE_OF_CONDUCT.md).