Class: Importex::Column

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Column

Returns a new instance of Column.



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

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#cell_value(str, row_number) ⇒ Object



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

#match_format?(str, format) ⇒ Boolean

Returns:



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

#required?Boolean

Returns:



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

def required?
  @required
end

#validate_cell(str) ⇒ Object



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