Module: FightCSV::Record::InstanceMethods
- Defined in:
- lib/fight_csv/record.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/fight_csv/record.rb', line 78
def method_missing(meth, *args, &block)
if field = schema.fields.find { |field| /#{field.identifier}(=)?/ === meth }
if $1 == '='
@row[field.identifier] = args.first
else
@row[field.identifier]
end
else
super
end
end
|
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
41
42
43
|
# File 'lib/fight_csv/record.rb', line 41
def errors
@errors
end
|
#row ⇒ Object
Returns the value of attribute row.
40
41
42
|
# File 'lib/fight_csv/record.rb', line 40
def row
@row
end
|
Instance Method Details
#fields ⇒ Object
54
55
56
|
# File 'lib/fight_csv/record.rb', line 54
def fields
Hash[schema.fields.map { |field| [field.identifier, self.send(field.identifier)] }]
end
|
#initialize(row = nil, options = {}) ⇒ Object
44
45
46
47
|
# File 'lib/fight_csv/record.rb', line 44
def initialize(row = nil,options = {})
@schema ||= self.class.schema
self.row = row if row
end
|
#schema=(schema) ⇒ Object
58
59
60
61
|
# File 'lib/fight_csv/record.rb', line 58
def schema=(schema)
super
self.row = @raw_row
end
|
#valid? ⇒ Boolean
63
64
65
66
67
|
# File 'lib/fight_csv/record.rb', line 63
def valid?
validation = self.validate
@errors = validate[:errors]
validation[:valid]
end
|
#validate ⇒ Object
69
70
71
72
73
74
75
76
|
# File 'lib/fight_csv/record.rb', line 69
def validate
self.schema.fields.inject({valid: true, errors: []}) do |validation_hash, field|
validation_of_field = field.validate(@raw_row, @header)
validation_hash[:valid] &&= validation_of_field[:valid]
validation_hash[:errors] << validation_of_field[:error] if validation_of_field[:error]
validation_hash
end
end
|