Class: Tabledata::CustomTable

Inherits:
Table
  • Object
show all
Defined in:
lib/tabledata/custom_table.rb

Constant Summary

Constants inherited from Table

Table::DefaultAccessors, Table::DefaultTableName, Table::InvalidFromFileOptions, Table::ValidOptions

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Table

#accessor_columns, #accessors, #column_accessors, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Table

#==, #[], #accessors?, #accessors_from_headers!, #body, #column, #column_accessor, #column_count, #column_name, #columns, #each, #each_column, #each_row, #eql?, #fetch_cell, #fetch_row, #footer, #footer?, #format, from_data, #hash, #headers, #headers?, #index_for_accessor, #index_for_column, #index_for_header, #inspect, #row, #size, #to_a

Constructor Details

#initialize(options) ⇒ CustomTable


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tabledata/custom_table.rb', line 30

def initialize(options)
  definition = self.class.definition
  columns    = definition.columns
  options    = options.merge(accessors: columns.map(&:accessor), name: definition.table_name) { |key,v1,v2|
    if key == :accessors
      raise "Can't handle reordering of accessors - don't redefine accessors in CustomTables for now"
    elsif key == :name
      v1 || v2
    end
  }

  super(options)

  @table_errors  = []
  @original_data = @data
  @original_rows = @rows

  @rows = @rows.map.with_index { |row, row_index|
    column_errors = {}
    coerced_values = *row.map.with_index { |value, column_index|
      column = columns[column_index]
      value, errors = column.coerce(value)
      column_errors[column.accessor] = errors unless errors.empty?
    }
    row_errors = []
    CoercedRow.new(self, row_index, coerced_values, column_errors, row_errors)
  }
  @data = @rows.map(&:to_a)
end

Class Attribute Details

.definitionObject (readonly)

Returns the value of attribute definition


10
11
12
# File 'lib/tabledata/custom_table.rb', line 10

def definition
  @definition
end

Instance Attribute Details

#original_dataObject (readonly)

Returns the value of attribute original_data


28
29
30
# File 'lib/tabledata/custom_table.rb', line 28

def original_data
  @original_data
end

#original_rowsObject (readonly)

Returns the value of attribute original_rows


28
29
30
# File 'lib/tabledata/custom_table.rb', line 28

def original_rows
  @original_rows
end

#table_errorsObject (readonly)

Returns the value of attribute table_errors


28
29
30
# File 'lib/tabledata/custom_table.rb', line 28

def table_errors
  @table_errors
end

Class Method Details

.from_file(path, options = nil) ⇒ Object


21
22
23
24
25
26
# File 'lib/tabledata/custom_table.rb', line 21

def self.from_file(path, options=nil)
  options        ||= {}
  options[:name] ||= @definition.table_name

  super(path, options)
end

.identifierObject


17
18
19
# File 'lib/tabledata/custom_table.rb', line 17

def self.identifier
  @definition.identifier
end

.table_nameObject


13
14
15
# File 'lib/tabledata/custom_table.rb', line 13

def self.table_name
  @definition.table_name
end

Instance Method Details

#<<(row) ⇒ Object

Raises:


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/tabledata/custom_table.rb', line 60

def <<(row)
  columns       = self.class.definition.columns
  index         = @data.size
  column_errors = {}

  begin
    row = row.to_ary
  rescue NoMethodError
    raise ArgumentError, "Row must be provided as Array or respond to `to_ary`, but got #{row.class} in row #{index}" unless row.respond_to?(:to_ary)
    raise
  end
  raise InvalidColumnCount.new(index, row.size, column_count) if @data.first && row.size != @data.first.size

  if index > 0 || !@has_headers
    coerced_values = *row.map.with_index { |value, column_index|
      column                         = columns[column_index]
      value, errors                  = column.coerce(value)
      column_errors[column.accessor] = errors unless errors.empty?

      value
    }
    row_errors = []
  else
    coerced_values = row.dup
    row_errors     = []
  end

  @original_data << row
  @original_rows << Row.new(self, index, row)
  @data << coerced_values
  @rows << CoercedRow.new(self, index, coerced_values, column_errors, row_errors)

  self
end

#valid?Boolean


95
96
97
# File 'lib/tabledata/custom_table.rb', line 95

def valid?
  @table_errors.empty? && @rows.all?(&:valid?)
end