class FightCSV::Field

Attributes

matcher[RW]

Public Class Methods

new(matcher, options = {}) click to toggle source
# File lib/fight_csv/field.rb, line 9
def initialize(matcher, options = {})
        @matcher = matcher
end

Public Instance Methods

identifier() click to toggle source
Calls superclass method
# File lib/fight_csv/field.rb, line 13
def identifier
  if super
    super
  else
    case self.matcher
    when String
      self.matcher.downcase.to_sym
    else
      raise ArgumentError, "Please specify an identifier"
    end
  end
end
ivar_symbol() click to toggle source
# File lib/fight_csv/field.rb, line 60
def ivar_symbol
        :"@#{self.identifier}"
end
match(row, header = nil) click to toggle source
# File lib/fight_csv/field.rb, line 43
def match(row, header = nil)
        case self.matcher
        when Integer
                row[matcher-1]
        else
                raise ArgumentError, 'No header is provided, but a matcher other than an Integer requires one' unless header
                index = header.index  { |n| self.matcher === n }
                index ? row[index] : nil
        end
end
process(row, header = nil) click to toggle source
# File lib/fight_csv/field.rb, line 54
def process(row, header = nil)
        if match = self.match(row, header)
                self.converter ? self.converter.call(match) : match
        end
end
validate(row, header = nil) click to toggle source
# File lib/fight_csv/field.rb, line 26
def validate(row, header = nil)
        match = self.match(row, header).to_s
        if self.validator.respond_to?(:call) 
                result = self.validator.call(match)
                verb = "pass"
        else
                result = (self.validator === match)
                verb = "match"
        end

        unless result
                { valid: false, error: "#{self.identifier.inspect} must #{verb} #{self.validator}, but was #{match.inspect}"}
        else
                { valid: true }
        end
end