hanami-cli 0.2.0.beta1 → 0.2.0.beta2

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: a068faee4ac4b225d048b6552c2f3c996dcebaeb2c4b45e7496882bd1633507b
4
- data.tar.gz: 51c34e8bd8306949cf344507bbf906a7876e47d5d39a26b70e94ad78e4191f0f
3
+ metadata.gz: 5c3fd112b3dc098cba00e1ef4b617afb7b32194584f9108a2de44e94be16d1d2
4
+ data.tar.gz: 034f35aff9557f17a1f28353f177ced498ee1479d631584e9fbc55eeee007f5b
5
5
  SHA512:
6
- metadata.gz: 5c5817a42a314f553bc42b24f04e9a574e44efae82d462721ff9c27bb37ab4ef3c9096156a6166e94ce11728d1e6f0e4a009b8feb3e48e0a670e034e3e5c6d16
7
- data.tar.gz: 349d5bcc6d5214feb852a01cbf57a185a8448072f1590e6160a4878cb550a80361c7abbec463cd787ed621c80046322351ec67d611f59f19ac2423ba4beb3176
6
+ metadata.gz: e09e61536293649735e70ffcffcd2868d1c41754055ab6e56ac8cfaf2e6c6e0b9dd70e10bdc5906078e821264103c3aea96bc6c498c1f52e9fd79df08e931bbf
7
+ data.tar.gz: 2320bac7acf1ff217a97e5b2f27810b5d939488961ed6668e8791793c75f4c33ea1720b56b1fbd95ae1cf362bbbd5ffd5cf4507695872fd0401a3f590e4949d2
data/CHANGELOG.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # Hanami::CLI
2
2
  General purpose Command Line Interface (CLI) framework for Ruby
3
3
 
4
+ ## v0.2.0.beta2 - 2018-03-23
5
+ ### Added
6
+ - [Anton Davydov & Luca Guidi] Support objects as callbacks
7
+
8
+ ### Fixed
9
+ - [Anton Davydov & Luca Guidi] Ensure callbacks' context of execution (aka `self`) to be the command that is being executed
10
+
4
11
  ## v0.2.0.beta1 - 2018-02-28
5
12
  ### Added
6
13
  - [Anton Davydov] Register `before`/`after` callbacks for commands
data/Gemfile CHANGED
@@ -6,6 +6,6 @@ unless ENV['TRAVIS']
6
6
  gem 'yard', require: false
7
7
  end
8
8
 
9
- gem 'hanami-utils', '1.2.0.beta1', require: false, git: 'https://github.com/hanami/utils.git', branch: 'develop'
9
+ gem 'hanami-utils', '1.2.0.beta2', require: false, git: 'https://github.com/hanami/utils.git', branch: 'develop'
10
10
 
11
11
  gem 'hanami-devtools', require: false, git: 'https://github.com/hanami/devtools.git'
data/hanami-cli.gemspec CHANGED
@@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
25
25
  f.match(%r{^(test|spec|features)/})
26
26
  end
27
27
 
28
- spec.add_dependency "hanami-utils", "1.2.0.beta1"
28
+ spec.add_dependency "hanami-utils", "1.2.0.beta2"
29
29
  spec.add_dependency "concurrent-ruby", "~> 1.0"
30
30
 
31
31
  spec.add_development_dependency "bundler"
data/lib/hanami/cli.rb CHANGED
@@ -53,9 +53,9 @@ module Hanami
53
53
  if result.found?
54
54
  command, args = parse(result, out)
55
55
 
56
- result.before_callbacks.run(self, args)
56
+ result.before_callbacks.run(command, args)
57
57
  command.call(args)
58
- result.after_callbacks.run(self, args)
58
+ result.after_callbacks.run(command, args)
59
59
  else
60
60
  usage(result, out)
61
61
  end
@@ -12,5 +12,21 @@ module Hanami
12
12
  super("unknown command: `#{command_name}'")
13
13
  end
14
14
  end
15
+
16
+ # @since 0.2.0
17
+ class InvalidCallbackError < Error
18
+ # @since 0.2.0
19
+ # @api private
20
+ def initialize(callback)
21
+ message = case callback
22
+ when Class
23
+ "expected `#{callback.inspect}' to respond to `#initialize' with arity 0"
24
+ else
25
+ "expected `#{callback.inspect}' to respond to `#call'"
26
+ end
27
+
28
+ super(message)
29
+ end
30
+ end
15
31
  end
16
32
  end
@@ -83,9 +83,13 @@ module Hanami
83
83
  # Register a before callback.
84
84
  #
85
85
  # @param command_name [String] the name used for command registration
86
- # @param callback [Proc] the callback
86
+ # @param callback [Class, #call] the callback object. If a class is given,
87
+ # it MUST respond to `#call`.
88
+ # @param blk [Proc] the callback espressed as a block
87
89
  #
88
90
  # @raise [Hanami::CLI::UnkwnownCommandError] if the command isn't registered
91
+ # @raise [Hanami::CLI::InvalidCallbackError] if the given callback doesn't
92
+ # implement the required interface
89
93
  #
90
94
  # @since 0.2.0
91
95
  #
@@ -106,16 +110,72 @@ module Hanami
106
110
  # before "hello", -> { puts "I'm about to say.." }
107
111
  # end
108
112
  # end
