stable 1.17.0 → 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: 48fc9fb3259b016fbd50f2f39b0bbcd8e2ed793de661fcf62510153336f9bf1b
4
- data.tar.gz: e5c14b2a616353cccf9562ef39d1d83aaddf56ddb770412df69e0679d3f053af
3
+ metadata.gz: e273db464150e7c0ece83faa1510fb3b94ca32a48072f867c659dc436bf3859f
4
+ data.tar.gz: 4ebcf45014456a2cb01e835e578d2b09fc3edfd6631e986c62b9d20fe7c47a83
5
5
  SHA512:
6
- metadata.gz: 57cb96d32e8edbaf0fcac195b2d295c2010e0c64e6a6f62a6a5101c6c3f8c9d7c5fc072bb098c8766d34f4a214419e580f866f5a2ff7419dffb16f895694cc1a
7
- data.tar.gz: 82e1bb9b223bb5dbeb8dca3ef613d310de66fd6b8ebcf79f1d99f761b4ffa16929c6e223682fa924f6f8aaa5ff197da3e7eb75da5ce8fecded8bc02a656ed924
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
data/lib/stable/fact.rb CHANGED
@@ -8,11 +8,12 @@ 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, :kwargs, :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:, kwargs: {}, 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
17
18
  @kwargs = (kwargs || {}).transform_keys(&:to_sym)
18
19
  @result = result
@@ -33,8 +34,12 @@ module Stable
33
34
  def run!
34
35
  begin
35
36
  klass = Object.const_get(class_name)
36
- instance = klass.new
37
- @actual_result = instance.public_send(method_name, *args, **kwargs)
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
38
43
  if error
39
44
  @status = :failed
40
45
  elsif actual_result == result
@@ -74,6 +79,7 @@ module Stable
74
79
  {
75
80
  class: class_name,
76
81
  method: method_name,
82
+ method_type: method_type,
77
83
  args: args,
78
84
  kwargs: kwargs,
79
85
  result: result,
@@ -89,6 +95,7 @@ module Stable
89
95
  new(
90
96
  class_name: data['class'],
91
97
  method_name: data['method'],
98
+ method_type: (data['method_type'] || :instance).to_sym,
92
99
  args: data['args'],
93
100
  kwargs: data['kwargs'],
94
101
  result: data['result'],
@@ -1,3 +1,3 @@
1
1
  module Stable
2
- VERSION = "1.17.0"
2
+ VERSION = "1.18.0"
3
3
  end
data/lib/stable.rb CHANGED
@@ -57,16 +57,19 @@ 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
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, **kwargs, &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,
71
74
  kwargs: kwargs,
72
75
  result: result
@@ -81,6 +84,7 @@ module Stable
81
84
  fact = Fact.new(
82
85
  class_name: klass.name,
83
86
  method_name: method_name,
87
+ method_type: type,
84
88
  args: args,
85
89
  kwargs: kwargs,
86
90
  error: {
@@ -97,11 +101,23 @@ module Stable
97
101
  raise e
98
102
  end
99
103
  else
100
- original_method.bind(self).call(*args, **kwargs, &block)
104
+ original_method.is_a?(UnboundMethod) ? original_method.bind(self).call(*args, **kwargs, &block) : original_method.call(*args, **kwargs, &block)
101
105
  end
102
106
  end
103
107
  end
104
- 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
105
121
  end
106
122
 
107
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.17.0
4
+ version: 1.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt
@@ -30,6 +30,7 @@ extensions: []
30
30
  extra_rdoc_files: []
31
31
  files:
32
32
  - lib/example/calculator.rb
33
+ - lib/example/class_methods.rb
33
34
  - lib/example/kw_calculator.rb
34
35
  - lib/stable.rb
35
36
  - lib/stable/configuration.rb