Class: Hotwire::Response::Base
- Defined in:
- lib/hotwire/response/base.rb
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Attributes inherited from Base
Instance Method Summary collapse
-
#add_column(type, params = nil) ⇒ Object
(also: #add_col)
Adds a new column to the visualization.
-
#add_columns(columns) ⇒ Object
Adds a colleciton of columns to the visualization
columns
can either be an array of column definitions: add_columns([‘string’, => ‘Column A’], [‘number’, => ‘Column B’]). -
#body ⇒ Object
Placeholder method for the subclasses to overwrite.
-
#initialize(request) ⇒ Base
constructor
Creates a new instance and validates it.
-
#set_data(data) ⇒ Object
Sets the data to be exported.
Methods inherited from Base
#add_error, #valid?, #validate
Constructor Details
#initialize(request) ⇒ Base
Creates a new instance and validates it.
6 7 8 9 10 11 12 13 14 |
# File 'lib/hotwire/response/base.rb', line 6 def initialize(request) super @request = request @columns = [] @data = [] @version = "0.5" @coltypes = [ "boolean", "number", "string", "date", "datetime", "timeofday"] @colkeys = [ :type, :id, :label, :pattern] end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
4 5 6 |
# File 'lib/hotwire/response/base.rb', line 4 def columns @columns end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
4 5 6 |
# File 'lib/hotwire/response/base.rb', line 4 def data @data end |
Instance Method Details
#add_column(type, params = nil) ⇒ Object Also known as: add_col
Adds a new column to the visualization. This method can be invoked as many times as the number of columns to export in the visualiation. Invocations can be chained.
Invocation order is important! The same order will be expected when setting the actual data and to produce the response.
type
must be one of the supported Wire datatypes, or an ArgumentError
will be raised. params
is an optional map to define extra column attributes. These include: :id
, :label
and :pattern
.
26 27 28 29 30 31 32 33 34 |
# File 'lib/hotwire/response/base.rb', line 26 def add_column(type, params=nil) raise ArgumentError.new("Invalid column type: #{type}(#{type.class.name})") if !@coltypes.include?(type) params ||= {} params[:type] = type # TODO: passing a wront type in params bypasses the previous type check. @columns << params.delete_if { |k,v| !@colkeys.include?(k) } return self end |
#add_columns(columns) ⇒ Object
Adds a colleciton of columns to the visualization columns
can either be an array of column definitions:
add_columns(['string', {:id => 'Column A'}], ['number', {:id => 'Column B'}])
or a sample row of data, represented as a hash keyed as column_name => value:
add_columns({'column_a' => 'a1', 'column_b' => 'b1})
column_name keys are expected to be strings.
44 45 46 47 48 49 50 |
# File 'lib/hotwire/response/base.rb', line 44 def add_columns(columns) if columns.first.is_a?(Hash) add_columns_from_data_hash(columns) else add_columns_from_array(columns) end end |
#body ⇒ Object
Placeholder method for the subclasses to overwrite
82 83 |
# File 'lib/hotwire/response/base.rb', line 82 def body end |
#set_data(data) ⇒ Object
Sets the data to be exported. data
can be a 2-dimensional array of plain data, an array of hashes that are keyed column_name => value, or a hash with a :columns key and a :rows key
If passing a 2-dimensional array, the first index should iterate over rows, the second over columns. Column ordering must be the same used in add_col
invokations. Anything that behaves like a 2-dimensional array and supports each
is a perfectly fine alternative.
If passing an array of column_name => value hashes and no columns have been added, a column will automatically be added for each entry in the first row of data. If columns have already been added then the data will be filtered to those columns. column_name keys are expected to be strings.
If passing a hash with :columns key and a :rows key, :columns should be an array of column ids: [‘col_a’, ‘col_b’…‘col_N’] :rows should be a 2-dimensional array of data: [[‘a1’, ‘b1’]…[‘aN’, ‘bN’]] Note that each row of data must be in the same order as the column id array.
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/hotwire/response/base.rb', line 70 def set_data(data) if data.is_a?(Hash) set_data_from_hash(data) elsif data.first.is_a?(Hash) set_data_from_array_of_hashes(data) else set_data_from_array(data) end return self end |