Class: Importer::Error

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, context = {}) ⇒ Error

Returns a new instance of Error.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/iron/import/error.rb', line 53

def initialize(text, context = {})
  @text = text.to_s
  @row = context[:row] || Error.context_row
  @column = context[:column] || Error.context_column
  @value = context[:value] || Error.context_value
  
  @row.errors << self if @row
  @column.errors << self if @column
  
  Error.error_occurred!
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



5
6
7
# File 'lib/iron/import/error.rb', line 5

def column
  @column
end

#rowObject (readonly)

Returns the value of attribute row.



5
6
7
# File 'lib/iron/import/error.rb', line 5

def row
  @row
end

#textObject (readonly)

Returns the value of attribute text.



5
6
7
# File 'lib/iron/import/error.rb', line 5

def text
  @text
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/iron/import/error.rb', line 5

def value
  @value
end

Class Method Details

.context_columnObject



41
42
43
# File 'lib/iron/import/error.rb', line 41

def self.context_column
  @context_column
end

.context_rowObject



37
38
39
# File 'lib/iron/import/error.rb', line 37

def self.context_row
  @context_row
end

.context_valueObject



45
46
47
# File 'lib/iron/import/error.rb', line 45

def self.context_value
  @context_value
end

.error_occurred!Object



49
50
51
# File 'lib/iron/import/error.rb', line 49

def self.error_occurred!
  @error_occurred = true
end

.with_context(importer, row, column, val) ⇒ Object

Block wrapper to set error context for any errors generated within the block



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/iron/import/error.rb', line 8

def self.with_context(importer, row, column, val)
  # Set new context
  old_row = @context_row
  @context_row = row
  old_col = @context_column
  @context_column = column
  old_val = @context_value
  @context_value = val
  old_err = @error_occurred
  @error_occurred = false
  
  # Run the block, catch raised exceptions as errors
  begin
    yield
  rescue RuntimeError => e
    # Old-style way of registering errors was to just raise 'foo'
    importer.add_exception(e)
  end
  had_error = @error_occurred
  
  # Reset to old context
  @context_row = old_row
  @context_column = old_col
  @context_value = old_val
  @error_occurred = old_err
  
  return had_error
end

Instance Method Details

#for_context?(context) ⇒ Boolean

Returns true if this error is for the given context, where context can be a Row or Importer instance.

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
# File 'lib/iron/import/error.rb', line 94

def for_context?(context)
  case context
  when Row
    return @row == context
  else
    return true
  end  
end

#importer_level?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/iron/import/error.rb', line 88

def importer_level?
  level == :importer
end

#levelObject

Returns the level at which this error occurred, one of :row, :importer



79
80
81
82
# File 'lib/iron/import/error.rb', line 79

def level
  return :row if @row
  return :importer
end

#row_level?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/iron/import/error.rb', line 84

def row_level?
  level == :row
end

#summaryObject



65
66
67
68
69
70
71
# File 'lib/iron/import/error.rb', line 65

def summary
  summary = ''
  if @row
    summary += "#{@row}: "
  end
  summary + @text
end

#to_sObject



73
74
75
# File 'lib/iron/import/error.rb', line 73

def to_s
  summary
end