Module: Stable

Defined in:
lib/stable/formatters/colors.rb,
lib/stable.rb,
lib/stable/fact.rb,
lib/stable/version.rb,
lib/stable/configuration.rb,
lib/stable/formatters/verbose.rb

Overview

lib/stable/formatters/colors.rb

Defined Under Namespace

Modules: Formatters Classes: Configuration, Fact

Constant Summary collapse

VERSION =
"1.16.4"

Class Method Summary collapse

Class Method Details

.configurationObject



13
14
15
# File 'lib/stable.rb', line 13

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



17
18
19
# File 'lib/stable.rb', line 17

def configure
  yield(configuration)
end

.disable!Object



25
26
27
# File 'lib/stable.rb', line 25

def disable!
  Thread.current[:stable_enabled] = false
end

.enable!Object



21
22
23
# File 'lib/stable.rb', line 21

def enable!
  Thread.current[:stable_enabled] = true
end

.enabled?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/stable.rb', line 29

def enabled?
  Thread.current[:stable_enabled] || configuration.enabled || false
end

.recordingObject

this method is a block-based way to enable and disable recording of facts. It ensures that recording is turned on for the duration of the block and is automatically turned off afterward, even if an error occurs.

example:

Stable.recording do
  # code in here will be recorded
end


51
52
53
54
55
56
57
58
# File 'lib/stable.rb', line 51

def recording
  enable!
  yield if block_given?
ensure
  disable!
  storage.close if storage.respond_to?(:close)
  @storage = nil
end

.storageObject



37
38
39
# File 'lib/stable.rb', line 37

def storage
  @storage ||= (configuration.storage_path && File.open(configuration.storage_path, 'a+')) || raise("Stable.storage must be set to an IO-like object")
end

.storage=(io) ⇒ Object



33
34
35
# File 'lib/stable.rb', line 33

def storage=(io)
  @storage = io
end

.verify(record_hash) ⇒ Object



105
106
107
# File 'lib/stable.rb', line 105

def verify(record_hash)
  Fact.from_jsonl(record_hash.to_json).run!
end

.watch(klass, method_name) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/stable.rb', line 60

def watch(klass, method_name)
  original_method = klass.instance_method(method_name)
  wrapper_module = Module.new do
    define_method(method_name) do |*args, &block|
      if Stable.enabled?
        begin
          result = original_method.bind(self).call(*args, &block)
          fact = Fact.new(
            class_name: klass.name,
            method_name: method_name,
            args: args,
            result: result
          )
          unless Stable.send(:_fact_exists?, fact.signature)
            Stable.storage.puts(fact.to_jsonl)
            Stable.storage.flush
            Stable.send(:_recorded_facts) << fact
          end
          result
        rescue => e
          fact = Fact.new(
            class_name: klass.name,
            method_name: method_name,
            args: args,
            error: {
              class: e.class.name,
              message: e.message,
              backtrace: e.backtrace
            }
          )
          unless Stable.send(:_fact_exists?, fact.signature)
            Stable.storage.puts(fact.to_jsonl)
            Stable.storage.flush
            Stable.send(:_recorded_facts) << fact
          end
          raise e
        end
      else
        original_method.bind(self).call(*args, &block)
      end
    end
  end
  klass.prepend(wrapper_module)
end