Class: Importex::Column
- Inherits:
-
Object
- Object
- Importex::Column
- Defined in:
- lib/importex/column.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #cell_value(str, row_number) ⇒ Object
-
#initialize(name, options = {}) ⇒ Column
constructor
A new instance of Column.
- #match_format?(str, format) ⇒ Boolean
- #required? ⇒ Boolean
- #validate_cell(str) ⇒ Object
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, = {}) @name = name @type = [:type] @format = [[:format]].compact.flatten @required = [:required] end |
Instance Attribute Details
#name ⇒ Object (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.}" end |
#match_format?(str, format) ⇒ Boolean
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 |
#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 |