module FightCSV::Record::InstanceMethods

Attributes

errors[R]
row[RW]

Public Class Methods

new(row = nil,options = {}) click to toggle source
# File lib/fight_csv/record.rb, line 44
def initialize(row = nil,options = {})
  @schema ||= self.class.schema
  self.row = row if row
end

Public Instance Methods

fields() click to toggle source
# File lib/fight_csv/record.rb, line 54
def fields
  Hash[schema.fields.map { |field| [field.identifier, self.send(field.identifier)] }]
end
method_missing(meth, *args, &block) click to toggle source
Calls superclass method
# 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
row=(raw_row) click to toggle source
# File lib/fight_csv/record.rb, line 49
def row=(raw_row)
  @raw_row = raw_row
  @row = Hash[self.schema.fields.map { |field| [field.identifier,field.process(@raw_row, @header)] }]
end
schema=(schema) click to toggle source
Calls superclass method
# File lib/fight_csv/record.rb, line 58
def schema=(schema)
  super
  self.row = @raw_row
end
valid?() click to toggle source
# File lib/fight_csv/record.rb, line 63
def valid?
  validation = self.validate
  @errors = validate[:errors]
  validation[:valid]
end
validate() click to toggle source
# 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