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 +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/lib/invokable/command.rb +60 -9
- data/lib/invokable/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a5eab3dc64de36ef9f40927b0d4fa4d68fb0cc1cf867a2f94effa90a0872027
|
4
|
+
data.tar.gz: 1fdc343357e9931e7dc5d62e71712159178cd44236ce6d50f7d37a88574d1403
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0760094c175a2e23824bd07ef6830528b09d5ad3f331d1c07dcf35ab2a635d08c9ee3172ff3ef6c7edb0fc83d7d8da36dbd8e76417acfb700f940c6d962c79db'
|
7
|
+
data.tar.gz: 278f13b9c4126e75704cffc87f472a2607acfeeabf10c3bd754f22ede54c5c1f2b073aaf399cb9f90f7085ce39bde425da04828648fc98386c1087b0a15e3a10
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/lib/invokable/command.rb
CHANGED
@@ -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 "
|
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
|
data/lib/invokable/version.rb
CHANGED