insane_hook 0.4.0 → 0.5.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 → README.org} +51 -41
- data/lib/insane_hook.rb +9 -9
- data/lib/insane_hook/class_methods.rb +14 -9
- data/lib/insane_hook/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af161e7e0ea7b92529311bdd6a93cf3c5be9025c35a34176cbbded0310818653
|
4
|
+
data.tar.gz: 36e3b3f6538c3374323080ec1cd7d87e4620862a5f043c77b087b5c66844beca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 609cf7608e901df5d52773072a61bcd660fe2824ae58e2abd79d92334e74643de878700ab372faffc78cfe30d092b1ca065dfb30660704882edf2dcdb2618c6a
|
7
|
+
data.tar.gz: 3c3a0aa8040962f2bb5d161e4ed7b232e87dbe73c716fa47c14bf5729cc733ce018304f3b9a19449b5a83fded733f2d408a1e141dd24840322ed682bc446024d
|
data/Gemfile.lock
CHANGED
data/{README.md → README.org}
RENAMED
@@ -1,91 +1,101 @@
|
|
1
|
-
|
1
|
+
* InsaneHook
|
2
2
|
|
3
3
|
Enjoy the enforcing-DSL of this command-patterny gem.
|
4
4
|
|
5
|
-
|
5
|
+
** Current version
|
6
6
|
|
7
|
-
0.
|
7
|
+
0.5.0
|
8
8
|
|
9
|
-
|
9
|
+
** Installation
|
10
10
|
|
11
11
|
Add this line to your application's Gemfile:
|
12
12
|
|
13
|
-
|
13
|
+
#+BEGIN_SRC ruby
|
14
14
|
gem 'insane_hook'
|
15
|
-
|
15
|
+
#+END_SRC
|
16
16
|
|
17
17
|
And then execute:
|
18
18
|
|
19
|
+
#+BEGIN_SRC bash
|
19
20
|
$ bundle
|
21
|
+
#+END_SRC
|
20
22
|
|
21
23
|
Or install it yourself as:
|
22
24
|
|
25
|
+
#+BEGIN_SRC bash
|
23
26
|
$ gem install insane_hook
|
27
|
+
#+END_SRC
|
24
28
|
|
25
|
-
|
29
|
+
** Usage
|
26
30
|
|
27
|
-
|
31
|
+
#+BEGIN_SRC ruby
|
28
32
|
class YeOldeTaske < InsaneHook
|
29
|
-
|
30
|
-
|
31
|
-
|
33
|
+
required :arg1, :arg2
|
34
|
+
required :arg0
|
35
|
+
optional :arg3, arg4: 3
|
36
|
+
optional :arg9
|
32
37
|
call do
|
33
|
-
|
38
|
+
leak "meaningful value"
|
34
39
|
end
|
35
40
|
end
|
36
|
-
|
41
|
+
#+END_SRC
|
37
42
|
|
38
43
|
Is equivalent to:
|
39
44
|
|
40
|
-
|
41
|
-
class YeOldeTaske
|
45
|
+
#+BEGIN_SRC ruby
|
46
|
+
class YeOldeTaske
|
42
47
|
|
43
|
-
|
44
|
-
|
45
|
-
|
48
|
+
def self.call(**args)
|
49
|
+
new(**args).call
|
50
|
+
end
|
46
51
|
|
47
|
-
|
48
|
-
def initialize(some_required_arg:, some_optional_arg: nil)
|
49
|
-
@some_required_arg = some_required_arg
|
50
|
-
@some_optional_arg = some_optional_arg
|
51
|
-
end
|
52
|
+
attr_reader :arg0, :arg1, :arg2, :arg3, :arg4, :arg9
|
52
53
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
54
|
+
def initialize(arg0:, arg1:, arg2:, arg3: nil, arg9: nil, arg4: 3)
|
55
|
+
@arg0 = arg0
|
56
|
+
@arg1 = arg1
|
57
|
+
@arg2 = arg2
|
58
|
+
@arg3 = arg3
|
59
|
+
@arg4 = arg4
|
60
|
+
@arg9 = arg9
|
61
|
+
end
|
57
62
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
63
|
+
def call
|
64
|
+
leak "meaningful value"
|
65
|
+
self
|
66
|
+
end
|
67
|
+
|
68
|
+
def leak(arg)
|
69
|
+
@_result = arg
|
70
|
+
end
|
71
|
+
|
72
|
+
def result
|
73
|
+
if instance_variable_defined?(:@_result)
|
74
|
+
@_result
|
62
75
|
else
|
63
76
|
raise InsaneHook::CommandNotRunError
|
64
77
|
end
|
65
|
-
else
|
66
|
-
@result = arg
|
67
78
|
end
|
68
79
|
end
|
80
|
+
#+END_SRC
|
69
81
|
|
70
|
-
end
|
71
|
-
```
|
72
82
|
|
73
83
|
|
74
|
-
|
84
|
+
** Design decisions
|
75
85
|
1. Usage of `call` is idiomatic Ruby. Procs and method objects respond to `call`, so we are extending an existing Ruby pattern.
|
76
|
-
2. Commands should not return anything, but if you are forced to check a result, then set the result to a single object and work off of that object
|
77
|
-
3. Composition is usually better than Inheritance, especially in a language that doesn't support multiple inheritance. Here we need to use inheritance because we are completely taking over both the `.new` and the `#initialize` methods, meaning the object does not truly belong to the person writing the code.
|
86
|
+
2. Commands should not return anything (they are not queries), but if you are forced to check a result, then set the result to a single object and work off of that object ("leak" that data out of the command).
|
87
|
+
3. Composition is usually better than Inheritance, especially in a language that doesn't support multiple inheritance. Here we need to use inheritance because we are completely taking over both the `.new` and the `#initialize` methods, meaning the object does not truly belong to the person writing the code: the programmer is limited by the provided framework.
|
78
88
|
|
79
|
-
|
89
|
+
** Development
|
80
90
|
|
81
91
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
82
92
|
|
83
93
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
84
94
|
|
85
|
-
|
95
|
+
** Contributing
|
86
96
|
|
87
97
|
Bug reports and pull requests are welcome on GitHub at https://github.com/trevoke/insane_hook.
|
88
98
|
|
89
|
-
|
99
|
+
** License
|
90
100
|
|
91
101
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/insane_hook.rb
CHANGED
@@ -7,19 +7,19 @@ class InsaneHook
|
|
7
7
|
include InsaneHook::Constants
|
8
8
|
|
9
9
|
def self.inherited(subclass)
|
10
|
-
subclass.class_variable_set(ARGS_VAR, {REQUIRED_ARGS => [], OPTIONAL_ARGS =>
|
10
|
+
subclass.class_variable_set(ARGS_VAR, {REQUIRED_ARGS => [], OPTIONAL_ARGS => {}})
|
11
11
|
subclass.extend(InsaneHook::ClassMethods)
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
14
|
+
def leak(arg)
|
15
|
+
instance_variable_set(RESULT_VAR, arg)
|
16
|
+
end
|
17
|
+
|
18
|
+
def result
|
19
|
+
if instance_variable_defined?(RESULT_VAR)
|
20
|
+
instance_variable_get(RESULT_VAR)
|
21
21
|
else
|
22
|
-
|
22
|
+
raise CommandNotRunError
|
23
23
|
end
|
24
24
|
end
|
25
25
|
end
|
@@ -13,8 +13,8 @@ class InsaneHook
|
|
13
13
|
obj.instance_variable_set("@#{var}", value)
|
14
14
|
obj.class.class_eval{attr_reader var}
|
15
15
|
end
|
16
|
-
self.class_variable_get(ARGS_VAR)[OPTIONAL_ARGS].each do |var|
|
17
|
-
value = args.fetch(var,
|
16
|
+
self.class_variable_get(ARGS_VAR)[OPTIONAL_ARGS].each do |var, val|
|
17
|
+
value = args.fetch(var, val)
|
18
18
|
obj.instance_variable_set("@#{var}", value)
|
19
19
|
obj.class.class_eval{attr_reader var}
|
20
20
|
end
|
@@ -22,17 +22,23 @@ class InsaneHook
|
|
22
22
|
obj
|
23
23
|
end
|
24
24
|
|
25
|
-
def
|
26
|
-
fail "#{key} is not a symbol" unless
|
25
|
+
def required(*keys)
|
26
|
+
fail "#{key} is not a symbol" unless keys.all? { |x| x.is_a? Symbol }
|
27
27
|
args = self.class_variable_get(ARGS_VAR)
|
28
|
-
args[REQUIRED_ARGS]
|
28
|
+
args[REQUIRED_ARGS] = (args[REQUIRED_ARGS] + keys).uniq
|
29
29
|
self.class_variable_set(ARGS_VAR, args)
|
30
30
|
end
|
31
31
|
|
32
|
-
def
|
33
|
-
|
32
|
+
def optional(*no_values, **with_values)
|
33
|
+
keys = no_values + with_values.keys
|
34
|
+
fail "#{key} is not a symbol" unless keys.all? { |x| x.is_a? Symbol }
|
34
35
|
args = self.class_variable_get(ARGS_VAR)
|
35
|
-
|
36
|
+
no_values.each do |optional_arg|
|
37
|
+
args[OPTIONAL_ARGS][optional_arg] ||= nil
|
38
|
+
end
|
39
|
+
with_values.each do |optional_arg, default_value|
|
40
|
+
args[OPTIONAL_ARGS][optional_arg] ||= default_value
|
41
|
+
end
|
36
42
|
self.class_variable_set(ARGS_VAR, args)
|
37
43
|
end
|
38
44
|
|
@@ -41,7 +47,6 @@ class InsaneHook
|
|
41
47
|
raise "Block cannot take arguments" if block.arity > 0
|
42
48
|
raise "call method already defined" if self.instance_methods.include?(:call)
|
43
49
|
define_method(:call) do
|
44
|
-
result(nil)
|
45
50
|
instance_eval(&block)
|
46
51
|
self
|
47
52
|
end
|
data/lib/insane_hook/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: insane_hook
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aldric Giacomoni, Ahmad Ragab
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-07-
|
11
|
+
date: 2018-07-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,7 +66,7 @@ files:
|
|
66
66
|
- Gemfile
|
67
67
|
- Gemfile.lock
|
68
68
|
- LICENSE.txt
|
69
|
-
- README.
|
69
|
+
- README.org
|
70
70
|
- Rakefile
|
71
71
|
- bin/console
|
72
72
|
- bin/setup
|