Class: Boxcars::Observation
- Inherits:
-
Object
- Object
- Boxcars::Observation
- Defined in:
- lib/boxcars/observation.rb
Overview
used by Boxcars to return structured result and additional context
Instance Attribute Summary collapse
-
#added_context ⇒ Object
readonly
Returns the value of attribute added_context.
-
#note ⇒ Object
readonly
Returns the value of attribute note.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
-
.err(note) ⇒ Boxcars::Observation
create a new Observaton from a text string with a status of :error.
-
.err_with_user(note, user_context:) ⇒ Boxcars::Observation
create a new Observation with user context and status :error.
-
.ok(note) ⇒ Boxcars::Observation
create a new Observaton from a text string with a status of :ok.
-
.ok_with_user(note, user_context:) ⇒ Boxcars::Observation
create a new Observation with user context and status :ok.
-
.with_user(note, user_context:, status: :ok) ⇒ Boxcars::Observation
create a new Observation with user context.
Instance Method Summary collapse
-
#initialize(note:, status: :ok, **added_context) ⇒ Observation
constructor
A new instance of Observation.
-
#to_h ⇒ Hash
The result as a hash.
-
#to_json ⇒ String
The result as a json string.
-
#to_s ⇒ String
An explanation of the result.
-
#to_text ⇒ String
An explanation of the result.
-
#user_context ⇒ Hash?
Extract user context from the observation.
-
#user_context? ⇒ Boolean
Check if this observation has user context.
Constructor Details
#initialize(note:, status: :ok, **added_context) ⇒ Observation
Returns a new instance of Observation.
11 12 13 14 15 |
# File 'lib/boxcars/observation.rb', line 11 def initialize(note:, status: :ok, **added_context) @note = note @status = status @added_context = added_context end |
Instance Attribute Details
#added_context ⇒ Object (readonly)
Returns the value of attribute added_context.
6 7 8 |
# File 'lib/boxcars/observation.rb', line 6 def added_context @added_context end |
#note ⇒ Object (readonly)
Returns the value of attribute note.
6 7 8 |
# File 'lib/boxcars/observation.rb', line 6 def note @note end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
6 7 8 |
# File 'lib/boxcars/observation.rb', line 6 def status @status end |
Class Method Details
.err(note) ⇒ Boxcars::Observation
create a new Observaton from a text string with a status of :error
52 53 54 |
# File 'lib/boxcars/observation.rb', line 52 def self.err(note, **) new(note:, status: :error, **) end |
.err_with_user(note, user_context:) ⇒ Boxcars::Observation
create a new Observation with user context and status :error
80 81 82 |
# File 'lib/boxcars/observation.rb', line 80 def self.err_with_user(note, user_context:, **) with_user(note, user_context:, status: :error, **) end |
.ok(note) ⇒ Boxcars::Observation
create a new Observaton from a text string with a status of :ok
44 45 46 |
# File 'lib/boxcars/observation.rb', line 44 def self.ok(note, **) new(note:, status: :ok, **) end |
.ok_with_user(note, user_context:) ⇒ Boxcars::Observation
create a new Observation with user context and status :ok
71 72 73 |
# File 'lib/boxcars/observation.rb', line 71 def self.ok_with_user(note, user_context:, **) with_user(note, user_context:, status: :ok, **) end |
.with_user(note, user_context:, status: :ok) ⇒ Boxcars::Observation
create a new Observation with user context
62 63 64 |
# File 'lib/boxcars/observation.rb', line 62 def self.with_user(note, user_context:, status: :ok, **) new(note:, status:, user_context:, **) end |
Instance Method Details
#to_h ⇒ Hash
Returns The result as a hash.
18 19 20 21 22 23 |
# File 'lib/boxcars/observation.rb', line 18 def to_h { note:, status: }.merge(added_context).compact end |
#to_json ⇒ String
Returns The result as a json string.
26 27 28 |
# File 'lib/boxcars/observation.rb', line 26 def to_json(*) JSON.generate(to_h, *) end |
#to_s ⇒ String
Returns An explanation of the result.
31 32 33 |
# File 'lib/boxcars/observation.rb', line 31 def to_s note.to_s end |
#to_text ⇒ String
Returns An explanation of the result.
36 37 38 |
# File 'lib/boxcars/observation.rb', line 36 def to_text to_s end |
#user_context ⇒ Hash?
Extract user context from the observation
86 87 88 |
# File 'lib/boxcars/observation.rb', line 86 def user_context added_context[:user_context] end |
#user_context? ⇒ Boolean
Check if this observation has user context
92 93 94 |
# File 'lib/boxcars/observation.rb', line 92 def user_context? !user_context.nil? end |