stable 1.3.1 → 1.5.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: fbfe47a4e34cd7aa98e7c0cdfa165adcb2612bb7d3e416ff30395239215df9df
4
- data.tar.gz: 40ea2eef27e201637b20bd88d722d1bf01cad66fd84faed5ca6b9573c2124074
3
+ metadata.gz: 449e9c176cb2085bca88f353966805090e49d1cb8292966069fc4ca32bb18fde
4
+ data.tar.gz: 48980832cbe61ac2dc13474eae12bd70e4bd5c6e42d015eac8ae28245b34ccd4
5
5
  SHA512:
6
- metadata.gz: a33475e16728dbb9bf94f6de1807c7157d4b8edabf4a6f8bb67dac10c800a9b092a36ff66a26e8a50da6399e1c83c571edcfc843b613a34e1e898345b7797e2e
7
- data.tar.gz: 7ed53b7619f0b5644f16f215cc6ed1a3e9ecb949741cb953edca5447784e2cbc71661890217e32674182a1614af4d5e33fd9ee845ada6e1b61fc934528625a46
6
+ metadata.gz: dac85ffc0a8efbd42ea82b62f8ba1796e810c60f7536060ced002c0f6c184d308374b012d8e4d2b30ae3706522a1b4642919fa8447c0b3fe4f3cb2c4516ef483
7
+ data.tar.gz: 491c78e463ac466fb08cf60c0437f4a99b6df645aa7c2537394a4e470d3707d06244a86154eaf7f94726ee5af2f3747bbbb98c8c7e5eae2bd3f67e04580867d9
@@ -0,0 +1,11 @@
1
+ # lib/stable/configuration.rb
2
+ module Stable
3
+ class Configuration
4
+ attr_accessor :storage_path, :enabled
5
+
6
+ def initialize
7
+ @storage_path = nil
8
+ @enabled = false
9
+ end
10
+ end
11
+ end
data/lib/stable.rb CHANGED
@@ -1,9 +1,18 @@
1
1
  # `stable` is a library for recording and replaying method calls.
2
2
  # See README.md for detailed usage instructions.
3
3
  require_relative 'stable/spec'
4
+ require_relative 'stable/configuration'
4
5
 
5
6
  module Stable
6
7
  class << self
8
+ def configuration
9
+ @configuration ||= Configuration.new
10
+ end
11
+
12
+ def configure
13
+ yield(configuration)
14
+ end
15
+
7
16
  def enable!
8
17
  Thread.current[:stable_enabled] = true
9
18
  end
@@ -13,7 +22,7 @@ module Stable
13
22
  end
14
23
 
15
24
  def enabled?
16
- Thread.current[:stable_enabled] || false
25
+ Thread.current[:stable_enabled] || configuration.enabled || false
17
26
  end
18
27
 
19
28
  def storage=(io)
@@ -21,10 +30,29 @@ module Stable
21
30
  end
22
31
 
23
32
  def storage
24
- @storage || raise("Stable.storage must be set to an IO-like object")
33
+ @storage ||= (configuration.storage_path && File.open(configuration.storage_path, 'a+')) || raise("Stable.storage must be set to an IO-like object")
34
+ end
35
+
36
+ # this method is a block-based way to enable and disable recording of
37
+ # specs. It ensures that recording is turned on for the duration of the
38
+ # block and is automatically turned off afterward, even if an error occurs.
39
+ #
40
+ # example:
41
+ #
42
+ # Stable.recording do
43
+ # # code in here will be recorded
44
+ # end
45
+ #
46
+ def recording
47
+ enable!
48
+ yield if block_given?
49
+ ensure
50
+ disable!
51
+ storage.close if storage.respond_to?(:close)
52
+ @storage = nil
25
53
  end
26
54
 
27
- def record(klass, method_name)
55
+ def watch(klass, method_name)
28
56
  original_method = klass.instance_method(method_name)
29
57
  wrapper_module = Module.new do
30
58
  define_method(method_name) do |*args, &block|
@@ -39,6 +67,7 @@ module Stable
39
67
  )
40
68
  unless Stable.send(:_spec_exists?, spec.signature)
41
69
  Stable.storage.puts(spec.to_jsonl)
70
+ Stable.storage.flush
42
71
  Stable.send(:_recorded_specs) << spec
43
72
  end
44
73
  result
@@ -55,6 +84,7 @@ module Stable
55
84
  )
56
85
  unless Stable.send(:_spec_exists?, spec.signature)
57
86
  Stable.storage.puts(spec.to_jsonl)
87
+ Stable.storage.flush
58
88
  Stable.send(:_recorded_specs) << spec
59
89
  end
60
90
  raise e
@@ -76,7 +106,10 @@ module Stable
76
106
  def _recorded_specs
77
107
  @_recorded_specs ||= begin
78
108
  return [] unless storage.respond_to?(:path) && File.exist?(storage.path)
79
- File.foreach(storage.path).map { |line| Spec.from_jsonl(line) }
109
+ storage.rewind
110
+ specs = storage.each_line.map { |line| Spec.from_jsonl(line) }
111
+ storage.seek(0, IO::SEEK_END)
112
+ specs
80
113
  end
81
114
  end
82
115
 
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.3.1
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt
@@ -17,6 +17,7 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - lib/stable.rb
20
+ - lib/stable/configuration.rb
20
21
  - lib/stable/spec.rb
21
22
  homepage: https://github.com/jefflunt/stable
22
23
  licenses: