Class: Hotwire::Response::Base

Inherits:
Base
  • Object
show all
Defined in:
lib/hotwire/response/base.rb

Direct Known Subclasses

Csv, Html, Invalid, Json

Instance Attribute Summary collapse

Attributes inherited from Base

#errors

Instance Method Summary collapse

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

#columnsObject (readonly)

Returns the value of attribute columns.



4
5
6
# File 'lib/hotwire/response/base.rb', line 4

def columns
  @columns
end

#dataObject (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.

Raises:

  • (ArgumentError)


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

#bodyObject

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