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, **kwargs) ⇒ 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, **kwargs)
  new(note: note, status: :error, **kwargs)
end

.ok(note, **kwargs) ⇒ 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, **kwargs)
  new(note: note, status: :ok, **kwargs)
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: note,
    status: status
  }.merge(added_context).compact
end

#to_json(*args) ⇒ String

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(*args)
  JSON.generate(to_h, *args)
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