Class: Krill::Table

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

Overview

A class that makes making tables for calls to 'show' easier.

Examples:

Create a table, add rows, and display it.

t = Table.new(
  a: "First column",
  b: "Second column"
)

t.a(1).b(2).append
t.a(3).b(4).append

show do
  table t.all.render
end

Instance Method Summary collapse

Constructor Details

#initialize(columns = {}) ⇒ Table

Create a table object, which makes making tables for calls to show easier.

Parameters:

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

    A list hash of the form { a: String, b: String, ... } defining the columns of the table and their headings.



29
30
31
32
33
34
35
36
# File 'lib/krill/table.rb', line 29

def initialize(columns = {})
  @columns = columns
  @selection = {}
  @rows = []
  @choice = []
  @from = 0
  @to = 100_000
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object (private)

Each column in the table can be used as a method.

Examples:

Suppose a table t has a row named :x. Then you can do

t.x("whatever").append


174
175
176
177
178
179
180
181
182
# File 'lib/krill/table.rb', line 174

def method_missing(m, *args, &block)

  if @columns[m]
    set(m, args[0])
  else
    super
  end

end

Instance Method Details

#add_column(name, values) ⇒ Object

Append a column to the table with a list of values

Parameters:

  • name (String)

    the heading of this column

  • values (Array)

    the values of the cells of this column



133
134
135
136
137
138
139
140
# File 'lib/krill/table.rb', line 133

def add_column(name, values)
  column(name.to_sym, name)
  values.each_with_index do |v, i|
    @rows[i] ||= {}
    @rows[i][name.to_sym] = v
  end
  self
end

#add_response_column(name, defaults, opts = {}) ⇒ Table

Append a column to the table which accepts user input Response data is stored under the key table_input in the data hash returned by show Note: data hash may not get properly populated in the protocol tester

Parameters:

  • name (String)

    the heading of this column

  • defaults (Array)

    the default values of the cells of this column

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

    additional options

Options Hash (opts):

  • :key (String)

    the key that can be used to access response data from ShowResponse hash

  • :type (String)

    defines type of user input -- can be either 'number' or 'text'

Returns:

  • (Table)

    The table, can be chained



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/krill/table.rb', line 152

def add_response_column(name, defaults, opts = {})
  default_opts = { key: name, type: 'number' }
  opts.merge default_opts
  # Although we are creating an input table that is not associated to an operationslist
  # we rely on the operationslist table input machinery (in operations_list_input_table)
  # which requires an opid field so data can be automatically placed in the op.temporary hash
  # of each associated op as a convienence.
  # Putting unique negative numbers here will allow that the ShowResponse still will be populated with values
  # even though there are no op.temporary hashes to fill (since no operations have negative ids)
  values = defaults.each_with_index.map do |default, idx|
    { type: opts[:type], operation_id: (-1 * idx - 1), key: opts[:key], default: default || 0 }
  end
  add_column(name, values)
end

#allTable

Select all columns.

Returns:

  • (Table)

    The table, can be chained.



84
85
86
87
# File 'lib/krill/table.rb', line 84

def all
  @choice = @columns.keys
  self
end

#appendTable

Append a row defined by the currently selectors.

Returns:

  • (Table)

    The table, can be chained.



76
77
78
79
80
# File 'lib/krill/table.rb', line 76

def append
  @rows << @selection
  clear
  self
end

#choose(columns) ⇒ Table

Choose which columns to display in a subsequent call to render.

Parameters:

  • columns (Array)

    An array of column names, as in [:x, :y, :z].

Returns:

  • (Table)

    The table, can be chained.



92
93
94
95
# File 'lib/krill/table.rb', line 92

def choose(columns)
  @choice = columns
  self
end

#clearTable

Clear the currently selected columns, and result from and to.

Returns:

  • (Table)

    The table, can be chained.



67
68
69
70
71
72
# File 'lib/krill/table.rb', line 67

def clear
  @selection = {}
  @from = 0
  @to = 100_000
  self
end

#column(name, heading) ⇒ Table

Add a column to the table.

Parameters:

  • name (symbol)

    The name of the column.

  • heading (String)

    A string to use for the heading of the column.

Returns:

  • (Table)

    The table with the heading added to it, can be chained.



43
44
45
46
# File 'lib/krill/table.rb', line 43

def column(name, heading)
  @columns[name] = heading
  self
end

#from(i) ⇒ Table

Define the row to start with in a subsequent call to render.

Parameters:

  • i (Fixnum)

    The column to start with.

Returns:

  • (Table)

    The table, can be chained.



100
101
102
103
104
105
# File 'lib/krill/table.rb', line 100

def from(i)
  raise "Table: from(#{i}) is out of range" unless i < @rows.length

  @from = i
  self
end

#has_column?(key) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/krill/table.rb', line 52

def has_column?(key)
  @columns[key] != nil
end

#has_columns?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/krill/table.rb', line 48

def has_columns?
  !@columns.keys.empty?
end

#renderArray

Return a matrix (Array or Arrays) representing the table for use in a call to 'show'.

Returns:

  • (Array)

    The table as an array or arrays.



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/krill/table.rb', line 117

def render

  heading = @choice.collect { |c| @columns[c] }

  body = (@from..[@to, @rows.length].min - 1).collect do |i|
    @choice.collect { |c| @rows[i][c] }
  end

  [heading] + body

end

#set(name, value) ⇒ Object

Set a value in the current row

Parameters:

  • name (symbol)

    The name of the column

  • value (object)

    Value to set



60
61
62
63
# File 'lib/krill/table.rb', line 60

def set(name, value)
  @selection[name] = value
  self
end

#to(i) ⇒ Table

Define the row to end with (actually i-1) in a subsequent call to render.

Parameters:

  • i (Fixnum)

    The column to end just before.

Returns:

  • (Table)

    The table, can be chained.



110
111
112
113
# File 'lib/krill/table.rb', line 110

def to(i)
  @to = i
  self
end