insane_hook 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +27 -3
- data/lib/insane_hook.rb +34 -23
- data/lib/insane_hook/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a61856275cac8c83baf51f5dbe739af495c2086
|
4
|
+
data.tar.gz: 04f39eb108182f93f346afbd2c164ec36a421fda
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 576f92cee691387c45cc6f06971ade6c8eb2c7113ae14a90e277d732266eae6b2a01656b3d24cc4805e67f7ee421b2e03f40825fbf657937c00206ce2f2a8933
|
7
|
+
data.tar.gz: 40c9e53c80c015a4cf4646449ac7dd41629205e5045ced29ab172ad8967f5139f267334ca2c83a2d9a87dd2faa054fcb86718e2bf0a0de7c9059c25a4217401b
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -30,12 +30,36 @@ class YeOldeTaske
|
|
30
30
|
# attr_readers are available for:
|
31
31
|
# - some_required_arg
|
32
32
|
# - some_optional_arg
|
33
|
-
result
|
33
|
+
result "meaningful value"
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
-
|
38
|
-
|
37
|
+
YeOldeTaske.new # => InsaneHook::MissingArgumentError
|
38
|
+
|
39
|
+
task = YeOldeTaske.new(some_required_arg: "input") # => YeOldeTaske instance
|
40
|
+
task.result # => raises InsaneHook::CommandNotRunError
|
41
|
+
task.call # => YeOldeTaske instance
|
42
|
+
task.result # => "meaningful value"
|
43
|
+
```
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
class YeOldeTaske
|
47
|
+
include InsaneHook
|
48
|
+
need :some_required_arg
|
49
|
+
allow :some_optional_arg
|
50
|
+
|
51
|
+
call do
|
52
|
+
# attr_readers are available for:
|
53
|
+
# - some_required_arg
|
54
|
+
# - some_optional_arg
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
task = YeOldeTaske.new(some_required_arg: 7).call
|
59
|
+
task.result # => nil
|
60
|
+
|
61
|
+
# Also
|
62
|
+
task = YeOldeTaske.call(some_required_arg: 7)
|
39
63
|
```
|
40
64
|
|
41
65
|
## Development
|
data/lib/insane_hook.rb
CHANGED
@@ -1,61 +1,72 @@
|
|
1
1
|
require "insane_hook/version"
|
2
2
|
|
3
3
|
module InsaneHook
|
4
|
-
|
4
|
+
ARGS_VAR = :@@_command
|
5
|
+
REQUIRED_ARGS = :required
|
6
|
+
OPTIONAL_ARGS = :optional
|
7
|
+
RESULT_VAR = :@_result
|
8
|
+
NO_ARG = :_no_value
|
5
9
|
class CommandNotRunError < StandardError ; end
|
10
|
+
class MissingArgumentError < StandardError ; end
|
6
11
|
|
7
12
|
def self.included(mod)
|
8
|
-
mod.class_variable_set(
|
13
|
+
mod.class_variable_set(::InsaneHook::ARGS_VAR, {REQUIRED_ARGS => [], OPTIONAL_ARGS => []})
|
9
14
|
mod.extend(ClassMethods)
|
10
15
|
mod.define_singleton_method(:need, mod.instance_method(:_need))
|
11
16
|
mod.define_singleton_method(:allow, mod.instance_method(:_allow))
|
12
17
|
mod.define_singleton_method(:call, mod.instance_method(:_call))
|
13
|
-
#mod.define_method(:result, mod.instance_method(:_result))
|
14
18
|
end
|
15
19
|
|
16
|
-
def result(value
|
17
|
-
if value ==
|
18
|
-
if instance_variable_defined?(
|
19
|
-
instance_variable_get(
|
20
|
+
def result(value=NO_ARG)
|
21
|
+
if value == NO_ARG
|
22
|
+
if instance_variable_defined?(::InsaneHook::RESULT_VAR)
|
23
|
+
instance_variable_get(::InsaneHook::RESULT_VAR)
|
20
24
|
else
|
21
25
|
raise CommandNotRunError
|
22
26
|
end
|
23
27
|
else
|
24
|
-
instance_variable_set(
|
28
|
+
instance_variable_set(::InsaneHook::RESULT_VAR, value)
|
25
29
|
end
|
26
30
|
end
|
27
31
|
|
28
32
|
def _need(key)
|
29
33
|
fail "#{key} is not a symbol" unless key.is_a? Symbol
|
30
|
-
args = self.class_variable_get(
|
31
|
-
args[
|
32
|
-
self.class_variable_set(
|
34
|
+
args = self.class_variable_get(::InsaneHook::ARGS_VAR)
|
35
|
+
args[REQUIRED_ARGS] << key
|
36
|
+
self.class_variable_set(::InsaneHook::ARGS_VAR, args)
|
33
37
|
end
|
34
38
|
|
35
39
|
def _allow(key)
|
36
40
|
fail "#{key} is not a symbol" unless key.is_a? Symbol
|
37
|
-
args = self.class_variable_get(
|
38
|
-
args[
|
39
|
-
self.class_variable_set(
|
41
|
+
args = self.class_variable_get(::InsaneHook::ARGS_VAR)
|
42
|
+
args[OPTIONAL_ARGS] << key
|
43
|
+
self.class_variable_set(::InsaneHook::ARGS_VAR, args)
|
40
44
|
end
|
41
45
|
|
42
|
-
def _call(&block)
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
46
|
+
def _call(**args, &block)
|
47
|
+
if block_given?
|
48
|
+
raise "Block cannot take arguments" if block.arity > 0
|
49
|
+
raise "call method already defined" if self.instance_methods.include?(:call)
|
50
|
+
define_method(:call) do
|
51
|
+
result(nil)
|
52
|
+
instance_eval(&block)
|
53
|
+
self
|
54
|
+
end
|
55
|
+
else
|
56
|
+
new(**args).call
|
47
57
|
end
|
58
|
+
|
48
59
|
end
|
49
60
|
|
50
61
|
module ClassMethods
|
51
|
-
def new(**args
|
62
|
+
def new(**args)
|
52
63
|
obj = self.allocate
|
53
|
-
self.class_variable_get(
|
54
|
-
value = args.fetch(var) { raise "#{var} not provided in #{self.class}" }
|
64
|
+
self.class_variable_get(::InsaneHook::ARGS_VAR)[REQUIRED_ARGS].each do |var|
|
65
|
+
value = args.fetch(var) { raise(::InsaneHook::MissingArgumentError, "#{var} not provided in #{self.class}") }
|
55
66
|
obj.instance_variable_set("@#{var}", value)
|
56
67
|
obj.class.class_eval{attr_reader var}
|
57
68
|
end
|
58
|
-
self.class_variable_get(
|
69
|
+
self.class_variable_get(::InsaneHook::ARGS_VAR)[OPTIONAL_ARGS].each do |var|
|
59
70
|
value = args.fetch(var, nil)
|
60
71
|
obj.instance_variable_set("@#{var}", value)
|
61
72
|
obj.class.class_eval{attr_reader var}
|
data/lib/insane_hook/version.rb
CHANGED