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.18.0"
Class Method Summary collapse
- .configuration ⇒ Object
- .configure {|configuration| ... } ⇒ Object
- .disable! ⇒ Object
- .enable! ⇒ Object
- .enabled? ⇒ Boolean
-
.recording ⇒ Object
this method is a block-based way to enable and disable recording of facts.
- .storage ⇒ Object
- .storage=(io) ⇒ Object
- .verify(record_hash) ⇒ Object
- .watch(klass, method_name, type: :instance) ⇒ Object
- .watch_all(klass, except: []) ⇒ Object
Class Method Details
.configuration ⇒ Object
13 14 15 |
# File 'lib/stable.rb', line 13 def configuration @configuration ||= Configuration.new end |
.configure {|configuration| ... } ⇒ Object
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
29 30 31 |
# File 'lib/stable.rb', line 29 def enabled? Thread.current[:stable_enabled] || configuration.enabled || false end |
.recording ⇒ Object
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 |
.storage ⇒ Object
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
123 124 125 |
# File 'lib/stable.rb', line 123 def verify(record_hash) Fact.from_jsonl(record_hash.to_json).run! end |
.watch(klass, method_name, type: :instance) ⇒ 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 104 105 106 107 108 109 |
# File 'lib/stable.rb', line 60 def watch(klass, method_name, type: :instance) original_method = type == :instance ? klass.instance_method(method_name) : klass.method(method_name) target = type == :instance ? klass : klass.singleton_class wrapper_module = Module.new do define_method(method_name) do |*args, **kwargs, &block| if Stable.enabled? begin result = original_method.is_a?(UnboundMethod) ? original_method.bind(self).call(*args, **kwargs, &block) : original_method.call(*args, **kwargs, &block) fact = Fact.new( class_name: klass.name, method_name: method_name, method_type: type, args: args, kwargs: kwargs, 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, method_type: type, args: args, kwargs: kwargs, error: { class: e.class.name, message: e., 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.is_a?(UnboundMethod) ? original_method.bind(self).call(*args, **kwargs, &block) : original_method.call(*args, **kwargs, &block) end end end target.prepend(wrapper_module) end |
.watch_all(klass, except: []) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/stable.rb', line 111 def watch_all(klass, except: []) klass.public_instance_methods(false).each do |method_name| next if except.include?(method_name) watch(klass, method_name, type: :instance) end klass.public_methods(false).each do |method_name| next if except.include?(method_name) watch(klass, method_name, type: :class) end end |