https://github.com/dbongo/callable-mixin
Tiny Ruby mix-in that adds a .call class method to any service object
https://github.com/dbongo/callable-mixin
call callable gem mixin ruby service-object
Last synced: 6 months ago
JSON representation
Tiny Ruby mix-in that adds a .call class method to any service object
- Host: GitHub
- URL: https://github.com/dbongo/callable-mixin
- Owner: dbongo
- License: mit
- Created: 2025-06-19T15:38:00.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-06-20T19:19:18.000Z (7 months ago)
- Last Synced: 2025-06-20T19:36:56.629Z (7 months ago)
- Topics: call, callable, gem, mixin, ruby, service-object
- Language: Ruby
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[](https://badge.fury.io/rb/callable-mixin)
[](https://github.com/dbongo/callable-mixin/actions)
# Callable
Callable is a minimal Ruby mix-in that provides your classes with a simple `.call` class method (plus `to_proc`). It allows you to instantiate and immediately invoke an instance method (`call`) without boilerplate or additional dependencies. Perfect for clean, readable, and concise service objects.
Learn more at https://rubydoc.info/gems/callable-mixin
## Features
- Provides a `.call` class method to any Ruby class.
- Supports `Class#to_proc`, letting you use `&YourService` in `Enumerable` methods.
- Transparently forwards positional args, keyword args, and blocks to `#call`.
- Raises `ConstructionError` (subclass of `ArgumentError`) for constructor arity/kwarg mismatches.
- Raises `NotImplementedError` when the class does not define an instance `#call`.
- Zero external runtime dependencies.
- Compatible with MRI Ruby 2.3 through Ruby 3.x.
## Installation
Add this to your application's Gemfile (version 0.2.0 or later):
```ruby
gem 'callable-mixin', '~> 0.2.0'
```
Then execute:
```bash
bundle install
```
Or install directly:
```bash
gem install callable-mixin
```
## Usage
### Example Service with Multiple Arguments
```ruby
class SendNotification
include Callable
def initialize(user, message)
@user = user
@message = message
end
def call
NotificationMailer.notify(@user, @message).deliver_now
end
end
```
- **Explicit invocation** when you need multiple constructor args:
```ruby
users.each do |user|
SendNotification.call(user, "Your message here")
end
# You can also call via the `.()` alias:
SendNotification.(user, "Your message here")
```
### Example Service with Single Argument (to_proc Shorthand)
```ruby
class WelcomeUser
include Callable
def initialize(user)
@user = user
end
def call
NotificationMailer.welcome(@user).deliver_now
end
end
```
- **Proc shorthand** for one-arg services:
```ruby
users.each(&WelcomeUser)
# Or invoke with the `.()` alias:
WelcomeUser.(current_user)
```
### Using in a Plain Ruby Script
```ruby
require 'callable-mixin'
# Define your service with Callable
class MyService
include Callable
def initialize(value)
@value = value
end
def call
puts "Processing #{@value}"
end
end
# Invoke the service
MyService.call("some data")
# Or use the `.()` alias:
MyService.("more data")
```
## Development
After cloning the repo, install dependencies with `bundle install`, then run tests with `bundle exec rspec`.
## Contributing
Please follow these resources before submitting code or issues:
- [Code of Conduct](https://github.com/dbongo/callable-mixin/blob/main/.github/CODE_OF_CONDUCT.md)
- [Contributing Guide](https://github.com/dbongo/callable-mixin/blob/main/.github/CONTRIBUTING.md)
- [Pull Request Template](https://github.com/dbongo/callable-mixin/blob/main/.github/PULL_REQUEST_TEMPLATE.md)
Bug reports and pull requests are welcome on GitHub: https://github.com/dbongo/callable-mixin
## License
The gem is available under the MIT License.