Class: Importer::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/iron/import/row.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#errorsObject (readonly)

Attributes



6
7
8
# File 'lib/iron/import/row.rb', line 6

def errors
  @errors
end

#lineObject (readonly)

Attributes



6
7
8
# File 'lib/iron/import/row.rb', line 6

def line
  @line
end

#valuesObject (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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


44
45
46
# File 'lib/iron/import/row.rb', line 44

def empty?
  @values.values.all?(&:nil?)
end

#error_mapObject

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

Returns:

  • (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_hObject

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_hashObject



62
# File 'lib/iron/import/row.rb', line 62

def to_hash ; to_h ; end

#to_sObject

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