Class: Boxcars::Observation

Inherits:
Object
  • Object
show all
Defined in:
lib/boxcars/observation.rb

Overview

used by Boxcars to return structured result and additional context

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(note:, status: :ok, **added_context) ⇒ Observation

Returns a new instance of Observation.

Parameters:

  • note (String)

    The note to use for the result

  • status (Symbol) (defaults to: :ok)

    :ok or :error

  • added_context (Hash)

    Any additional context to add to the result



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_contextObject (readonly)

Returns the value of attribute added_context.



6
7
8
# File 'lib/boxcars/observation.rb', line 6

def added_context
  @added_context
end

#noteObject (readonly)

Returns the value of attribute note.



6
7
8
# File 'lib/boxcars/observation.rb', line 6

def note
  @note
end

#statusObject (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

Parameters:

  • note (String)

    The text to use for the observation

  • added_context (Hash)

    Any additional context to add to the result

Returns:



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

Parameters:

  • note (String)

    The text to use for the observation

  • user_context (Hash)

    User information (e.g., { id: 123, email: “[email protected]”, role: “admin” })

  • added_context (Hash)

    Any additional context to add to the result

Returns:



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

Parameters:

  • note (String)

    The text to use for the observation

  • added_context (Hash)

    Any additional context to add to the result

Returns:



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

Parameters:

  • note (String)

    The text to use for the observation

  • user_context (Hash)

    User information (e.g., { id: 123, email: “[email protected]”, role: “admin” })

  • added_context (Hash)

    Any additional context to add to the result

Returns:



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

Parameters:

  • note (String)

    The text to use for the observation

  • user_context (Hash)

    User information (e.g., { id: 123, email: “[email protected]”, role: “admin” })

  • status (Symbol) (defaults to: :ok)

    :ok or :error

  • added_context (Hash)

    Any additional context to add to the result

Returns:



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_hHash

Returns The result as a hash.

Returns:

  • (Hash)

    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_jsonString

Returns The result as a json string.

Returns:

  • (String)

    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_sString

Returns An explanation of the result.

Returns:

  • (String)

    An explanation of the result



31
32
33
# File 'lib/boxcars/observation.rb', line 31

def to_s
  note.to_s
end

#to_textString

Returns An explanation of the result.

Returns:

  • (String)

    An explanation of the result



36
37
38
# File 'lib/boxcars/observation.rb', line 36

def to_text
  to_s
end

#user_contextHash?

Extract user context from the observation

Returns:

  • (Hash, nil)

    The user context if present



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

Returns:

  • (Boolean)

    true if user context is present



92
93
94
# File 'lib/boxcars/observation.rb', line 92

def user_context?
  !user_context.nil?
end