Sha256: af31e375b738e2c03a65c451b7789c8b04a5728efacd16217aa6816b24395e0e

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

require_relative 'discover'

module Csv2hash
  module Validator
    include Discover

    def validate_rules y=nil
      find_positions!

      definition.cells.each do |cell|
        _y, x = position cell.rules.fetch(:position)
        begin
          validate_cell (_y||y), x, cell
        rescue => e
          self.errors << { y: (_y||y), x: x, message: e.message, key: cell.rules.fetch(:key) }
          raise if break_on_failure
        end
      end
    end

    def valid?() self.errors.empty?; end

    protected

    def validate_cell y, x, cell
      value = data_source[y][x] rescue nil
      begin
        raise unless value unless cell.rules.fetch :allow_blank
        if (extra_validator = cell.rules.fetch :extra_validator) && extra_validator.kind_of?(ExtraValidator)
          raise unless extra_validator.valid? cell.rules, value
        else
          if value && (values = cell.rules.fetch :values)
            if values.class == Range
              raise unless values.include?(value.to_f)
            else
              raise unless values.include?(value)
            end
          end
        end
      rescue => e
        raise message(cell, y, x)
      end
    end

    def message cell, y, x
      msg = cell.rules.fetch(:message).tap do |msg|
        cell.rules.each { |key, value| msg.gsub!(":#{key.to_s}", value.to_s) unless key == :position }
      end
      msg.gsub ':position', "[#{y}, #{x}]"
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
csv2hash-0.6.4 lib/csv2hash/validator.rb
csv2hash-0.6.3 lib/csv2hash/validator.rb
csv2hash-0.6.2 lib/csv2hash/validator.rb