Sha256: cac6835b85a9e2c41aa6ffad40632a804fe105b6c50987c64a3488d78951cbbf

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

require 'active_support'

require_relative 'structure_validator/deprecation'

module Csv2hash
  module StructureValidator
    include Deprecation

    class ValidationError < StandardError ; end

    MAX_COLUMN = 'max_columns'.freeze
    MIN_COLUMN = 'min_columns'.freeze
    RULES_NAME = [ MIN_COLUMN, MAX_COLUMN ]

    def validate_structure!
      definition.structure_rules.each do |rule, options|
        begin
          rule_instance(rule, options).validate! data_source
        rescue => e
          self.errors << { y: nil, x: nil, message: e.message, key: nil }
          raise if break_on_failure
        end
      end
      nil
    end

    def rule_instance rule, options
      _rule = check_params rule
      begin
        StructureValidator.const_get(_rule.camelize).new(options)
      rescue NameError => e
        raise "Structure rule #{rule} unknow, please use one of these #{RULES_NAME}"
      end
    end

    module Validator
      def validate! source
        source.index { |line| validate_line line }.tap do |line|
          raise ValidationError, error_message(line) unless line.nil?
        end
        true
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
csv2hash-0.7.7 lib/csv2hash/structure_validator.rb
csv2hash-0.7.6 lib/csv2hash/structure_validator.rb