stable 1.19.0 → 1.20.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: 1fc5ee08da64f57a06f6bfba7ca771def37b21e98ec1957ce990b67cf57f643b
4
- data.tar.gz: 32f6fea778aabb893ea4818d5780889a9d0a3de212a2de1c5c1d5aac7d767347
3
+ metadata.gz: 62b2d2b99ffdecc5553ba21e97ceadf89bd02563c930e79d765875b941bb1bfa
4
+ data.tar.gz: 89c84d540ca58cbec3935d3503d1f787677d1ebb506b6d909b00954ef88ad273
5
5
  SHA512:
6
- metadata.gz: 101de5c354c5e24afb46efad44b20540c1437a8ac8b2f8e5d951dacc68d00ef215845cc4eaf1c32f9ff536447adc0f6f892562d25fc550524f62d3e41430d98a
7
- data.tar.gz: 68c5b4834dc41e3e178906ada7d8500b0d0ab96cd236d6425e2975d180bd3de5b4db9dd5a879962fd7b2e735aba0e1df1d1d5ebc12c0c1c40570cbe6e7fec8b8
6
+ metadata.gz: 937aef6d33cb4e758035bf02abcbb660258051a69dfdb72565c39bdc17e47974bcf0e008d812a63e05c11e3328229e70a9e956e9c789eb9e2e67f72cfc57511c
7
+ data.tar.gz: 1cff3aab0cdbe3c76f2e91e040f536ec4d5b73b61d3c6d39b956a5436f3d1d62a4290858fd8b8ae80da944914073135ba260cdcb3430bea2f28132d9f3c982fe
@@ -0,0 +1,25 @@
1
+ # lib/example/stateful_calculator.rb
2
+
3
+ # this class demonstrates a stateful calculator where operations rely on the
4
+ # value stored in the `@memory` instance variable.
5
+ class StatefulCalculator
6
+ def initialize
7
+ @memory = 0
8
+ end
9
+
10
+ def add(number)
11
+ @memory += number
12
+ end
13
+
14
+ def subtract(number)
15
+ @memory -= number
16
+ end
17
+
18
+ def clear
19
+ @memory = 0
20
+ end
21
+
22
+ def memory
23
+ @memory
24
+ end
25
+ end
data/lib/stable/fact.rb CHANGED
@@ -8,9 +8,9 @@ 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, :method_type, :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, :prior
12
12
 
13
- def initialize(class_name:, method_name:, args:, method_type: :instance, 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, prior: nil)
14
14
  @class_name = class_name
15
15
  @method_name = method_name
16
16
  @method_type = method_type
@@ -23,6 +23,7 @@ module Stable
23
23
  @signature = Digest::SHA256.hexdigest("#{class_name}##{method_name}:#{args.to_json}:#{kwargs.to_json}")
24
24
  @name = name || uuid.split('-').last
25
25
  @source_file = source_file
26
+ @prior = prior
26
27
  end
27
28
 
28
29
  def name=(new_name)
@@ -31,11 +32,16 @@ module Stable
31
32
  end
32
33
 
33
34
 
34
- def run!
35
+ def verify!
35
36
  begin
36
37
  klass = Object.const_get(class_name)
37
38
  if method_type == :instance
38
39
  instance = klass.new
40
+ if prior
41
+ prior.each do |var, value|
42
+ instance.instance_variable_set(var, value)
43
+ end
44
+ end
39
45
  @actual_result = instance.public_send(method_name, *args, **kwargs)
40
46
  else
41
47
  @actual_result = klass.public_send(method_name, *args, **kwargs)
@@ -82,6 +88,7 @@ module Stable
82
88
  method_type: method_type,
83
89
  args: args,
84
90
  kwargs: kwargs,
91
+ prior: prior,
85
92
  result: result,
86
93
  error: error,
87
94
  uuid: uuid,
@@ -98,6 +105,7 @@ module Stable
98
105
  method_type: (data['method_type'] || :instance).to_sym,
99
106
  args: data['args'],
100
107
  kwargs: data['kwargs'],
108
+ prior: data['prior'],
101
109
  result: data['result'],
102
110
  error: data['error'],
103
111
  uuid: data['uuid'],
@@ -105,7 +113,5 @@ module Stable
105
113
  source_file: source_file
106
114
  )
107
115
  end
108
-
109
-
110
116
  end
