Class: Krill::Table
- Inherits:
-
Object
- Object
- Krill::Table
- Defined in:
- lib/krill/table.rb
Overview
A class that makes making tables for calls to 'show' easier.
Instance Method Summary collapse
-
#add_column(name, values) ⇒ Object
Append a column to the table with a list of values.
-
#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 byshow
Note: data hash may not get properly populated in the protocol tester. -
#all ⇒ Table
Select all columns.
-
#append ⇒ Table
Append a row defined by the currently selectors.
-
#choose(columns) ⇒ Table
Choose which columns to display in a subsequent call to render.
-
#clear ⇒ Table
Clear the currently selected columns, and result from and to.
-
#column(name, heading) ⇒ Table
Add a column to the table.
-
#from(i) ⇒ Table
Define the row to start with in a subsequent call to render.
- #has_column?(key) ⇒ Boolean
- #has_columns? ⇒ Boolean
-
#initialize(columns = {}) ⇒ Table
constructor
Create a table object, which makes making tables for calls to show easier.
-
#render ⇒ Array
Return a matrix (Array or Arrays) representing the table for use in a call to 'show'.
-
#set(name, value) ⇒ Object
Set a value in the current row.
-
#to(i) ⇒ Table
Define the row to end with (actually i-1) in a subsequent call to render.
Constructor Details
#initialize(columns = {}) ⇒ Table
Create a table object, which makes making tables for calls to show easier.
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.
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
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
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 |
#all ⇒ Table
Select all columns.
84 85 86 87 |
# File 'lib/krill/table.rb', line 84 def all @choice = @columns.keys self end |
#append ⇒ Table
Append a row defined by the currently selectors.
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.
92 93 94 95 |
# File 'lib/krill/table.rb', line 92 def choose(columns) @choice = columns self end |
#clear ⇒ Table
Clear the currently selected columns, and result from and to.
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.
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.
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
52 53 54 |
# File 'lib/krill/table.rb', line 52 def has_column?(key) @columns[key] != nil end |
#has_columns? ⇒ Boolean
48 49 50 |
# File 'lib/krill/table.rb', line 48 def has_columns? !@columns.keys.empty? end |
#render ⇒ Array
Return a matrix (Array or Arrays) representing the table for use in a call to 'show'.
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
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.
110 111 112 113 |
# File 'lib/krill/table.rb', line 110 def to(i) @to = i self end |