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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 52087f990c211ae1507916fb8c05de5e34fa3cc4
4
- data.tar.gz: ff9200db9a99ccf204e6282df0b8b23114267bad
3
+ metadata.gz: 0a61856275cac8c83baf51f5dbe739af495c2086
4
+ data.tar.gz: 04f39eb108182f93f346afbd2c164ec36a421fda
5
5
  SHA512:
6
- metadata.gz: be0a31b1693bc8321e422ca159084f2c49ac90da847072ee578b9c499314e5adbceb0d55631a792e7204e27cc9f1e037574b311a4fc43fe1d787d890204a27e9
7
- data.tar.gz: 4a81df5fa6fd250662cc7241469d0908717e5cef03b903e2df4d393a7ebd2b0b3612c791fd615a49889c77a672abdf8105a7d99211b7be207179197d91c5e75c
6
+ metadata.gz: 576f92cee691387c45cc6f06971ade6c8eb2c7113ae14a90e277d732266eae6b2a01656b3d24cc4805e67f7ee421b2e03f40825fbf657937c00206ce2f2a8933
7
+ data.tar.gz: 40c9e53c80c015a4cf4646449ac7dd41629205e5045ced29ab172ad8967f5139f267334ca2c83a2d9a87dd2faa054fcb86718e2bf0a0de7c9059c25a4217401b
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- insane_hook (0.1.0)
4
+ insane_hook (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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 meaningful_value
33
+ result "meaningful value"
34
34
  end
35
35
  end
36
36
 
37
- task = YeOldeTaske.new(some_required_arg: 7).call # => task instance
38
- task.result # => meaningful_value
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(:@@_command, {required: [], optional: []})
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=:_no_value)
17
- if value == :_no_value
18
- if instance_variable_defined?(:@_result)
19
- instance_variable_get(:@_result)
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(:@_result, value)
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(:@@_command)
31
- args[:required] << key
32
- self.class_variable_set(:@@_command, args)
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(:@@_command)
38
- args[:optional] << key
39
- self.class_variable_set(:@@_command, args)
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
- define_method(:call) do
44
- result(nil)
45
- instance_eval(&block)
46
- self
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, &block)
62
+ def new(**args)
52
63
  obj = self.allocate
53
- self.class_variable_get(:@@_command)[:required].each do |var|
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(:@@_command)[:optional].each do |var|
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}
@@ -1,3 +1,3 @@
1
1
  module InsaneHook
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: insane_hook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aldric Giacomoni, Ahmad Ragab