stable 1.16.4 → 1.18.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: 646d4e400be20c44ac27c773d3f4d474096596f6eb609d62e06f6506bd1114d9
4
- data.tar.gz: 185a341bb632286cbf3e6de0b8142487776098706ca8a72afeee11da1baca48b
3
+ metadata.gz: e273db464150e7c0ece83faa1510fb3b94ca32a48072f867c659dc436bf3859f
4
+ data.tar.gz: 4ebcf45014456a2cb01e835e578d2b09fc3edfd6631e986c62b9d20fe7c47a83
5
5
  SHA512:
6
- metadata.gz: 7ba3f4001b86d8925f9b92a089ae712d3a6de4a9e7906579cd5b8f00937bd85ba20b3c3f14fe5b9c7019f2ccf9d0cd6d123fa613f08946645bee05c620074fca
7
- data.tar.gz: 6232b8095323ba636da21d7c43605c772e1463613e20d8b83b01f93891d8bcd5d67ece963c42d360ca1a4cb72a166fe18a5ccc30731735406768fd03bee72581
6
+ metadata.gz: bf83e82322298736666f5f6bcd1bf13b9ac6ef20d71f4be8faca14cdcef845f3af88ec39227c4457cf1ad9bab416d1fa37c6890ed3e958609a70c7437325e65d
7
+ data.tar.gz: a8cc263e9d936fb34dfc60bf7d90b5793c0800143a3e2bd418ed4ea9cc9a6124caa2c166a62a21a32af0b9ea8cf49401adf9cfd0241ce307f564ee2613b3732e
@@ -0,0 +1,12 @@
1
+ # lib/example/class_methods.rb
2
+ module MyModule
3
+ def self.do_something(val)
4
+ "module did #{val}"
5
+ end
6
+ end
7
+
8
+ class MyClass
9
+ def self.do_something_else(val)
10
+ "class did #{val}"
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ # lib/example/kw_calculator.rb
2
+ class KwCalculator
3
+ def add(a, b:, c: 0)
4
+ a + b + c
5
+ end
6
+ end
data/lib/stable/fact.rb CHANGED
@@ -8,17 +8,19 @@ module Stable
8
8
  # outputs. it's a self-contained, serializable representation of a method's
9
9
  # behavior at a specific point in time.
10
10
  class Fact
11
- attr_reader :class_name, :method_name, :args, :result, :error, :actual_result, :actual_error, :status, :uuid, :signature, :name, :source_file
11
+ attr_reader :class_name, :method_name, :method_type, :args, :kwargs, :result, :error, :actual_result, :actual_error, :status, :uuid, :signature, :name, :source_file
12
12
 
13
- def initialize(class_name:, method_name:, args:, result: nil, error: nil, uuid: SecureRandom.uuid, name: nil, source_file: nil)
13
+ def initialize(class_name:, method_name:, args:, method_type: :instance, kwargs: {}, result: nil, error: nil, uuid: SecureRandom.uuid, name: nil, source_file: nil)
14
14
  @class_name = class_name
15
15
  @method_name = method_name
16
+ @method_type = method_type
16
17
  @args = args
18
+ @kwargs = (kwargs || {}).transform_keys(&:to_sym)
17
19
  @result = result
18
20
  @error = error
19
21
  @status = :pending
20
22
  @uuid = uuid
21
- @signature = Digest::SHA256.hexdigest("#{class_name}##{method_name}:#{args.to_json}")
23
+ @signature = Digest::SHA256.hexdigest("#{class_name}##{method_name}:#{args.to_json}:#{kwargs.to_json}")
22
24
  @name = name || uuid.split('-').last
23
25
  @source_file = source_file
24
26
  end
@@ -32,8 +34,12 @@ module Stable
32
34
  def run!
33
35
  begin
34
36
  klass = Object.const_get(class_name)
35
- instance = klass.new
36
- @actual_result = instance.public_send(method_name, *args)
37
+ if method_type == :instance
38
+ instance = klass.new
39
+ @actual_result = instance.public_send(method_name, *args, **kwargs)
40
+ else
41
+ @actual_result = klass.public_send(method_name, *args, **kwargs)
42
+ end
37
43
  if error
