invokable 0.5.0 → 0.5.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6dcc085ff857ef0a95578db5f816338c0ade3da6ef4ecb263f86924e4b4e2cfb
4
- data.tar.gz: 235f058b7b39b8ff91b8d2580281fad56a388fcea46d19ae3a4bc3657c913d1c
3
+ metadata.gz: 0a5eab3dc64de36ef9f40927b0d4fa4d68fb0cc1cf867a2f94effa90a0872027
4
+ data.tar.gz: 1fdc343357e9931e7dc5d62e71712159178cd44236ce6d50f7d37a88574d1403
5
5
  SHA512:
6
- metadata.gz: bc8294c008fedd2a69337d886515b8fb9c3a2dd6fea8330b15371ffa9b44db0a5ce3c679badede4e2455230db25845ac723b11aa6736f9b62d690b462f7623e9
7
- data.tar.gz: f6cab459416371d902933ee1ef3fd24f935bcc0ad7b6c9e0a9bd261bf95d8736c763ec416b94b063acf52fbc4a08df04586ac4b6bde8c02aedcb47b8f8074ef9
6
+ metadata.gz: '0760094c175a2e23824bd07ef6830528b09d5ad3f331d1c07dcf35ab2a635d08c9ee3172ff3ef6c7edb0fc83d7d8da36dbd8e76417acfb700f940c6d962c79db'
7
+ data.tar.gz: 278f13b9c4126e75704cffc87f472a2607acfeeabf10c3bd754f22ede54c5c1f2b073aaf399cb9f90f7085ce39bde425da04828648fc98386c1087b0a15e3a10
@@ -5,3 +5,7 @@
5
5
  - `invokable/array` is no longer loaded with `invokable/data`.
6
6
  This created a bit of havok in a few places. Including breaking
7
7
  puma bootup in Rails 5.2.4.1.
8
+
9
+ ## 0.5.0
10
+
11
+ - Added `Invokable::Command` and `Invokable::Core#arity`
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- invokable (0.5.0)
4
+ invokable (0.5.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -31,17 +31,11 @@ module Invokable
31
31
  #
32
32
  # @return [Integer]
33
33
  def initializer_arity
34
+ return @initializer_arity if @initializer_arity
35
+
34
36
  @initializer ? @initializer.arity : 0
35
37
  end
36
38
 
37
- # To specify any enclosed state
38
- def enclose(&block)
39
- raise 'A block is required' if block.nil?
40
-
41
- @initializer = block
42
- define_method :initialize, &block
43
- end
44
-
45
39
  # Handle automatic currying--will accept either the initializer arity or the total arity of the class. If
46
40
  # the initializer arity is used return a class instance. If the total arity is used instantiate the class
47
41
  # and return the results of the `call` method.
@@ -57,7 +51,64 @@ module Invokable
57
51
  call_args = args.slice(initializer_arity, args.length)
58
52
  new(*init_args).call(*call_args)
59
53
  else
60
- raise "Wrong number of arguments expected #{initializer_arity} or #{arity}, got: #{args.length}"
54
+ raise ArgumentError, "wrong number of arguments (given #{args.length}, expected #{initializer_arity} or #{arity})"
55
+ end
56
+ end
57
+
58
+ # Specify any enclosed state with a block or named attributes
59
+ #
60
+ # @example
61
+ # class TwitterPater
62
+ # include Invokable::Command
63
+ #
64
+ # enclose :api_key
65
+ #
66
+ # def call(user)
67
+ # # interact with twitter, return results
68
+ # end
69
+ # end
70
+ #
71
+ # TwitterPater.new(API_KEY).call(User.find(1))
72
+ # TwitterPater.new(API_KEY).api_key == API_KEY # => true
73
+ #
74
+ # class TwitterPater
75
+ # include Invokable::Command
76
+ #
77
+ # enclose do |api_key|
78
+ # @api_key = api_key
79
+ # end
80
+ #
81
+ # def call(user)
82
+ # # interact with twitter, return results
83
+ # end
84
+ # end
85
+ #
86
+ # TwitterPater.new(API_KEY).call(User.find(1))
87
+ # TwitterPater.new(API_KEY).api_key # error 'method' missing
88
+ def enclose(*names, &block)
89
+ return define_initializer_with_block(block) unless block.nil?
90
+
91
+ define_initializer_with_names(names)
92
+ end
93
+
94
+ private
95
+
96
+ def define_initializer_with_block(block)
97
+ @initializer = block
98
+ define_method :initialize, &block
99
+ end
100
+
101
+ def define_initializer_with_names(names)
102
+ @initializer_arity = names.length
103
+
104
+ names.each do |name|
105
+ attr_reader name
106
+ end
107
+
108
+ define_method :initialize do |*args|
109
+ names.each_with_index do |name, i|
110
+ instance_variable_set(:"@#{name}", args[i])
111
+ end
61
112
  end
62
113
  end
63
114
  end
@@ -1,3 +1,3 @@
1
1
  module Invokable
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: invokable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Delon Newman