solid_callback 1.0.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 +7 -0
- data/.rspec +3 -0
- data/.ruby-gemset +1 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +37 -0
- data/LICENSE.txt +21 -0
- data/README.md +197 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/solid_callback/core.rb +98 -0
- data/lib/solid_callback/hooks.rb +31 -0
- data/lib/solid_callback/method_wrapper.rb +55 -0
- data/lib/solid_callback/version.rb +5 -0
- data/lib/solid_callback.rb +33 -0
- data/sig/solid_callback.rbs +4 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 921e574702baf327a4d7c3f925f266ded194cac1ca8f3378a0a65a8d37665727
|
4
|
+
data.tar.gz: 6cb662e52538824419e3b089c0e10f8af4571bd4712c492eb28c968736694092
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1e489657dceb943f10aafbdbc05485589bda6627a3d0d2fb9fd8efc1277356aeb7606011830f6f139bbc029c4271decad67cdb8b6320d37039cf3aeb31e2ffcb
|
7
|
+
data.tar.gz: c378f4c32879c3194e7736f497261396b5eb6cbefea55724a30751444dd91c2f07895d6865c8f45710a10502b016d568d7b88028f74eb90be8b888c38d8ee385
|
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
solid_callback_2.6.0
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at pgokulmca@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
solid_callback (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.6.0)
|
10
|
+
rake (13.2.1)
|
11
|
+
rspec (3.13.0)
|
12
|
+
rspec-core (~> 3.13.0)
|
13
|
+
rspec-expectations (~> 3.13.0)
|
14
|
+
rspec-mocks (~> 3.13.0)
|
15
|
+
rspec-core (3.13.3)
|
16
|
+
rspec-support (~> 3.13.0)
|
17
|
+
rspec-expectations (3.13.3)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.13.0)
|
20
|
+
rspec-mocks (3.13.2)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.13.0)
|
23
|
+
rspec-support (3.13.2)
|
24
|
+
yard (0.9.37)
|
25
|
+
|
26
|
+
PLATFORMS
|
27
|
+
x86_64-darwin-23
|
28
|
+
|
29
|
+
DEPENDENCIES
|
30
|
+
bundler (~> 2.0)
|
31
|
+
rake (~> 13.0)
|
32
|
+
rspec (~> 3.0)
|
33
|
+
solid_callback!
|
34
|
+
yard (~> 0.9)
|
35
|
+
|
36
|
+
BUNDLED WITH
|
37
|
+
2.2.33
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Gokul
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,197 @@
|
|
1
|
+
# SolidCallback
|
2
|
+
|
3
|
+
[](https://badge.fury.io/rb/solid_callback)
|
4
|
+
[](https://github.com/gklsan/solid_callback/actions)
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
6
|
+
|
7
|
+
SolidCallback adds powerful method interception capabilities to your Ruby classes with near-zero overhead. Clean, flexible, and unobtrusive.
|
8
|
+
|
9
|
+
## Features
|
10
|
+
|
11
|
+
- 🔄 **Method Lifecycle Hooks**: `before_call`, `after_call`, and `around_call` - intercept methods without modifying their code
|
12
|
+
- 🧩 **Zero-coupling**: Keep your business logic and cross-cutting concerns separate
|
13
|
+
- 🔍 **Selective targeting**: Apply callbacks to specific methods or all methods
|
14
|
+
- ⚡ **Performance-focused**: Minimal overhead through efficient method wrapping
|
15
|
+
- 🔒 **Thread-safe**: Safely use in concurrent applications
|
16
|
+
- 📝 **Conditional execution**: Run callbacks only when specific conditions are met
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem 'solid_callback'
|
24
|
+
```
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
```bash
|
29
|
+
$ bundle install
|
30
|
+
```
|
31
|
+
|
32
|
+
Or install it yourself:
|
33
|
+
|
34
|
+
```bash
|
35
|
+
$ gem install solid_callback
|
36
|
+
```
|
37
|
+
|
38
|
+
## Usage
|
39
|
+
|
40
|
+
### Basic Example
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
require 'solid_callback'
|
44
|
+
|
45
|
+
class UserService
|
46
|
+
include SolidCallback
|
47
|
+
|
48
|
+
before_call :authenticate
|
49
|
+
after_call :log_activity
|
50
|
+
around_call :measure_performance
|
51
|
+
|
52
|
+
def find_user(id)
|
53
|
+
puts "Finding user with ID: #{id}"
|
54
|
+
{ id: id, name: "User #{id}" }
|
55
|
+
end
|
56
|
+
|
57
|
+
def update_user(id, attributes)
|
58
|
+
puts "Updating user #{id} with #{attributes}"
|
59
|
+
{ id: id, updated: true }
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def authenticate
|
65
|
+
puts "🔐 Authenticating request"
|
66
|
+
end
|
67
|
+
|
68
|
+
def log_activity
|
69
|
+
puts "📝 Logging activity"
|
70
|
+
end
|
71
|
+
|
72
|
+
def measure_performance
|
73
|
+
start_time = Time.now
|
74
|
+
result = yield # Execute the original method
|
75
|
+
duration = Time.now - start_time
|
76
|
+
puts "⏱️ Method took #{duration} seconds"
|
77
|
+
result # Return the original result
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
service = UserService.new
|
82
|
+
service.find_user(42)
|
83
|
+
```
|
84
|
+
|
85
|
+
### Advanced Usage
|
86
|
+
|
87
|
+
Apply callbacks to specific methods:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
class PaymentProcessor
|
91
|
+
include SolidCallback
|
92
|
+
|
93
|
+
before_call :validate_amount, only: [:charge, :refund]
|
94
|
+
after_call :send_receipt, only: [:charge]
|
95
|
+
after_call :notify_fraud_department, only: [:flag_suspicious]
|
96
|
+
around_call :transaction, only: [:charge, :refund]
|
97
|
+
|
98
|
+
# Rest of class...
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
102
|
+
Use conditional callbacks:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
class DocumentProcessor
|
106
|
+
include SolidCallback
|
107
|
+
|
108
|
+
attr_reader :document_size
|
109
|
+
|
110
|
+
before_call :check_permissions
|
111
|
+
before_call :backup_document, if: :large_document?
|
112
|
+
around_call :with_retry, unless: :read_only?
|
113
|
+
|
114
|
+
def process_document(doc)
|
115
|
+
# Implementation...
|
116
|
+
end
|
117
|
+
|
118
|
+
private
|
119
|
+
|
120
|
+
def large_document?
|
121
|
+
@document_size > 10_000
|
122
|
+
end
|
123
|
+
|
124
|
+
def read_only?
|
125
|
+
# Some condition
|
126
|
+
end
|
127
|
+
end
|
128
|
+
```
|
129
|
+
|
130
|
+
You can even use procs for conditions:
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
before_call :notify_admin, if: -> { Rails.env.production? }
|
134
|
+
```
|
135
|
+
|
136
|
+
### Skipping Callbacks
|
137
|
+
|
138
|
+
Skip callbacks for specific methods:
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
class ApiService
|
142
|
+
include SolidCallback
|
143
|
+
|
144
|
+
before_call :rate_limit
|
145
|
+
|
146
|
+
skip_callbacks_for :health_check
|
147
|
+
|
148
|
+
def get_data
|
149
|
+
# Implementation...
|
150
|
+
end
|
151
|
+
|
152
|
+
def health_check
|
153
|
+
# This method won't trigger the rate_limit callback
|
154
|
+
{ status: "ok" }
|
155
|
+
end
|
156
|
+
end
|
157
|
+
```
|
158
|
+
|
159
|
+
## Callback Options
|
160
|
+
|
161
|
+
Each callback method accepts the following options:
|
162
|
+
|
163
|
+
| Option | Description |
|
164
|
+
|--------|-------------|
|
165
|
+
| `only` | Array of method names to which the callback applies |
|
166
|
+
| `except` | Array of method names to which the callback does not apply |
|
167
|
+
| `if` | Symbol (method name) or Proc that must return true for the callback to run |
|
168
|
+
| `unless` | Symbol (method name) or Proc that must return false for the callback to run |
|
169
|
+
|
170
|
+
## How It Works
|
171
|
+
|
172
|
+
Callbacker uses Ruby's metaprogramming to wrap your methods with callback functionality:
|
173
|
+
|
174
|
+
1. When included, it extends your class with callback registration methods
|
175
|
+
2. When a callback is registered, it stores the configuration
|
176
|
+
3. When a method is defined, it wraps the method with callback handling code
|
177
|
+
4. When the method is called, it executes the callbacks in the proper order
|
178
|
+
|
179
|
+
## 📚 Use Cases
|
180
|
+
|
181
|
+
- Authentication & Authorization
|
182
|
+
- Logging & Monitoring
|
183
|
+
- Caching
|
184
|
+
- Performance measurement
|
185
|
+
- Error handling
|
186
|
+
- Background job retries
|
187
|
+
- Transaction management
|
188
|
+
- Input validation
|
189
|
+
- Data transformation
|
190
|
+
|
191
|
+
## 🤝 Contributing
|
192
|
+
|
193
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/gklsan/solid_callback.
|
194
|
+
|
195
|
+
## 📄 License
|
196
|
+
|
197
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "solid_callback"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
module SolidCallback
|
2
|
+
module Core
|
3
|
+
# Get all callbacks of a specific type
|
4
|
+
def callbacks_for(type)
|
5
|
+
@_solid_callback_store ||= { before: {}, after: {}, around: {} }
|
6
|
+
@_solid_callback_store[type]
|
7
|
+
end
|
8
|
+
|
9
|
+
# Register a callback
|
10
|
+
def register_callback(type, callback_method, options = {})
|
11
|
+
# Make sure callback store exists
|
12
|
+
@_solid_callback_store ||= { before: {}, after: {}, around: {} }
|
13
|
+
|
14
|
+
# Normalize options
|
15
|
+
only = Array(options[:only] || :all)
|
16
|
+
except = Array(options[:except] || [])
|
17
|
+
if_condition = options[:if]
|
18
|
+
unless_condition = options[:unless]
|
19
|
+
|
20
|
+
# Store the callback information
|
21
|
+
@_solid_callback_store[type][callback_method] = {
|
22
|
+
method: callback_method,
|
23
|
+
only: only,
|
24
|
+
except: except,
|
25
|
+
if: if_condition,
|
26
|
+
unless: unless_condition
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
# Check if a callback should be run for a method
|
31
|
+
def callback_applicable?(callback, method_name, instance)
|
32
|
+
# Check only/except constraints
|
33
|
+
return false if callback[:except].include?(method_name)
|
34
|
+
return false unless callback[:only] == [:all] || callback[:only].include?(method_name)
|
35
|
+
|
36
|
+
# Check conditional constraints
|
37
|
+
if callback[:if]
|
38
|
+
condition = callback[:if]
|
39
|
+
return false unless evaluate_condition(condition, instance)
|
40
|
+
end
|
41
|
+
|
42
|
+
if callback[:unless]
|
43
|
+
condition = callback[:unless]
|
44
|
+
return false if evaluate_condition(condition, instance)
|
45
|
+
end
|
46
|
+
|
47
|
+
true
|
48
|
+
end
|
49
|
+
|
50
|
+
# Evaluate a condition (symbol method name, proc, or lambda)
|
51
|
+
def evaluate_condition(condition, instance)
|
52
|
+
case condition
|
53
|
+
when Symbol, String
|
54
|
+
instance.send(condition)
|
55
|
+
when Proc
|
56
|
+
instance.instance_exec(&condition)
|
57
|
+
else
|
58
|
+
true
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Handle method_added hook
|
63
|
+
def handle_method_added(method_name)
|
64
|
+
# Skip if we're in the process of defining a wrapped method
|
65
|
+
return if @_solid_callback_wrapping_method
|
66
|
+
|
67
|
+
# Skip special methods and private/protected methods
|
68
|
+
return if method_name.to_s.start_with?('_solid_callback_')
|
69
|
+
return if private_method_defined?(method_name) || protected_method_defined?(method_name)
|
70
|
+
|
71
|
+
wrap_method_with_callbacks(method_name)
|
72
|
+
end
|
73
|
+
|
74
|
+
# Wrap a method with callbacks
|
75
|
+
def wrap_method_with_callbacks(method_name)
|
76
|
+
return unless instance_methods(false).include?(method_name)
|
77
|
+
|
78
|
+
@_solid_callback_wrapping_method = true
|
79
|
+
|
80
|
+
# Create a reference to the original method
|
81
|
+
alias_method "_solid_callback_original_#{method_name}", method_name
|
82
|
+
|
83
|
+
# Redefine the method with callbacks
|
84
|
+
define_method(method_name) do |*args, &block|
|
85
|
+
run_callbacks(:before, method_name)
|
86
|
+
|
87
|
+
# Execute around callbacks or the original method
|
88
|
+
result = run_around_callbacks(method_name, args, block)
|
89
|
+
|
90
|
+
run_callbacks(:after, method_name)
|
91
|
+
|
92
|
+
result
|
93
|
+
end
|
94
|
+
|
95
|
+
@_solid_callback_wrapping_method = false
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module SolidCallback
|
2
|
+
module Hooks
|
3
|
+
# Before hooks
|
4
|
+
def before_call(method_name, options = {})
|
5
|
+
register_callback(:before, method_name, options)
|
6
|
+
end
|
7
|
+
|
8
|
+
# After hooks
|
9
|
+
def after_call(method_name, options = {})
|
10
|
+
register_callback(:after, method_name, options)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Around hooks
|
14
|
+
def around_call(method_name, options = {})
|
15
|
+
register_callback(:around, method_name, options)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Register all methods that need callbacks
|
19
|
+
def wrap_methods(*method_names)
|
20
|
+
method_names.each do |method_name|
|
21
|
+
wrap_method_with_callbacks(method_name) if method_defined?(method_name)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Skip callbacks for specific methods
|
26
|
+
def skip_callbacks_for(*method_names)
|
27
|
+
@_solid_callback_skip_methods ||= []
|
28
|
+
@_solid_callback_skip_methods.concat(method_names)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module SolidCallback
|
2
|
+
module MethodWrapper
|
3
|
+
private
|
4
|
+
|
5
|
+
# Run callbacks of a specific type for a method
|
6
|
+
def run_callbacks(type, method_name)
|
7
|
+
callbacks = self.class.callbacks_for(type)
|
8
|
+
return if callbacks.nil? || callbacks.empty?
|
9
|
+
|
10
|
+
callbacks.each do |callback_method, callback_config|
|
11
|
+
next unless self.class.callback_applicable?(callback_config, method_name, self)
|
12
|
+
send(callback_method)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Run around callbacks as a chain
|
17
|
+
def run_around_callbacks(method_name, args, block)
|
18
|
+
around_callbacks = collect_applicable_around_callbacks(method_name)
|
19
|
+
|
20
|
+
if around_callbacks.empty?
|
21
|
+
# No around callbacks, directly call the original method
|
22
|
+
send("_solid_callback_original_#{method_name}", *args, &block)
|
23
|
+
else
|
24
|
+
# Execute the chain of around callbacks
|
25
|
+
execute_around_chain(around_callbacks, 0, method_name, args, block)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Collect all applicable around callbacks for a method
|
30
|
+
def collect_applicable_around_callbacks(method_name)
|
31
|
+
callbacks = self.class.callbacks_for(:around)
|
32
|
+
return [] if callbacks.nil? || callbacks.empty?
|
33
|
+
|
34
|
+
callbacks.select do |_, callback_config|
|
35
|
+
self.class.callback_applicable?(callback_config, method_name, self)
|
36
|
+
end.map { |callback_method, _| callback_method }
|
37
|
+
end
|
38
|
+
|
39
|
+
# Execute the chain of around callbacks
|
40
|
+
def execute_around_chain(callbacks, index, method_name, args, block)
|
41
|
+
if index >= callbacks.length
|
42
|
+
# End of the chain, call the original method
|
43
|
+
send("_solid_callback_original_#{method_name}", *args, &block)
|
44
|
+
else
|
45
|
+
# Call the next callback in the chain
|
46
|
+
callback_method = callbacks[index]
|
47
|
+
|
48
|
+
# Call the around callback with a continuation to the next callback
|
49
|
+
send(callback_method) do
|
50
|
+
execute_around_chain(callbacks, index + 1, method_name, args, block)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "solid_callback/version"
|
4
|
+
require "solid_callback/core"
|
5
|
+
require "solid_callback/hooks"
|
6
|
+
require "solid_callback/method_wrapper"
|
7
|
+
|
8
|
+
module SolidCallback
|
9
|
+
class Error < StandardError; end
|
10
|
+
|
11
|
+
# This method is called when the module is included in a class
|
12
|
+
def self.included(base)
|
13
|
+
base.extend(SolidCallback::Core)
|
14
|
+
base.extend(SolidCallback::Hooks)
|
15
|
+
base.send(:include, SolidCallback::MethodWrapper)
|
16
|
+
|
17
|
+
# Initialize callback store on the class
|
18
|
+
base.instance_variable_set(:@_solid_callback_store, {
|
19
|
+
before: {},
|
20
|
+
after: {},
|
21
|
+
around: {}
|
22
|
+
})
|
23
|
+
|
24
|
+
# Setup method_added hook to handle methods defined after including SolidCallback
|
25
|
+
base.singleton_class.prepend(Module.new do
|
26
|
+
def method_added(method_name)
|
27
|
+
super
|
28
|
+
# Delegate to SolidCallback's method added handler
|
29
|
+
handle_method_added(method_name) if respond_to?(:handle_method_added)
|
30
|
+
end
|
31
|
+
end)
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: solid_callback
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gokul
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-03-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '13.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '13.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: yard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.9'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.9'
|
69
|
+
description: SolidCallback adds powerful method interception capabilities to your
|
70
|
+
Ruby classes with near-zero overhead. Clean, flexible, and unobtrusive.
|
71
|
+
email:
|
72
|
+
- pgokulmca@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".rspec"
|
78
|
+
- ".ruby-gemset"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- Gemfile.lock
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/solid_callback.rb
|
88
|
+
- lib/solid_callback/core.rb
|
89
|
+
- lib/solid_callback/hooks.rb
|
90
|
+
- lib/solid_callback/method_wrapper.rb
|
91
|
+
- lib/solid_callback/version.rb
|
92
|
+
- sig/solid_callback.rbs
|
93
|
+
homepage: https://github.com/gklsan/solid_callback
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata:
|
97
|
+
homepage_uri: https://github.com/gklsan/solid_callback
|
98
|
+
source_code_uri: https://github.com/gklsan/solid_callback
|
99
|
+
changelog_uri: https://github.com/gklsan/solid_callback/releases
|
100
|
+
bug_tracker_uri: https://github.com/gklsan/solid_callback/issues
|
101
|
+
documentation_uri: https://rubydoc.info/github/gklsan/solid_callback
|
102
|
+
post_install_message:
|
103
|
+
rdoc_options: []
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 2.6.0
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubygems_version: 3.0.3.1
|
118
|
+
signing_key:
|
119
|
+
specification_version: 4
|
120
|
+
summary: SolidCallback adds powerful method interception capabilities to your Ruby
|
121
|
+
classes with near-zero overhead. Clean, flexible, and unobtrusive.
|
122
|
+
test_files: []
|