Class: CSVRow
- Inherits:
-
Object
show all
- Includes:
- Normalizer, Card::Model::SaveHelper
- Defined in:
- lib/csv_row.rb,
lib/csv_row/normalizer.rb
Overview
Inherit from CSVRow to describe and process a csv row. CSVFile creates an instance of CSVRow for every row and calls #execute_import on it
Defined Under Namespace
Modules: Normalizer
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Normalizer
#comma_list_to_pointer, #to_html
Constructor Details
#initialize(row, index, import_manager = nil) ⇒ CSVRow
Returns a new instance of CSVRow.
36
37
38
39
40
41
42
43
|
# File 'lib/csv_row.rb', line 36
def initialize row, index, import_manager=nil
@row = row
@import_manager = import_manager || ImportManager.new(nil)
@extra_data = @import_manager.(index)
@abort_on_error = true
@row_index = index merge_corrections
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
160
161
162
|
# File 'lib/csv_row.rb', line 160
def method_missing method_name, *args
respond_to_missing?(method_name) ? @row[method_name.to_sym] : super
end
|
Class Attribute Details
.columns ⇒ Object
Returns the value of attribute columns.
20
21
22
|
# File 'lib/csv_row.rb', line 20
def columns
@columns
end
|
.required ⇒ Object
Returns the value of attribute required.
20
21
22
|
# File 'lib/csv_row.rb', line 20
def required
@required
end
|
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
31
32
33
|
# File 'lib/csv_row.rb', line 31
def errors
@errors
end
|
#import_manager ⇒ Object
Returns the value of attribute import_manager.
31
32
33
|
# File 'lib/csv_row.rb', line 31
def import_manager
@import_manager
end
|
#name ⇒ Object
Returns the value of attribute name.
32
33
34
|
# File 'lib/csv_row.rb', line 32
def name
@name
end
|
#row_index ⇒ Object
Returns the value of attribute row_index.
31
32
33
|
# File 'lib/csv_row.rb', line 31
def row_index
@row_index
end
|
#status ⇒ Object
Returns the value of attribute status.
32
33
34
|
# File 'lib/csv_row.rb', line 32
def status
@status
end
|
Class Method Details
.normalize(key) ⇒ Object
22
23
24
|
# File 'lib/csv_row.rb', line 22
def normalize key
@normalize && @normalize[key]
end
|
.validate(key) ⇒ Object
26
27
28
|
# File 'lib/csv_row.rb', line 26
def validate key
@validate && @validate[key]
end
|
Instance Method Details
#[](key) ⇒ Object
152
153
154
|
# File 'lib/csv_row.rb', line 152
def [] key
@row[key]
end
|
#check_required_fields ⇒ Object
84
85
86
87
88
|
# File 'lib/csv_row.rb', line 84
def check_required_fields
required.each do |key|
error "value for #{key} missing" unless @row[key].present?
end
end
|
#collect_errors ⇒ Object
90
91
92
93
94
95
96
|
# File 'lib/csv_row.rb', line 90
def collect_errors
@abort_on_error = false
yield
skip :failed if errors?
ensure
@abort_on_error = true
end
|
#columns ⇒ Object
119
120
121
|
# File 'lib/csv_row.rb', line 119
def columns
self.class.columns
end
|
#error(msg) ⇒ Object
110
111
112
113
|
# File 'lib/csv_row.rb', line 110
def error msg
@import_manager.report_error msg
skip :failed if @abort_on_error
end
|
#errors? ⇒ Boolean
102
103
104
|
# File 'lib/csv_row.rb', line 102
def errors?
@import_manager.errors? self
end
|
#execute_import ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/csv_row.rb', line 66
def execute_import
@import_manager.handle_import(self) do
prepare_import
ImportLog.debug "start import"
import
end
rescue => e
ImportLog.debug "import failed: #{e.message}"
ImportLog.debug e.backtrace
raise e
end
|
#fields ⇒ Object
156
157
158
|
# File 'lib/csv_row.rb', line 156
def fields
@row
end
|
#label ⇒ Object
49
50
51
52
53
|
# File 'lib/csv_row.rb', line 49
def label
label = "##{@row_index + 1}"
label += ": #{@name}" if @name
label
end
|
#merge_corrections ⇒ Object
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/csv_row.rb', line 55
def merge_corrections
@corrections = @extra_data[:corrections]
@corrections = {} unless @corrections.is_a? Hash
@before_corrected = {}
@corrections.each do |k, v|
next unless v.present?
@before_corrected[k] = @row[k]
@row[k] = v
end
end
|
#method_name(field, type) ⇒ Object
147
148
149
150
|
# File 'lib/csv_row.rb', line 147
def method_name field, type
method_name = "#{type}_#{field}".to_sym
respond_to?(method_name) ? method_name : self.class.send(type, field)
end
|
#normalize ⇒ Object
123
124
125
126
127
|
# File 'lib/csv_row.rb', line 123
def normalize
@row.each do |k, v|
normalize_field k, v
end
end
|
#normalize_field(field, value) ⇒ Object
135
136
137
138
|
# File 'lib/csv_row.rb', line 135
def normalize_field field, value
return unless (method_name = method_name(field, :normalize))
@row[field] = send method_name, value
end
|
#original_row ⇒ Object
45
46
47
|
# File 'lib/csv_row.rb', line 45
def original_row
@row.merge @before_corrected
end
|
#prepare_import ⇒ Object
78
79
80
81
82
|
# File 'lib/csv_row.rb', line 78
def prepare_import
collect_errors { check_required_fields }
normalize
collect_errors { validate }
end
|
#required ⇒ Object
115
116
117
|
# File 'lib/csv_row.rb', line 115
def required
self.class.required == :all ? columns : self.class.required
end
|
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
164
165
166
|
# File 'lib/csv_row.rb', line 164
def respond_to_missing? method_name, _include_private=false
@row.keys.include? method_name
end
|
#skip(status = :skipped) ⇒ Object
98
99
100
|
# File 'lib/csv_row.rb', line 98
def skip status=:skipped
throw :skip_row, status
end
|
#validate ⇒ Object
129
130
131
132
133
|
# File 'lib/csv_row.rb', line 129
def validate
@row.each do |k, v|
validate_field k, v
end
end
|
#validate_field(field, value) ⇒ Object
140
141
142
143
144
|
# File 'lib/csv_row.rb', line 140
def validate_field field, value
return unless (method_name = method_name(field, :validate))
return if send method_name, value
error "row #{@row_index + 1}: invalid value for #{field}: #{value}"
end
|