insane_hook 0.4.0 → 0.5.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 02b68a08a81d3f28cfad66779ed793df6ce08fcc200f56395f2c4d6bea82d786
4
- data.tar.gz: c141a6ee66ed0766c6e36301f020fbdb2ceb4350e17f021b6fd03f7cf543c9fb
3
+ metadata.gz: af161e7e0ea7b92529311bdd6a93cf3c5be9025c35a34176cbbded0310818653
4
+ data.tar.gz: 36e3b3f6538c3374323080ec1cd7d87e4620862a5f043c77b087b5c66844beca
5
5
  SHA512:
6
- metadata.gz: d4280e7a961f0c9d5d442ba93f89dc4bf7da836f96380883306051f12a1227423e69424b9881903c5936bca5b6a4f2dfcbfb758250a4c87a3e749ec2ad4bc172
7
- data.tar.gz: 793c51f4be50e4b7af99cba480c64c9fb604230d598428b83b71ff0d6ad9818ec18337b25483f5c0dff930dff3d83b7f8d0c200900f5578dfde36e661b982188
6
+ metadata.gz: 609cf7608e901df5d52773072a61bcd660fe2824ae58e2abd79d92334e74643de878700ab372faffc78cfe30d092b1ca065dfb30660704882edf2dcdb2618c6a
7
+ data.tar.gz: 3c3a0aa8040962f2bb5d161e4ed7b232e87dbe73c716fa47c14bf5729cc733ce018304f3b9a19449b5a83fded733f2d408a1e141dd24840322ed682bc446024d
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- insane_hook (0.4.0)
4
+ insane_hook (0.5.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,91 +1,101 @@
1
- # InsaneHook
1
+ * InsaneHook
2
2
 
3
3
  Enjoy the enforcing-DSL of this command-patterny gem.
4
4
 
5
- ## Current version
5
+ ** Current version
6
6
 
7
- 0.4.0
7
+ 0.5.0
8
8
 
9
- ## Installation
9
+ ** Installation
10
10
 
11
11
  Add this line to your application's Gemfile:
12
12
 
13
- ```ruby
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
- ## Usage
29
+ ** Usage
26
30
 
27
- ```ruby
31
+ #+BEGIN_SRC ruby
28
32
  class YeOldeTaske < InsaneHook
29
- requires :some_required_arg
30
- fallbacks :some_optional_arg
31
-
33
+ required :arg1, :arg2
34
+ required :arg0
35
+ optional :arg3, arg4: 3
36
+ optional :arg9
32
37
  call do
33
- result "meaningful value"
38
+ leak "meaningful value"
34
39
  end
35
40
  end
36
- ```
41
+ #+END_SRC
37
42
 
38
43
  Is equivalent to:
39
44
 
40
- ```ruby
41
- class YeOldeTaske
45
+ #+BEGIN_SRC ruby
46
+ class YeOldeTaske
42
47
 
43
- def self.call(**args)
44
- new(**args).call
45
- end
48
+ def self.call(**args)
49
+ new(**args).call
50
+ end
46
51
 
47
- attr_reader :some_required_arg, :some_optional_arg
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
- def call
54
- result "meaningful value"
55
- self
56
- end
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
- def result(arg = InsaneHook::NO_ARG)
59
- if arg == InsaneHook::NO_ARG
60
- if instance_variable_defined?(:@result)
61
- @result
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
- ## Design decisions
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, for instance if you want to use the command as one of the clauses of a case statement (in which case the result would be a boolean)
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
- ## Development
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
- ## Contributing
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
- ## License
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).
@@ -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 result(value=NO_ARG)
15
- if value == NO_ARG
16
- if instance_variable_defined?(RESULT_VAR)
17
- instance_variable_get(RESULT_VAR)
18
- else
19
- raise CommandNotRunError
20
- end
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
- instance_variable_set(RESULT_VAR, value)
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, nil)
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 requires(key)
26
- fail "#{key} is not a symbol" unless key.is_a? Symbol
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] << key
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 fallbacks(key)
33
- fail "#{key} is not a symbol" unless key.is_a? Symbol
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
- args[OPTIONAL_ARGS] << key
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
@@ -1,3 +1,3 @@
1
1
  class InsaneHook
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
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.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-22 00:00:00.000000000 Z
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.md
69
+ - README.org
70
70
  - Rakefile
71
71
  - bin/console
72
72
  - bin/setup