Class: Stable::Fact
- Inherits:
-
Object
- Object
- Stable::Fact
- Defined in:
- lib/stable/fact.rb
Overview
a fact is a recording of a single method call, including the inputs and outputs. it’s a self-contained, serializable representation of a method’s behavior at a specific point in time.
Instance Attribute Summary collapse
-
#actual_error ⇒ Object
readonly
Returns the value of attribute actual_error.
-
#actual_result ⇒ Object
readonly
Returns the value of attribute actual_result.
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#class_name ⇒ Object
readonly
Returns the value of attribute class_name.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#kwargs ⇒ Object
readonly
Returns the value of attribute kwargs.
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
-
#method_type ⇒ Object
readonly
Returns the value of attribute method_type.
-
#name ⇒ Object
Returns the value of attribute name.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#signature ⇒ Object
readonly
Returns the value of attribute signature.
-
#source_file ⇒ Object
readonly
Returns the value of attribute source_file.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#uuid ⇒ Object
readonly
Returns the value of attribute uuid.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(class_name:, method_name:, args:, method_type: :instance, kwargs: {}, result: nil, error: nil, uuid: SecureRandom.uuid, name: nil, source_file: nil) ⇒ Fact
constructor
A new instance of Fact.
- #run! ⇒ Object
- #to_jsonl ⇒ Object
- #update! ⇒ Object
Constructor Details
#initialize(class_name:, method_name:, args:, method_type: :instance, kwargs: {}, result: nil, error: nil, uuid: SecureRandom.uuid, name: nil, source_file: nil) ⇒ Fact
Returns a new instance of Fact.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/stable/fact.rb', line 13 def initialize(class_name:, method_name:, args:, method_type: :instance, kwargs: {}, result: nil, error: nil, uuid: SecureRandom.uuid, name: nil, source_file: nil) @class_name = class_name @method_name = method_name @method_type = method_type @args = args @kwargs = (kwargs || {}).transform_keys(&:to_sym) @result = result @error = error @status = :pending @uuid = uuid @signature = Digest::SHA256.hexdigest("#{class_name}##{method_name}:#{args.to_json}:#{kwargs.to_json}") @name = name || uuid.split('-').last @source_file = source_file end |
Instance Attribute Details
#actual_error ⇒ Object (readonly)
Returns the value of attribute actual_error.
11 12 13 |
# File 'lib/stable/fact.rb', line 11 def actual_error @actual_error end |
#actual_result ⇒ Object (readonly)
Returns the value of attribute actual_result.
11 12 13 |
# File 'lib/stable/fact.rb', line 11 def actual_result @actual_result end |
#args ⇒ Object (readonly)
Returns the value of attribute args.
11 12 13 |
# File 'lib/stable/fact.rb', line 11 def args @args end |
#class_name ⇒ Object (readonly)
Returns the value of attribute class_name.
11 12 13 |
# File 'lib/stable/fact.rb', line 11 def class_name @class_name end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
11 12 13 |
# File 'lib/stable/fact.rb', line 11 def error @error end |
#kwargs ⇒ Object (readonly)
Returns the value of attribute kwargs.
11 12 13 |
# File 'lib/stable/fact.rb', line 11 def kwargs @kwargs end |
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
11 12 13 |
# File 'lib/stable/fact.rb', line 11 def method_name @method_name end |
#method_type ⇒ Object (readonly)
Returns the value of attribute method_type.
11 12 13 |
# File 'lib/stable/fact.rb', line 11 def method_type @method_type end |
#name ⇒ Object
Returns the value of attribute name.
11 12 13 |
# File 'lib/stable/fact.rb', line 11 def name @name end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
11 12 13 |
# File 'lib/stable/fact.rb', line 11 def result @result end |
#signature ⇒ Object (readonly)
Returns the value of attribute signature.
11 12 13 |
# File 'lib/stable/fact.rb', line 11 def signature @signature end |
#source_file ⇒ Object (readonly)
Returns the value of attribute source_file.
11 12 13 |
# File 'lib/stable/fact.rb', line 11 def source_file @source_file end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
11 12 13 |
# File 'lib/stable/fact.rb', line 11 def status @status end |
#uuid ⇒ Object (readonly)
Returns the value of attribute uuid.
11 12 13 |
# File 'lib/stable/fact.rb', line 11 def uuid @uuid end |
Class Method Details
.from_jsonl(jsonl_string, source_file = nil) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/stable/fact.rb', line 93 def self.from_jsonl(jsonl_string, source_file = nil) data = JSON.parse(jsonl_string) new( class_name: data['class'], method_name: data['method'], method_type: (data['method_type'] || :instance).to_sym, args: data['args'], kwargs: data['kwargs'], result: data['result'], error: data['error'], uuid: data['uuid'], name: data['name'], source_file: source_file ) end |
Instance Method Details
#run! ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/stable/fact.rb', line 34 def run! begin klass = Object.const_get(class_name) if method_type == :instance instance = klass.new @actual_result = instance.public_send(method_name, *args, **kwargs) else @actual_result = klass.public_send(method_name, *args, **kwargs) end if error @status = :failed elsif actual_result == result @status = :passed else @status = :failed end rescue => e @actual_error = e if error && e.class.name == error["class"] @status = :passed_with_error else @status = :failed end end self end |
#to_jsonl ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/stable/fact.rb', line 78 def to_jsonl { class: class_name, method: method_name, method_type: method_type, args: args, kwargs: kwargs, result: result, error: error, uuid: uuid, signature: signature, name: name }.compact.to_json end |
#update! ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/stable/fact.rb', line 61 def update! if actual_error @error = { class: actual_error.class.name, message: actual_error., backtrace: actual_error.backtrace } @result = nil else @result = actual_result @error = nil end @status = :passed end |