action_handler 0.1.0 → 0.2.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: 06a9029c41b4a4ad4284e7e91f59724885da4f47b75b561cb30ed03f76734e19
4
- data.tar.gz: ffbd72526d8bf9977a6807da8c10596db7b788d0596f50c558f216cc5af38f06
3
+ metadata.gz: 25a1f7268d1e4a69c5b7b6539add71a3c1de890b16089326be89b8b29df485b5
4
+ data.tar.gz: 01412c3d175cce1e05361ef8cc1debe36f095bc94588505af64c1f3bded98587
5
5
  SHA512:
6
- metadata.gz: be995c1aea5765076ec538b25558a67c7105aa212aa9432e3bf1a83cd0b76dd6a94019dd121dcc6b00e130f6e554aa7c0997aab98bd10c52df9635fead2ce409
7
- data.tar.gz: fccdd48b45bf653d32a27165945c45c6c174ec8ee872a356b22271ce4946a01f86dc1edcc39257b31426e8c23ce66b69e46d4900fd28e8abd775b59f60633a05
6
+ metadata.gz: 0dc55dca8b126cc3487a7e69b44d34ebdf83a8f0cd27b91dc1afe111f932e8e45166d226ea4d7ae6695c8111cb669ecbf7b6cf84629f7a784da608eb60284ee6
7
+ data.tar.gz: e6812ff0295766d8e8bb55026d032dacf57bdd744b3f3feca197d24def657eae0cb08ef9c17365a40f322f2546e9b724a05e2c8acae87b8a8112c2393a709977
@@ -14,16 +14,24 @@ module ActionHandler
14
14
  handler_class.instance_variable_set(CONFIG_VAR_NAME, config)
15
15
  end
16
16
 
17
+ attr_reader :as_controller
17
18
  attr_reader :action_methods
18
19
  attr_reader :args_suppliers
19
20
  attr_reader :custom_args
20
21
 
21
22
  def initialize
23
+ @as_controller = nil
22
24
  @action_methods = nil
23
25
  @args_suppliers = []
24
26
  @custom_args = {} # { method_name: proc }
25
27
  end
26
28
 
29
+ def as_controller=(block)
30
+ raise ArgumentError, 'must be proc' unless block.is_a?(Proc)
31
+
32
+ @as_controller = block
33
+ end
34
+
27
35
  def action_methods=(names)
28
36
  raise ArgumentError, 'must be array' unless names.is_a?(Array)
29
37
 
@@ -35,6 +35,10 @@ module ActionHandler
35
35
  end
36
36
 
37
37
  module HandlerExtension
38
+ def as_controller(&block)
39
+ ActionHandler::Config.get(self).as_controller = block
40
+ end
41
+
38
42
  def action_methods(*method_names)
39
43
  ActionHandler::Config.get(self).action_methods = method_names
40
44
  end
@@ -26,11 +26,14 @@ module ActionHandler
26
26
 
27
27
  installer = self
28
28
  initializer = Module.new.tap do |m|
29
- m.define_method(:initialize) do |*args|
30
- super(*args)
29
+ m.send(:define_method, :initialize) do |*args|
31
30
  factory = self.class.instance_variable_get(:@_action_handler_factory)
32
- handler = factory.call(self)
31
+ handler = factory.call
32
+ config = ActionHandler::Config.get(handler.class)
33
+ self.class.class_eval(&config.as_controller) if config&.as_controller
33
34
  installer.send(:setup, self, handler)
35
+
36
+ super(*args)
34
37
  end
35
38
  end
36
39
 
@@ -49,7 +52,7 @@ module ActionHandler
49
52
  # If we use `define_singleton_method`, methods don't work correctly.
50
53
  # I don't know Rails internal details but
51
54
  # Rails requires methods to be defined in a class.
52
- ctrl.class.define_method(name) do
55
+ ctrl.class.send(:define_method, name) do
53
56
  method = handler.method(name)
54
57
  args = installer.args_maker.make_args(method, args_supplier, context: self)
55
58
  res = method.call(*args)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActionHandler
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_handler
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
  - ryym
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-02 00:00:00.000000000 Z
11
+ date: 2018-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec