callable-mixin 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 963cf5da4e14188c462b46f0f7a9cfc546cc189d4b464f5d265f2f6806a9b0d1
4
+ data.tar.gz: 1610376b0f5f392f3f6dd2bc87a926eae0baf3c8c1f6a6751380857f7957250a
5
+ SHA512:
6
+ metadata.gz: 258cb27a1405b6cdefca5c17e132864d54f0d895a8e73a6138cc2ba9b0bad92545e2cab8f17a52b214040d910fd34497e6046425fe4217cf405ccf8cffa42377
7
+ data.tar.gz: 32e8483f9145fb9660243276d5406a86cd41215df09ef90df59763420111d3a7f0227b85fca11cde468428bc851c11c924467043ff9c0ee66c287045584da5a1
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # Callable
2
+
3
+ Callable is a minimal Ruby mix-in that provides your classes with a simple `.call` class method. 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.
4
+
5
+ ## Features
6
+
7
+ - Provides a `.call` class method to any Ruby class.
8
+ - Transparently forwards positional arguments, keyword arguments, and blocks.
9
+ - Handles argument errors gracefully, raising clear exceptions.
10
+ - Zero external runtime dependencies.
11
+ - Compatible with MRI Ruby 2.3 through Ruby 3.x.
12
+
13
+ ## Installation
14
+
15
+ Add this to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'callable-mixin'
19
+ ```
20
+
21
+ Then execute:
22
+
23
+ ```bash
24
+ bundle install
25
+ ```
26
+
27
+ Or install directly:
28
+
29
+ ```bash
30
+ gem install callable-mixin
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ ### In a Bundler-based app (Rails, Hanami, etc.)
36
+
37
+ Simply include `Callable` in your class and implement an instance method named `call`:
38
+
39
+ ```ruby
40
+ class SendNotification
41
+ include Callable
42
+
43
+ def initialize(user, message)
44
+ @user = user
45
+ @message = message
46
+ end
47
+
48
+ def call
49
+ NotificationMailer.notify(@user, @message).deliver_now
50
+ end
51
+ end
52
+ ```
53
+
54
+ ### In a plain Ruby script
55
+
56
+ Require `'callable'`, include `Callable` in your class and implement an instance method named `call`:
57
+
58
+ ```ruby
59
+ require 'callable'
60
+
61
+ class SendNotification
62
+ include Callable
63
+
64
+ def initialize(user, message)
65
+ @user = user
66
+ @message = message
67
+ end
68
+
69
+ def call
70
+ NotificationMailer.notify(@user, @message).deliver_now
71
+ end
72
+ end
73
+ ```
74
+
75
+ Then run your class using the `.call` class method:
76
+
77
+ ```ruby
78
+ SendNotification.call(current_user, "Hello from Callable!")
79
+
80
+ # or use .() (syntactic sugar for .call)
81
+ SendNotification.(current_user, "Hello from Callable!")
82
+ ```
83
+
84
+
85
+ ## Development
86
+
87
+ After cloning the repo, install dependencies with `bundle install`, then run tests with `bundle exec rspec`.
88
+
89
+ ## Contributing
90
+
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dbongo/callable-mixin.
92
+
93
+ ## License
94
+
95
+ The gem is available under the MIT License.
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Callable
4
+ # Raised when .new fails due to arity / keyword mismatch
5
+ class ConstructionError < ArgumentError; end
6
+
7
+ def self.included(base)
8
+ base.extend(ClassMethods)
9
+ end
10
+
11
+ module ClassMethods
12
+ ##
13
+ # Instantiate and immediately invoke #call
14
+ # @param args [Array] positional arguments for initialize
15
+ # @param kwargs [Hash] keyword arguments for initialize
16
+ # @yield [*] optional configuration/result block
17
+ # @return [Object] whatever the instance #call returns
18
+ # @raise [ConstructionError] when instantiation fails
19
+ def call(*args, **kwargs, &block)
20
+ inst = begin
21
+ # avoids Ruby 2.0–2.6 quirk
22
+ if kwargs.empty?
23
+ new(*args, &block)
24
+ else
25
+ new(*args, **kwargs, &block)
26
+ end
27
+ end
28
+
29
+ if block_given? && inst.method(:call).arity.zero?
30
+ inst.call(&block)
31
+ else
32
+ inst.call
33
+ end
34
+ rescue ArgumentError => error
35
+ raise ConstructionError,
36
+ "Failed to construct #{name}.new with the supplied arguments: #{error.message}",
37
+ error.backtrace
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Callable
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'callable'
data/lib/callable.rb ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'callable/version'
4
+ require 'callable/mixin'
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: callable-mixin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Crowther
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rake
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '13.0'
19
+ type: :development
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '13.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.0'
40
+ description: Callable provides Ruby classes with a convenient `.call` method, simplifying
41
+ instantiation and method invocation.
42
+ email:
43
+ - crow404@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - README.md
49
+ - lib/callable-mixin.rb
50
+ - lib/callable.rb
51
+ - lib/callable/mixin.rb
52
+ - lib/callable/version.rb
53
+ homepage: https://github.com/dbongo/callable-mixin
54
+ licenses:
55
+ - MIT
56
+ metadata:
57
+ homepage_uri: https://github.com/dbongo/callable-mixin
58
+ source_code_uri: https://github.com/dbongo/callable-mixin
59
+ changelog_uri: https://github.com/dbongo/callable-mixin/blob/main/CHANGELOG.md
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 2.3.0
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.6.7
75
+ specification_version: 4
76
+ summary: Lightweight .call mix-in for service objects
77
+ test_files: []