super_callbacks 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +39 -1
- data/lib/super_callbacks.rb +7 -4
- data/lib/super_callbacks/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 281eed10edbb6eb455f6a91c229a6a7391a2ab65
|
4
|
+
data.tar.gz: ed6e915ce8778783ceeb9aa666622c7e36f7fdd4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 004497ddd9066a5c602f617024dc95b6993ffd455a8f153f28e760b0f4e4246c578781888c067885bf116065c16c088ce2f64cc133ad972f62c5b3e934cb99cc
|
7
|
+
data.tar.gz: 5d8c7b1d744307b9d9000f810cd3affb7089a4ce259035bef9e7e6e4505a6918cc038bf16166024b8873a3e42bb4a3ee200054c4a6543b17e1aa449123e6e0d6
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# SuperCallbacks
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/jrpolidario/super_callbacks.svg?branch=master)](https://travis-ci.org/jrpolidario/super_callbacks)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/super_callbacks.svg)](https://badge.fury.io/rb/super_callbacks)
|
4
5
|
|
5
6
|
* Allows `before` and `after` callbacks to any Class.
|
6
7
|
* Supports both class and instance level callbacks
|
@@ -106,6 +107,7 @@ foo.bar
|
|
106
107
|
```
|
107
108
|
|
108
109
|
*Notice above that you can also call another method instead of supplying a block.*
|
110
|
+
|
109
111
|
*Above uses `before`, but works similarly with `after`*
|
110
112
|
|
111
113
|
### Example 3 (Multiple Callbacks)
|
@@ -142,6 +144,7 @@ foo.bar
|
|
142
144
|
```
|
143
145
|
|
144
146
|
*Notice above multiple callbacks are supported, and that they are called in firt-come-first-served order.*
|
147
|
+
|
145
148
|
*Above uses `before`, but works similarly with `after`*
|
146
149
|
|
147
150
|
### Example 4 (Setter Method Callbacks)
|
@@ -286,6 +289,7 @@ foo_2.bar
|
|
286
289
|
```
|
287
290
|
|
288
291
|
*Notice above that foo_1 and foo_2 both call the class-level callbacks, while they have independent (not-shared) instance-level callbacks defined. Order of execution is class-level first then instance-level, of which defined callbacks are then in order of first-come-first-serve.*
|
292
|
+
|
289
293
|
*Above uses `before`, but works similarly with `after`*
|
290
294
|
|
291
295
|
### Example 8 (Inherited Callbacks)
|
@@ -322,8 +326,42 @@ sub_foo.bar
|
|
322
326
|
```
|
323
327
|
|
324
328
|
*Notice above `sub_foo` calls both `before` callbacks defined in `Foo` and `SubFoo`, because SubFoo inherits from Foo. Callbacks are called in order of ancestors descending; meaning it starts calling the top-level ancestor superclass callbacks, and then calling its subclass callbacks, until it reaches the instance's class callbacks*
|
329
|
+
|
325
330
|
*Above uses `before`, but works similarly with `after`*
|
326
331
|
|
332
|
+
### Example 9 (Requiring Method To Be Defined)
|
333
|
+
|
334
|
+
```ruby
|
335
|
+
class Foo
|
336
|
+
include SuperCallbacks
|
337
|
+
|
338
|
+
after! :bar do
|
339
|
+
puts 'after bar!'
|
340
|
+
end
|
341
|
+
|
342
|
+
def bar
|
343
|
+
puts 'bar!'
|
344
|
+
end
|
345
|
+
end
|
346
|
+
# => ArgumentError: `bar` is not or not yet defined for Foo
|
347
|
+
|
348
|
+
class Foo
|
349
|
+
include SuperCallbacks
|
350
|
+
|
351
|
+
def bar
|
352
|
+
puts 'bar!'
|
353
|
+
end
|
354
|
+
|
355
|
+
after! :bar do
|
356
|
+
puts 'after bar!'
|
357
|
+
end
|
358
|
+
end
|
359
|
+
# => [NO ERRORS]
|
360
|
+
```
|
361
|
+
|
362
|
+
*From above, sometimes I noticed that I forgot to define a method! So the bang `!` version is just basically like `after` except that this raises an error if `method_name` is not defined or not yet defined (at the time `after!` is called). This works perfect with `attr_accesors` I normally put them at the top of the lines of a Class, and so I can now safely call `before!` or `after!` because I am sure that I already defined everything I needed to define. If I forgot something then, this `before!` would raise an error and alert me, and not silently failing. Helps debugging :)*
|
363
|
+
|
364
|
+
*Above uses `after!`, but works similarly with `before!`*
|
327
365
|
|
328
366
|
## TODOs
|
329
367
|
* when the need already arises, implement `around` (If you have ideas or want to help this part, please feel free to fork or send me a message! :)
|
@@ -344,7 +382,7 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
344
382
|
|
345
383
|
## Changelog
|
346
384
|
|
347
|
-
* 1.0.
|
385
|
+
* 1.0.3 (2019-08-12)
|
348
386
|
* Cleaner code without explicitly calling `run_callbacks` anymore; done now because of ruby upgrade from 1.9 to 2.0+ which already supports `prepend`
|
349
387
|
* Supported both class and instance level callbacks
|
350
388
|
* Supported inherited callbacks
|
data/lib/super_callbacks.rb
CHANGED
@@ -4,6 +4,9 @@ module SuperCallbacks
|
|
4
4
|
VALID_OPTION_KEYS = [:if].freeze
|
5
5
|
|
6
6
|
def self.included(base)
|
7
|
+
# prevent re-including
|
8
|
+
return if base.ancestors.detect { |ancestor| ancestor.is_a? SuperCallbacks::Prepended }
|
9
|
+
|
7
10
|
base.singleton_class.send :attr_accessor, *[:before_callbacks, :after_callbacks]
|
8
11
|
base.send :attr_accessor, *[:before_callbacks, :after_callbacks]
|
9
12
|
base.extend ClassMethods
|
@@ -39,14 +42,14 @@ module SuperCallbacks
|
|
39
42
|
end
|
40
43
|
|
41
44
|
module ClassAndInstanceMethods
|
42
|
-
def before!(method_name, *remaining_args)
|
45
|
+
def before!(method_name, *remaining_args, &callback_proc)
|
43
46
|
raise ArgumentError, "`#{method_name}` is not or not yet defined for #{self}" unless method_defined? method_name
|
44
|
-
before(method_name, *remaining_args)
|
47
|
+
before(method_name, *remaining_args, &callback_proc)
|
45
48
|
end
|
46
49
|
|
47
|
-
def after!(method_name, *remaining_args)
|
50
|
+
def after!(method_name, *remaining_args, &callback_proc)
|
48
51
|
raise ArgumentError, "`#{method_name}` is not or not yet defined for #{self}" unless method_defined? method_name
|
49
|
-
|
52
|
+
after(method_name, *remaining_args, &callback_proc)
|
50
53
|
end
|
51
54
|
|
52
55
|
def before(method_name, callback_method_name = nil, options = {}, &callback_proc)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: super_callbacks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jules Roman B. Polidario
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-08-
|
11
|
+
date: 2019-08-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|