38
44
  @status = :failed
39
45
  elsif actual_result == result
@@ -73,7 +79,9 @@ module Stable
73
79
  {
74
80
  class: class_name,
75
81
  method: method_name,
82
+ method_type: method_type,
76
83
  args: args,
84
+ kwargs: kwargs,
77
85
  result: result,
78
86
  error: error,
79
87
  uuid: uuid,
@@ -87,7 +95,9 @@ module Stable
87
95
  new(
88
96
  class_name: data['class'],
89
97
  method_name: data['method'],
98
+ method_type: (data['method_type'] || :instance).to_sym,
90
99
  args: data['args'],
100
+ kwargs: data['kwargs'],
91
101
  result: data['result'],
92
102
  error: data['error'],
93
103
  uuid: data['uuid'],
@@ -1,3 +1,3 @@
1
1
  module Stable
2
- VERSION = "1.16.4"
2
+ VERSION = "1.18.0"
3
3
  end
data/lib/stable.rb CHANGED
@@ -57,17 +57,21 @@ module Stable
57
57
  @storage = nil
58
58
  end
59
59
 
60
- def watch(klass, method_name)
61
- original_method = klass.instance_method(method_name)
60
+ def watch(klass, method_name, type: :instance)
61
+ original_method = type == :instance ? klass.instance_method(method_name) : klass.method(method_name)
62
+ target = type == :instance ? klass : klass.singleton_class
63
+
62
64
  wrapper_module = Module.new do
63
- define_method(method_name) do |*args, &block|
65
+ define_method(method_name) do |*args, **kwargs, &block|
64
66
  if Stable.enabled?
65
67
  begin
66
- result = original_method.bind(self).call(*args, &block)
68
+ result = original_method.is_a?(UnboundMethod) ? original_method.bind(self).call(*args, **kwargs, &block) : original_method.call(*args, **kwargs, &block)
67
69
  fact = Fact.new(
68
70
  class_name: klass.name,
69
71
  method_name: method_name,
72
+ method_type: type,
70
73
  args: args,
74
+ kwargs: kwargs,
71
75
  result: result
72
76
  )
73
77
  unless Stable.send(:_fact_exists?, fact.signature)
@@ -80,7 +84,9 @@ module Stable
80
84
  fact = Fact.new(
81
85
  class_name: klass.name,
82
86
  method_name: method_name,
87
+ method_type: type,
83
88
  args: args,
89
+ kwargs: kwargs,
84
90
  error: {
85
91
  class: e.class.name,
86
92
  message: e.message,
@@ -95,11 +101,23 @@ module Stable
95
101
  raise e
96
102
  end
97
103
  else
98
- original_method.bind(self).call(*args, &block)
104
+ original_method.is_a?(UnboundMethod) ? original_method.bind(self).call(*args, **kwargs, &block) : original_method.call(*args, **kwargs, &block)
99
105
  end
100
106
  end
101
107
  end
102
- klass.prepend(wrapper_module)
108
+ target.prepend(wrapper_module)
109
+ end
110
+
111
+ def watch_all(klass, except: [])
112
+ klass.public_instance_methods(false).each do |method_name|
113
+ next if except.include?(method_name)
114
+ watch(klass, method_name, type: :instance)
115
+ end
116
+
117
+ klass.public_methods(false).each do |method_name|
118
+ next if except.include?(method_name)
119
+ watch(klass, method_name, type: :class)
120
+ end
103
121
  end
104
122
 
105
123
  def verify(record_hash)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.16.4
4
+ version: 1.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt
@@ -30,6 +30,8 @@ extensions: []
30
30
  extra_rdoc_files: []
31
31
  files:
32
32
  - lib/example/calculator.rb
33
+ - lib/example/class_methods.rb
34
+ - lib/example/kw_calculator.rb
33
35
  - lib/stable.rb
34
36
  - lib/stable/configuration.rb
35
37
  - lib/stable/fact.rb