111
117
  end
@@ -1,3 +1,3 @@
1
1
  module Stable
2
- VERSION = "1.19.0"
2
+ VERSION = "1.20.0"
3
3
  end
data/lib/stable.rb CHANGED
@@ -66,6 +66,7 @@ module Stable
66
66
  define_method(method_name) do |*args, **kwargs, &block|
67
67
  if Stable.enabled?
68
68
  begin
69
+ prior = Stable.send(:_capture_state, self)
69
70
  result = original_method.is_a?(UnboundMethod) ? original_method.bind(self).call(*args, **kwargs, &block) : original_method.call(*args, **kwargs, &block)
70
71
  fact = Fact.new(
71
72
  class_name: klass.name,
@@ -73,6 +74,7 @@ module Stable
73
74
  method_type: type,
74
75
  args: args,
75
76
  kwargs: kwargs,
77
+ prior: prior,
76
78
  result: result
77
79
  )
78
80
  unless Stable.send(:_fact_exists?, fact.signature)
@@ -82,12 +84,14 @@ module Stable
82
84
  end
83
85
  result
84
86
  rescue => e
87
+ prior = Stable.send(:_capture_state, self)
85
88
  fact = Fact.new(
86
89
  class_name: klass.name,
87
90
  method_name: method_name,
88
91
  method_type: type,
89
92
  args: args,
90
93
  kwargs: kwargs,
94
+ prior: prior,
91
95
  error: {
92
96
  class: e.class.name,
93
97
  message: e.message,
@@ -122,7 +126,7 @@ module Stable
122
126
  end
123
127
 
124
128
  def verify(record_hash)
125
- Fact.from_jsonl(record_hash.to_json).run!
129
+ Fact.from_jsonl(record_hash.to_json).verify!
126
130
  end
127
131
 
128
132
  private
@@ -140,5 +144,11 @@ module Stable
140
144
  def _fact_exists?(signature)
141
145
  _recorded_facts.any? { |fact| fact.signature == signature }
142
146
  end
147
+
148
+ def _capture_state(obj)
149
+ obj.instance_variables.each_with_object({}) do |var, hash|
150
+ hash[var] = obj.instance_variable_get(var)
151
+ end
152
+ end
143
153
  end
144
154
  end
@@ -1,4 +1,4 @@
1
- # lib/tasks/stable.rake
1
+ require 'rake'
2
2
  require_relative '../stable'
3
3
  require_relative '../stable/formatters/verbose'
4
4
 
@@ -8,6 +8,7 @@ namespace :stable do
8
8
  require_relative '../../lib/example/calculator'
9
9
  require_relative '../../lib/example/kw_calculator'
10
10
  require_relative '../../lib/example/class_methods'
11
+ require_relative '../../lib/example/stateful_calculator'
11
12
 
12
13
  fact_path = File.expand_path('../../../facts/calculator.fact.example', __FILE__)
13
14
  Stable.configure do |config|
@@ -23,7 +24,7 @@ namespace :stable do
23
24
  puts formatter.header
24
25
 
25
26
  _filter_facts(facts, args[:filter].to_s.strip.downcase).each do |fact|
26
- fact.run!
27
+ fact.verify!
27
28
  puts formatter.to_s(fact)
28
29
  end
29
30
 
@@ -43,7 +44,7 @@ namespace :stable do
43
44
  puts formatter.header
44
45
 
45
46
  facts.each do |fact|
46
- fact.run!
47
+ fact.verify!
47
48
  puts formatter.to_s(fact)
48
49
  end
49
50
 
@@ -96,7 +97,7 @@ namespace :stable do
96
97
 
97
98
  updated_facts = []
98
99
  _filter_facts(facts, _clean_filter(args[:filter])).each do |fact|
99
- fact.run!
100
+ fact.verify!
100
101
  if fact.status == :failed
101
102
  puts formatter.to_s(fact)
102
103
  print " update this fact? (y/n): "
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.19.0
4
+ version: 1.20.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt
@@ -32,6 +32,7 @@ files:
32
32
  - lib/example/calculator.rb
33
33
  - lib/example/class_methods.rb
34
34
  - lib/example/kw_calculator.rb
35
+ - lib/example/stateful_calculator.rb
35
36
  - lib/stable.rb
36
37
  - lib/stable/configuration.rb
37
38
  - lib/stable/fact.rb