thor-plus 0.1.0 → 0.2.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
  SHA1:
3
- metadata.gz: 65643a66eb29ec071d58e0df7c191b5230d6fc52
4
- data.tar.gz: 5cb5b49921fb2a1c29526d9b6891c176ab53e03a
3
+ metadata.gz: f483251a5a8dc7d64817f01855dd09a1f3073de4
4
+ data.tar.gz: 49de3b45cc64696aa9e98810508224fafddc2a5d
5
5
  SHA512:
6
- metadata.gz: 83bd49b2ed6a9c62f805fda556f933b3457c3acd98dd9bb87e7a436644514f5471bc09f4ffca1b925569df9c2015bd3071eddae15ff170bfc1720480f577726f
7
- data.tar.gz: fe8320dd9d0447dd8ecede00cd745cafe58c445b1d734af36ca765d644235211fd745e3aefaf0ae77fe5b33597a6ca358d86c3044d85201ec9722812bd0fc1b9
6
+ metadata.gz: c7fb604ecf73e9d478625b053a8353cc2f2e763d6dd1b6b749e2b84df262713531e7b22dc0d45764c788bea252ef2762afc892ff0fb3f645c31a10b5ffc2f159
7
+ data.tar.gz: bae1a232716f10d9243bdefa1e35f42bbc8d59d99bfee0883853a2d3955385b3905698c64f621ca7047282ca9e23b502380a022f1500929843e5c6509229d926
@@ -27,6 +27,39 @@ class Thor # rubocop:disable ClassLength
27
27
  end
28
28
  alias_method :default_task, :default_command
29
29
 
30
+ # Adds a callback method that should be executed before a command is executed.
31
+ #
32
+ # ==== Parameters
33
+ # methods<*String|Symbol>:: Callbacks to execute before command runs.
34
+ def before_command(*methods)
35
+ methods.each { |m| register_callback(:before, m) }
36
+ end
37
+ alias_method :before_task, :before_command
38
+
39
+ # Adds a callback method that should be executed after a task is executed.
40
+ #
41
+ # ==== Parameters
42
+ # methods<*String|Symbol>:: Callbacks to execute after command runs.
43
+ def after_command(*methods)
44
+ methods.each { |m| register_callback(:after, m) }
45
+ end
46
+ alias_method :after_task, :after_command
47
+
48
+ # All callbacks defined for this class & superclass.
49
+ def callbacks
50
+ @callbacks ||= from_superclass(:callbacks, {})
51
+ end
52
+
53
+ # Register a callback of generic type.
54
+ #
55
+ # ==== Parameters
56
+ # type<Symbol>:: Type of callback to register.
57
+ # name<Symbol>:: Name of callback command to run.
58
+ def register_callback(type, name)
59
+ callbacks[type.to_sym] ||= []
60
+ callbacks[type.to_sym] << name.to_sym
61
+ end
62
+
30
63
  # Registers another Thor subclass as a command.
31
64
  #
32
65
  # ==== Parameters
@@ -84,6 +84,15 @@ class Thor
84
84
  @args = thor_args.remaining
85
85
  end
86
86
 
87
+ # Run callbacks by type.
88
+ #
89
+ # ==== Parameters
90
+ # type<Symbol>:: Type of callbacks to run.
91
+ def run_callbacks(type)
92
+ callbacks = self.class.callbacks[type.to_sym] || []
93
+ callbacks.each { |c| __send__(c) }
94
+ end
95
+
87
96
  class << self
88
97
  def included(base) #:nodoc:
89
98
  base.extend ClassMethods
@@ -24,7 +24,10 @@ class Thor
24
24
  instance.class.handle_no_command_error(name)
25
25
  elsif public_method?(instance)
26
26
  arity = instance.method(name).arity
27
+
28
+ instance.run_callbacks(:before)
27
29
  instance.__send__(name, *args)
30
+ instance.run_callbacks(:after)
28
31
  elsif local_method?(instance, :method_missing)
29
32
  instance.__send__(:method_missing, name.to_sym, *args)
30
33
  else
@@ -1,3 +1,3 @@
1
1
  class Thor
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thor-plus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yehuda Katz
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-06-08 00:00:00.000000000 Z
13
+ date: 2016-06-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler