invokable 0.5.2 → 0.6.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 +4 -4
- data/CHANGELOG.md +8 -0
- data/Gemfile.lock +1 -1
- data/README.md +6 -6
- data/lib/invokable.rb +41 -0
- data/lib/invokable/closure.rb +2 -3
- data/lib/invokable/command.rb +1 -0
- data/lib/invokable/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1c89f44b80135945cce3300f6f9b8e5b1210282d8c3d10256c2593ccf281948
|
4
|
+
data.tar.gz: 2a0e9ce82c10b9f57b1767087d56b5d53a67bdfeb139e5a642fb071a9bc6a8e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 20d8a431d84d973709f122211c8e606d4ac43e162c54a7bdf8e7507873c0235dede63a8e73a69d296152b6020ebd93a3001be8c73916dee7a7cfbcc6c600b0df
|
7
|
+
data.tar.gz: c1df682d8c7eed2525d7a2eaadce3e5dedb8114b18d64df3f7e323995b7c30c31827d2c6f2da94738094c06614a74bf4309dc20a1d3a2e9b66152c6e265b7035
|
data/CHANGELOG.md
CHANGED
@@ -9,3 +9,11 @@
|
|
9
9
|
## 0.5.0
|
10
10
|
|
11
11
|
- Added `Invokable::Command` and `Invokable::Core#arity`
|
12
|
+
|
13
|
+
## 0.5.2
|
14
|
+
|
15
|
+
- `Invokable::Command` deprecated in favor of `Invokable::Closure`.
|
16
|
+
|
17
|
+
## 0.6.0
|
18
|
+
|
19
|
+
- `Invokable::Closure` deprecated comparable behavior has been added to `Invokable` itself.
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -49,12 +49,13 @@ User.all.map(&data_for_user)
|
|
49
49
|
```ruby
|
50
50
|
# command objects that enclose state, can be treated as automatically curried functions.
|
51
51
|
require 'invokable'
|
52
|
-
require 'invokable/closure'
|
53
52
|
|
54
53
|
class TwitterPoster
|
55
|
-
include Invokable
|
54
|
+
include Invokable
|
56
55
|
|
57
|
-
|
56
|
+
def initialize(model)
|
57
|
+
@model = model
|
58
|
+
end
|
58
59
|
|
59
60
|
def call(user)
|
60
61
|
# do the dirt
|
@@ -66,16 +67,15 @@ end
|
|
66
67
|
TwitterPoster.call(Model.find(1)) # => #<TwitterPoster ...>
|
67
68
|
TwitterPoster.call(Model.find(1), current_user) # => #<TwitterStatus ...>
|
68
69
|
|
69
|
-
# both the class and it's instances can be used
|
70
|
+
# both the class and it's instances can be used anywhere Procs are.
|
70
71
|
|
71
|
-
Model.where(created_at: Date.today).map(
|
72
|
+
Model.where(created_at: Date.today).map(&TwitterPoster) # => [#<TwitterPoster ...>, ...]
|
72
73
|
```
|
73
74
|
|
74
75
|
Use as much or a little as you need:
|
75
76
|
|
76
77
|
```ruby
|
77
78
|
require 'invokable' # loads Invokable module
|
78
|
-
require 'invokable/closure' # loads Invokable::Closure module
|
79
79
|
require 'invokable/hash' # loads hash patch
|
80
80
|
require 'invokable/array' # loads array patch
|
81
81
|
require 'invokable/set' # loads set patch
|
data/lib/invokable.rb
CHANGED
@@ -12,5 +12,46 @@ module Invokable
|
|
12
12
|
def self.included(base)
|
13
13
|
base.include(Invokable::Core)
|
14
14
|
base.include(Invokable::Compose)
|
15
|
+
base.extend(Invokable::Core)
|
16
|
+
base.extend(Invokable::Compose)
|
17
|
+
base.extend(ClassMethods)
|
18
|
+
end
|
19
|
+
|
20
|
+
module ClassMethods
|
21
|
+
# Return the "total" arity of the class (i.e. the arity of the initializer and the arity of the call method)
|
22
|
+
#
|
23
|
+
# @version 0.6.0
|
24
|
+
# @see https://ruby-doc.org/core-2.7.1/Proc.html#method-i-arity Proc#arity
|
25
|
+
# @see initializer_arity
|
26
|
+
#
|
27
|
+
# @return [Integer]
|
28
|
+
def arity
|
29
|
+
initializer_arity + instance_method(:call).arity
|
30
|
+
end
|
31
|
+
|
32
|
+
# Handle automatic currying--will accept either the initializer arity or the total arity of the class. If
|
33
|
+
# the initializer arity is used return a class instance. If the total arity is used instantiate the class
|
34
|
+
# and return the results of the `call` method.
|
35
|
+
#
|
36
|
+
# @version 0.6.0
|
37
|
+
# @see arity
|
38
|
+
# @see initializer_arity
|
39
|
+
def call(*args)
|
40
|
+
if args.length == initializer_arity
|
41
|
+
new(*args)
|
42
|
+
elsif args.length == arity
|
43
|
+
init_args = args.slice(0, initializer_arity)
|
44
|
+
call_args = args.slice(initializer_arity, args.length)
|
45
|
+
new(*init_args).call(*call_args)
|
46
|
+
else
|
47
|
+
raise ArgumentError, "wrong number of arguments (given #{args.length}, expected #{initializer_arity} or #{arity})"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def initializer_arity
|
54
|
+
instance_method(:initialize).arity
|
55
|
+
end
|
15
56
|
end
|
16
57
|
end
|
data/lib/invokable/closure.rb
CHANGED
@@ -4,6 +4,7 @@ module Invokable
|
|
4
4
|
# @see https://ruby-doc.org/core-2.7.0/Proc.html#method-i-curry Proc#curry
|
5
5
|
#
|
6
6
|
# @version 0.5.2
|
7
|
+
# @deprecated These features are included in the Invokable by default now.
|
7
8
|
module Closure
|
8
9
|
def self.included(klass)
|
9
10
|
klass.include(Invokable)
|
@@ -31,9 +32,7 @@ module Invokable
|
|
31
32
|
#
|
32
33
|
# @return [Integer]
|
33
34
|
def initializer_arity
|
34
|
-
|
35
|
-
|
36
|
-
@initializer ? @initializer.arity : 0
|
35
|
+
instance_method(:initialize).arity
|
37
36
|
end
|
38
37
|
|
39
38
|
# Handle automatic currying--will accept either the initializer arity or the total arity of the class. If
|
data/lib/invokable/command.rb
CHANGED
data/lib/invokable/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: invokable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Delon Newman
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-08-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|