Class: Importex::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/importex/column.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Column) initialize(name, options = {})



5
6
7
8
9
10
# File 'lib/importex/column.rb', line 5

def initialize(name, options = {})
  @name = name
  @type = options[:type]
  @format = [options[:format]].compact.flatten
  @required = options[:required]
end

Instance Attribute Details

- (Object) name (readonly)

Returns the value of attribute name



3
4
5
# File 'lib/importex/column.rb', line 3

def name
  @name
end

Instance Method Details

- (Object) cell_value(str, row_number)



12
13
14
15
16
17
# File 'lib/importex/column.rb', line 12

def cell_value(str, row_number)
  validate_cell(str)
  @type ? @type.importex_value(str) : str
rescue InvalidCell => e
  raise InvalidCell, "#{str} (column #{name}, row #{row_number+1}) does not match required format: #{e.message}"
end

- (Boolean) match_format?(str, format)



25
26
27
28
29
30
31
# File 'lib/importex/column.rb', line 25

def match_format?(str, format)
  case format
  when String then str == format
  when Regexp then str =~ format
  when Proc then format.call(str)
  end
end

- (Boolean) required?



33
34
35
# File 'lib/importex/column.rb', line 33

def required?
  @required
end

- (Object) validate_cell(str)



19
20
21
22
23
# File 'lib/importex/column.rb', line 19

def validate_cell(str)
  if @format && !@format.empty? && !@format.any? { |format| match_format?(str, format) }
    raise InvalidCell, @format.reject { |r| r.kind_of? Proc }.inspect
  end
end