invokable 0.5.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b107ca53f8d89104fa38152d27141d24ae62dbec10e8e313b1428b5e711f61e9
4
- data.tar.gz: b17e3b4207c4262637a08975772063f8d99583c6ef472220e18598885c272860
3
+ metadata.gz: b1c89f44b80135945cce3300f6f9b8e5b1210282d8c3d10256c2593ccf281948
4
+ data.tar.gz: 2a0e9ce82c10b9f57b1767087d56b5d53a67bdfeb139e5a642fb071a9bc6a8e6
5
5
  SHA512:
6
- metadata.gz: c036b555078552a8902b83ccb7800ce878c23844a3ac6c7586d510622a56ba9ef0e4da985773662d5a605566308bb1c835b28eb988b417cc5a42a1c92aa95464
7
- data.tar.gz: 900fab835907e7a8af2f8284629361ed0fbe1db81e6d05d8a2a98152fc223e4a44de352eb1eba538a9e53d406e95d97bcf4ad82f133a96e91c282626999175a7
6
+ metadata.gz: 20d8a431d84d973709f122211c8e606d4ac43e162c54a7bdf8e7507873c0235dede63a8e73a69d296152b6020ebd93a3001be8c73916dee7a7cfbcc6c600b0df
7
+ data.tar.gz: c1df682d8c7eed2525d7a2eaadce3e5dedb8114b18d64df3f7e323995b7c30c31827d2c6f2da94738094c06614a74bf4309dc20a1d3a2e9b66152c6e265b7035
@@ -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.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- invokable (0.5.2)
4
+ invokable (0.6.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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::Closure
54
+ include Invokable
56
55
 
57
- enclose :model
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 any where Procs are.
70
+ # both the class and it's instances can be used anywhere Procs are.
70
71
 
71
- Model.where(created_at: Date.today).map(&:TwitterPoster) # => [#<TwitterPoster ...>, ...]
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
@@ -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
@@ -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
- return @initializer_arity if @initializer_arity
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
@@ -6,6 +6,7 @@ module Invokable
6
6
  # @see https://ruby-doc.org/core-2.7.0/Proc.html#method-i-curry Proc#curry
7
7
  #
8
8
  # @version 0.5.0
9
+ # @deprecated These features are included in the Invokable by default now.
9
10
  module Command
10
11
  def self.included(klass)
11
12
  klass.include(Invokable)
@@ -1,3 +1,3 @@
1
1
  module Invokable
2
- VERSION = "0.5.2"
2
+ VERSION = "0.6.0"
3
3
  end
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.5.2
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-07-28 00:00:00.000000000 Z
11
+ date: 2020-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler