Class: Krill::ShowResponse

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/krill/show_response.rb

Overview

Defines a wrapper for the data hash that is returned by the show method, with a simplified interface, additional convenience methods, and abstraction of implementation details. This is a decorator class to be instantiated with a Hash. Initialized with a Hash, which has a :timepoint value as a float, and a :table_inputs value as an array of hashes in the format expected from show return

Instance Method Summary collapse

Instance Method Details

#get_response(var) ⇒ Object

Return the response that was stored under var. When used with

a key associated with a table response-set, returns a list of table responses in order of the rows of the table.

Parameters:

  • var (Symbol/String)

    The var used to store data under specified in the get or select call of the ShowBlock



25
26
27
# File 'lib/krill/show_response.rb', line 25

def get_response(var)
  responses[var.to_sym]
end

#get_table_response(var, opts = {}) ⇒ String/Fixnum

Returns data recorded in a specified row of an input table

Parameters:

  • var (Symbol/String)

    the table key that was specified to store data under in the get.

  • opts (Hash) (defaults to: {})

    additional options

  • op (Hash)

    a customizable set of options

  • row (Hash)

    a customizable set of options

Returns:

  • (String/Fixnum)

    the data inputted in the particular input cell specified by the column associated with var, and the row associated with either op or row returns nil if the requested row/column pair doesn't exist

Raises:

  • (TableCellUndefined)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/krill/show_response.rb', line 41

def get_table_response(var, opts = {})
  raise TableCellUndefined.new('Invalid parameters for get_table_response - specify one of op or row, not both') if (opts[:op] && opts[:row]) || (!opts[:op] && !opts[:row])
  return nil if self[:table_inputs].nil?

  target_table = self[:table_inputs].select { |ti| (ti[:key].to_sym == var.to_sym) }
  return nil if target_table.empty?

  if opts[:op]
    raise TableCellUndefined.new("Invalid parameters for get_table_response - an :op option cannot be specified for a table that doesn't have operations corresponding to its rows") if target_table.first[:opid] < 0

    opid = Operation.find(opts[:op]).id # return op.id if passed an operation or the id itself
    target_input_cell = target_table.find { |ti| ti[:opid] == opid }
  elsif opts[:row]
    target_input_cell = target_table.find { |ti| ti[:row] == opts[:row] }
  end
  raise TableCellUndefined if target_input_cell.nil?

  (target_input_cell[:type] == 'number' ? target_input_cell[:value].to_f : target_input_cell[:value])
end

#responsesHash

Returns a hash of user responses, each under the var name specified in the ShowBlock where the response was collected. Table responses are stored in this hash as a list in order of the rows of the table.

Returns:

  • (Hash)

    the response hash with all user input



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/krill/show_response.rb', line 66

def responses
  inline_responses = select { |key, value| key != :table_inputs && key != :timestamp && !is_upload?(key) }

  upload_response_keys = select { |key, value| is_upload?(key) }.keys
  upload_responses = {}
  upload_response_keys.each do |key|
    upload_responses[key] = get_upload_response(key)
  end

  table_response_keys = self[:table_inputs] ? self[:table_inputs].map { |ti| ti[:key] }.uniq : []
  table_responses = {}
  table_response_keys.each do |key|
    table_responses[key.to_sym] = get_table_responses_column(key)
  end

  inline_responses.merge(table_responses).merge(upload_responses)
end

#timestampInteger

Returns a Unix timestamp of the timepoint when the showblock associated with this ShowResponse was seen and interacted with by the technician

Returns:

  • (Integer)

    Unix timestamp as seconds since 1970



88
89
90
# File 'lib/krill/show_response.rb', line 88

def timestamp
  self[:timestamp]
end