Class: Tabledata::CoercedRow

Inherits:
Row
  • Object
show all
Defined in:
lib/tabledata/coerced_row.rb

Instance Attribute Summary collapse

Attributes inherited from Row

#data, #index, #table

Instance Method Summary collapse

Methods inherited from Row

#[], #at, #at_accessor, #at_header, #at_index, #each, #fetch, #size, #slice, #to_a, #to_h, #values_at

Constructor Details

#initialize(table, index, data, column_errors = {}, row_errors = []) ⇒ CoercedRow

Returns a new instance of CoercedRow.


11
12
13
14
15
# File 'lib/tabledata/coerced_row.rb', line 11

def initialize(table, index, data, column_errors={}, row_errors=[])
  super(table, index, data)
  @column_errors = column_errors
  @row_errors    = row_errors
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Allow reading and writing cell values by their accessor name.

Raises:

  • (ArgumentError)

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tabledata/coerced_row.rb', line 18

def method_missing(name, *args, &block)
  return super unless @table.accessors?

  name              =~ /^(\w+)(=)?$/
  name_mod, assign  = $1, $2
  index             = @table.index_for_accessor(name_mod)
  arg_count         = assign ? 1 : 0

  return super unless index

  raise ArgumentError, "Wrong number of arguments (#{args.size} for #{arg_count})" if args.size > arg_count

  if assign then
    raise "Coercions not yet implemented" # todo, coerce data when set
    @data[index] = args.first
  else
    @data[index]
  end
end

Instance Attribute Details

#column_errorsObject (readonly)

Returns the value of attribute column_errors


8
9
10
# File 'lib/tabledata/coerced_row.rb', line 8

def column_errors
  @column_errors
end

#row_errorsObject (readonly)

Returns the value of attribute row_errors


9
10
11
# File 'lib/tabledata/coerced_row.rb', line 9

def row_errors
  @row_errors
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)

38
39
40
# File 'lib/tabledata/coerced_row.rb', line 38

def valid?
  @column_errors.empty? && @row_errors.empty?
end