Class: Stable::Fact

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_errorObject (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_resultObject (readonly)

Returns the value of attribute actual_result.



11
12
13
# File 'lib/stable/fact.rb', line 11

def actual_result
  @actual_result
end

#argsObject (readonly)

Returns the value of attribute args.



11
12
13
# File 'lib/stable/fact.rb', line 11

def args
  @args
end

#class_nameObject (readonly)

Returns the value of attribute class_name.



11
12
13
# File 'lib/stable/fact.rb', line 11

def class_name
  @class_name
end

#errorObject (readonly)

Returns the value of attribute error.



11
12
13
# File 'lib/stable/fact.rb', line 11

def error
  @error
end

#kwargsObject (readonly)

Returns the value of attribute kwargs.



11
12
13
# File 'lib/stable/fact.rb', line 11

def kwargs
  @kwargs
end

#method_nameObject (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_typeObject (readonly)

Returns the value of attribute method_type.



11
12
13
# File 'lib/stable/fact.rb', line 11

def method_type
  @method_type
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/stable/fact.rb', line 11

def name
  @name
end

#resultObject (readonly)

Returns the value of attribute result.



11
12
13
# File 'lib/stable/fact.rb', line 11

def result
  @result
end

#signatureObject (readonly)

Returns the value of attribute signature.



11
12
13
# File 'lib/stable/fact.rb', line 11

def signature
  @signature
end

#source_fileObject (readonly)

Returns the value of attribute source_file.



11
12
13
# File 'lib/stable/fact.rb', line 11

def source_file
  @source_file
end

#statusObject (readonly)

Returns the value of attribute status.



11
12
13
# File 'lib/stable/fact.rb', line 11

def status
  @status
end

#uuidObject (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_jsonlObject



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.message,
      backtrace: actual_error.backtrace
    }
    @result = nil
  else
    @result = actual_result
    @error = nil
  end
  @status = :passed
end