Class: Importer::Row
- Inherits:
-
Object
- Object
- Importer::Row
- Defined in:
- lib/iron/import/row.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Attributes.
-
#line ⇒ Object
readonly
Attributes.
-
#values ⇒ Object
readonly
Attributes.
Instance Method Summary collapse
-
#[](column_key) ⇒ Object
Returns the value of a column.
- #add_error(msg) ⇒ Object
-
#all?(*keys) ⇒ Boolean
True when all columns have a non-nil value, useful in filtering out junk rows.
-
#empty? ⇒ Boolean
True when all row columns have nil values.
-
#error_map ⇒ Object
Return a map of column key to Error, intended for use in error reporting.
- #has_errors? ⇒ Boolean
-
#initialize(importer, line, value_hash = nil) ⇒ Row
constructor
A new instance of Row.
- #set_values(value_hash) ⇒ Object
-
#to_h ⇒ Object
This row’s values as a hash of :column_key => <parsed + validated value>.
- #to_hash ⇒ Object
-
#to_s ⇒ Object
The row’s name, e.g.
Constructor Details
#initialize(importer, line, value_hash = nil) ⇒ Row
Returns a new instance of Row.
11 12 13 14 15 16 17 |
# File 'lib/iron/import/row.rb', line 11 def initialize(importer, line, value_hash = nil) @importer = importer @line = line set_values(value_hash) @errors = [] end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Attributes
6 7 8 |
# File 'lib/iron/import/row.rb', line 6 def errors @errors end |
#line ⇒ Object (readonly)
Attributes
6 7 8 |
# File 'lib/iron/import/row.rb', line 6 def line @line end |
#values ⇒ Object (readonly)
Attributes
6 7 8 |
# File 'lib/iron/import/row.rb', line 6 def values @values end |
Instance Method Details
#[](column_key) ⇒ Object
Returns the value of a column.
49 50 51 |
# File 'lib/iron/import/row.rb', line 49 def [](column_key) @values[column_key] end |
#add_error(msg) ⇒ Object
64 65 66 |
# File 'lib/iron/import/row.rb', line 64 def add_error(msg) @importer.add_error(msg, :row => self) end |
#all?(*keys) ⇒ Boolean
True when all columns have a non-nil value, useful in filtering out junk rows. Pass in one or more keys to check only those keys for presence.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/iron/import/row.rb', line 25 def all?(*keys) keys.flatten! if keys.any? # Check only the specified keys valid = true keys.each do |key| unless @values.has_key?(key) raise "Unknown column key :#{key} in call to Row#all?" end valid = valid && !@values[key].nil? end valid else # Check all value keys @values.values.all? {|v| !v.nil? } end end |
#empty? ⇒ Boolean
True when all row columns have nil values.
44 45 46 |
# File 'lib/iron/import/row.rb', line 44 def empty? @values.values.all?(&:nil?) end |
#error_map ⇒ Object
Return a map of column key to Error, intended for use in error reporting.
73 74 75 76 77 78 79 |
# File 'lib/iron/import/row.rb', line 73 def error_map map = {} @errors.each do |err| map[err.column.key] = err end map end |
#has_errors? ⇒ Boolean
68 69 70 |
# File 'lib/iron/import/row.rb', line 68 def has_errors? @errors && @errors.count > 0 end |
#set_values(value_hash) ⇒ Object
19 20 21 |
# File 'lib/iron/import/row.rb', line 19 def set_values(value_hash) @values = value_hash end |
#to_h ⇒ Object
This row’s values as a hash of :column_key => <parsed + validated value>
59 60 61 |
# File 'lib/iron/import/row.rb', line 59 def to_h @values.dup end |
#to_hash ⇒ Object
62 |
# File 'lib/iron/import/row.rb', line 62 def to_hash ; to_h ; end |
#to_s ⇒ Object
The row’s name, e.g. ‘Row 4’
54 55 56 |
# File 'lib/iron/import/row.rb', line 54 def to_s "Row #{@line}" end |