109
- def before(command_name, &callback)
110
- command(command_name).before_callbacks.append(&callback)
113
+ #
114
+ # @example Register an object as callback
115
+ # require "hanami/cli"
116
+ #
117
+ # module Callbacks
118
+ # class Hello
119
+ # def call(*)
120
+ # puts "world"
121
+ # end
122
+ # end
123
+ # end
124
+ #
125
+ # module Foo
126
+ # module Commands
127
+ # extend Hanami::CLI::Registry
128
+ #
129
+ # class Hello < Hanami::CLI::Command
130
+ # def call(*)
131
+ # puts "I'm about to say.."
132
+ # end
133
+ # end
134
+ #
135
+ # register "hello", Hello
136
+ # before "hello", Callbacks::Hello.new
137
+ # end
138
+ # end
139
+ #
140
+ # @example Register a class as callback
141
+ # require "hanami/cli"
142
+ #
143
+ # module Callbacks
144
+ # class Hello
145
+ # def call(*)
146
+ # puts "world"
147
+ # end
148
+ # end
149
+ # end
150
+ #
151
+ # module Foo
152
+ # module Commands
153
+ # extend Hanami::CLI::Registry
154
+ #
155
+ # class Hello < Hanami::CLI::Command
156
+ # def call(*)
157
+ # puts "I'm about to say.."
158
+ # end
159
+ # end
160
+ #
161
+ # register "hello", Hello
162
+ # before "hello", Callbacks::Hello
163
+ # end
164
+ # end
165
+ def before(command_name, callback = nil, &blk)
166
+ command(command_name).before_callbacks.append(&_callback(callback, blk))
111
167
  end
112
168
 
113
169
  # Register an after callback.
114
170
  #
115
171
  # @param command_name [String] the name used for command registration
116
- # @param callback [Proc] the callback
172
+ # @param callback [Class, #call] the callback object. If a class is given,
173
+ # it MUST respond to `#call`.
174
+ # @param blk [Proc] the callback espressed as a block
117
175
  #
118
176
  # @raise [Hanami::CLI::UnkwnownCommandError] if the command isn't registered
177
+ # @raise [Hanami::CLI::InvalidCallbackError] if the given callback doesn't
178
+ # implement the required interface
119
179
  #
120
180
  # @since 0.2.0
121
181
  #
@@ -136,8 +196,60 @@ module Hanami
136
196
  # after "hello", -> { puts "world" }
137
197
  # end
138
198
  # end
139
- def after(command_name, &callback)
140
- command(command_name).after_callbacks.append(&callback)
199
+ #
200
+ # @example Register an object as callback
201
+ # require "hanami/cli"
202
+ #
203
+ # module Callbacks
204
+ # class World
205
+ # def call(*)
206
+ # puts "world"
207
+ # end
208
+ # end
209
+ # end
210
+ #
211
+ # module Foo
212
+ # module Commands
213
+ # extend Hanami::CLI::Registry
214
+ #
215
+ # class Hello < Hanami::CLI::Command
216
+ # def call(*)
217
+ # puts "hello"
218
+ # end
219
+ # end
220
+ #
221
+ # register "hello", Hello
222
+ # after "hello", Callbacks::World.new
223
+ # end
224
+ # end
225
+ #
226
+ # @example Register a class as callback
227
+ # require "hanami/cli"
228
+ #
229
+ # module Callbacks
230
+ # class World
231
+ # def call(*)
232
+ # puts "world"
233
+ # end
234
+ # end
235
+ # end
236
+ #
237
+ # module Foo
238
+ # module Commands
239
+ # extend Hanami::CLI::Registry
240
+ #
241
+ # class Hello < Hanami::CLI::Command
242
+ # def call(*)
243
+ # puts "hello"
244
+ # end
245
+ # end
246
+ #
247
+ # register "hello", Hello
248
+ # after "hello", Callbacks::World
249
+ # end
250
+ # end
251
+ def after(command_name, callback = nil, &blk)
252
+ command(command_name).after_callbacks.append(&_callback(callback, blk))
141
253
  end
142
254
 
143
255
  # @since 0.1.0
@@ -158,6 +270,28 @@ module Hanami
158
270
  end
159
271
  end
160
272
 
273
+ # @since 0.2.0
274
+ # @api private
275
+ #
276
+ # rubocop:disable Metrics/MethodLength
277
+ def _callback(callback, blk)
278
+ return blk if blk.respond_to?(:to_proc)
279
+
280
+ case callback
281
+ when ->(c) { c.respond_to?(:call) }
282
+ callback.method(:call)
283
+ when Class
284
+ begin
285
+ _callback(callback.new, blk)
286
+ rescue ArgumentError
287
+ raise InvalidCallbackError.new(callback)
288
+ end
289
+ else
290
+ raise InvalidCallbackError.new(callback)
291
+ end
292
+ end
293
+ # rubocop:enable Metrics/MethodLength
294
+
161
295
  # Command name prefix
162
296
  #
163
297
  # @since 0.1.0
@@ -1,6 +1,6 @@
1
1
  module Hanami
2
2
  class CLI
3
3
  # @since 0.1.0
4
- VERSION = "0.2.0.beta1".freeze
4
+ VERSION = "0.2.0.beta2".freeze
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanami-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0.beta1
4
+ version: 0.2.0.beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-28 00:00:00.000000000 Z
11
+ date: 2018-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hanami-utils
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 1.2.0.beta1
19
+ version: 1.2.0.beta2
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 1.2.0.beta1
26
+ version: 1.2.0.beta2
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: concurrent-ruby
29
29
  requirement: !ruby/object:Gem::Requirement