protoboard 0.1.5 → 0.2.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
- SHA1:
3
- metadata.gz: c81cf53f96503f2eb2e797e9e7034abe3f1fde97
4
- data.tar.gz: d394dde7965c0a5334cbe7dc2ce1fa5ee1f656f0
2
+ SHA256:
3
+ metadata.gz: 26c6de613a9f52598427a885b88f3633bafbbbd9687ef8f6195245e18861dc28
4
+ data.tar.gz: f5d1a0a55487c21fe5a8c5f12c0b80ef5cac20c086ae3387bf570151a0086730
5
5
  SHA512:
6
- metadata.gz: e4e889e92fa0a9f0e255bb30b25a965d244b05987d4edad86f2f0906f2258704c1892b81a2b325e44f9088febe06b365dfdc293bdd34ff53150cc550b233b01c
7
- data.tar.gz: 31095568cddf438c81fd6b5794674874eed45e870fe2647154bbbfa75cc04307ba7fefdab85e4609c2c1d8e19df45dc5c6ecbdf4ce51ded17cbbe5489220f47a
6
+ metadata.gz: 3214c6f8b365ea31d311d8f81a2aeea9109969e630d2287bf848b1cd06299202ec389ff0adec6e63e61280c6be66d7bfd18b98cf040b845ba90b2e5667043820
7
+ data.tar.gz: d4558604304fa9456c060852719e77f84137931e659d63943ac85e8be14673d7cfac7111b45964e63944fdda49d90711fa99d9e830264cbb0baf2e68d76c3066
data/README.md CHANGED
@@ -66,7 +66,7 @@ end
66
66
  Also if you want to add more than one method in the same class and customize the circuit name:
67
67
 
68
68
  ```ruby
69
- class Foo4
69
+ class MyFooService
70
70
  include Protoboard::CircuitBreaker
71
71
 
72
72
  register_circuits({ some_method: 'my_custom_name', other_method: 'my_other_custom_name' },
@@ -81,6 +81,30 @@ class Foo4
81
81
  end
82
82
  ```
83
83
 
84
+ And you can add singleton methods to be wrapped by a circuit:
85
+
86
+ ```ruby
87
+ class MyFooService
88
+ include Protoboard::CircuitBreaker
89
+
90
+ register_circuits [:some_method, :some_singleton_method],
91
+ singleton_methods: [:some_singleton_method],
92
+ options: {
93
+ service: 'my_cool_service',
94
+ open_after: 2,
95
+ cool_off_after: 3
96
+ }
97
+
98
+ def self.some_singleton_method
99
+ # Something that can break
100
+ end
101
+
102
+ def some_method
103
+ # Something that can break
104
+ end
105
+ end
106
+ ```
107
+
84
108
  ### Callbacks
85
109
 
86
110
  Any callback should receive one argument that it will be an instance of `CircuitExecution` class, that object will respond to the following methods:
@@ -20,8 +20,13 @@ module Protoboard
20
20
  @fallback = options[:fallback]
21
21
  @on_before = options.fetch(:on_before, [])
22
22
  @on_after = options.fetch(:on_after, [])
23
+ @singleton_method = options.fetch(:singleton_method, false)
23
24
  rescue KeyError => error
24
25
  raise ArgumentError, "Missing required arguments: #{error.message}"
25
26
  end
27
+
28
+ def singleton_method?
29
+ @singleton_method
30
+ end
26
31
  end
27
32
  end
@@ -28,7 +28,7 @@ module Protoboard
28
28
  # ====
29
29
  #
30
30
  # * +fallback+ - A callable object with code to be executed as an alternative plan if the code of the circuit fails
31
- def register_circuits(circuit_methods, on_before: [], on_after: [], options:, fallback: nil)
31
+ def register_circuits(circuit_methods, on_before: [], on_after: [], options:, fallback: nil, singleton_methods: [])
32
32
  Protoboard::Helpers::VALIDATE_CALLBACKS.call(on_before)
33
33
  Protoboard::Helpers::VALIDATE_CALLBACKS.call(on_after)
34
34
 
@@ -39,7 +39,8 @@ module Protoboard
39
39
  fallback: fallback,
40
40
  on_before: on_before,
41
41
  on_after: on_after
42
- )
42
+ ),
43
+ singleton_methods
43
44
  )
44
45
 
45
46
  circuits.each do |circuit|
@@ -47,7 +48,10 @@ module Protoboard
47
48
  end
48
49
 
49
50
  proxy_module = Protoboard::CircuitBreaker.create_circuit_proxy(circuits, name)
50
- prepend proxy_module
51
+
52
+ prepend proxy_module::InstanceMethods
53
+
54
+ singleton_class.prepend proxy_module::ClassMethods
51
55
  end
52
56
  end
53
57
 
@@ -84,7 +88,7 @@ module Protoboard
84
88
 
85
89
  ##
86
90
  # Creates a new +circuit+.
87
- def create_circuits(circuit_methods,class_name, options)
91
+ def create_circuits(circuit_methods,class_name, options, singleton_methods)
88
92
  circuit_hash = case circuit_methods
89
93
  when Array
90
94
  circuit_methods.reduce({}) do |memo, value|
@@ -95,8 +99,14 @@ module Protoboard
95
99
  else
96
100
  raise ArgumentError, 'Invalid input for circuit methods'
97
101
  end
102
+
98
103
  circuit_hash.map do |circuit_method, circuit_name|
99
- Circuit.new({ name: circuit_name, method_name: circuit_method }.merge(options))
104
+ Circuit.new({
105
+ name: circuit_name,
106
+ method_name: circuit_method,
107
+ singleton_method: singleton_methods.include?(circuit_method.to_sym)
108
+ }
109
+ .merge(options))
100
110
  end
101
111
  end
102
112
 
@@ -12,14 +12,32 @@ module Protoboard
12
12
  module_name = infer_module_name(class_name, circuits.map(&:method_name))
13
13
  proxy_module = Module.new
14
14
 
15
- proxy_module.instance_exec do
15
+ # Encapsulates instance methods in a module to be used later
16
+ instance_methods = Module.new do
16
17
  circuits.each do |circuit|
17
- define_method(circuit.method_name) do |*args|
18
- Protoboard.config.adapter.run_circuit(circuit) { super(*args) }
18
+ unless circuit.singleton_method?
19
+ define_method(circuit.method_name) do |*args|
20
+ Protoboard.config.adapter.run_circuit(circuit) { super(*args) }
21
+ end
19
22
  end
20
23
  end
21
24
  end
22
25
 
26
+ proxy_module.const_set('InstanceMethods', instance_methods)
27
+
28
+ # Encapsulates singleton methods in a module to be used later
29
+ class_methods = Module.new do
30
+ circuits.each do |circuit|
31
+ if circuit.singleton_method?
32
+ define_method(circuit.method_name) do |*args|
33
+ Protoboard.config.adapter.run_circuit(circuit) { super(*args) }
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ proxy_module.const_set('ClassMethods', class_methods)
40
+
23
41
  Protoboard.const_set(module_name, proxy_module)
24
42
  end
25
43
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Protoboard
4
- VERSION = '0.1.5'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protoboard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Atkinson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2018-06-12 00:00:00.000000000 Z
12
+ date: 2018-06-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: dry-configurable
@@ -163,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
163
  version: '0'
164
164
  requirements: []
165
165
  rubyforge_project:
166
- rubygems_version: 2.6.8
166
+ rubygems_version: 2.7.3
167
167
  signing_key:
168
168
  specification_version: 4
169
169
  summary: Protoboard abstracts the way you use Circuit Breaker allowing you to easily