https://github.com/dreikanter/callee
A Ruby gem to make classes callable
https://github.com/dreikanter/callee
callables hacktoberfest helpers ruby ruby-gems
Last synced: 10 months ago
JSON representation
A Ruby gem to make classes callable
- Host: GitHub
- URL: https://github.com/dreikanter/callee
- Owner: dreikanter
- License: mit
- Created: 2019-07-27T17:45:46.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-10-28T18:48:30.000Z (over 1 year ago)
- Last Synced: 2025-05-06T21:06:02.257Z (about 1 year ago)
- Topics: callables, hacktoberfest, helpers, ruby, ruby-gems
- Language: Ruby
- Homepage: https://rubygems.org/gems/callee
- Size: 57.6 KB
- Stars: 16
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Callee
This gem helps to define callable classes with strict params specification.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'callee'
```
And then execute:
```sh
bundle
```
Or install it yourself as:
```sh
gem install callee
```
## Usage
To make a class callable, you need to include `Callee` mixin and implement `call` instance method. Use [dry-initializer DSL](https://dry-rb.org/gems/dry-initializer/3.0/) to specify calling parameters and options if necessary. Here is a basic usage example:
``` ruby
class Power
include Callee
# Required parameter
param :base
# Option with a default value
option :exponent, optional: true, default: -> { 2 }
def call
base.pow(exponent)
end
end
Power.call(2) # => 4
Power.call(2, exponent: 10) # => 1024
```
Callable class may be used as a Proc. Compact notation in the next example is identical to `[1, 2, 3].map { |value| Power.call(value) }`
``` ruby
[1, 2, 3].map(&Power) # => [1, 4, 9]
```
Since Callee mixin inherits `dry-initializer` DSL, type constraints and coercion will also work, as usual. Just make sure to include `dry-types`:
``` ruby
require "dry-types"
class StrictPower
include Callee
param :base, type: Dry::Types["strict.integer"]
option :exponent, type: Dry::Types["strict.integer"], optional: true, default: -> { 2 }
def call
base.pow(exponent)
end
end
# Let's inherit StrictPower params definition
# and override "base" with more forgiving constraint
class CoerciblePower < StrictPower
param :value, type: Dry::Types["coercible.integer"]
end
string_values = %w[1 2 3]
string_values.map(&StrictPower) # Will raise Dry::Types::ConstraintError
string_values.map(&CoerciblePower) # => [1, 4, 9]
```
See [more examples](https://dry-rb.org/gems/dry-initializer/type-constraints/) in dry-rb docs.
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/dreikanter/callee.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).