Sha256: 87a78daa808741ad7817d085e5c70f80116fadd348410db1bbec1bfb3ccbefa4

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

require 'active_support/core_ext'

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
    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

4 entries across 4 versions & 1 rubygems

Version Path
csv2hash-0.6.4 lib/csv2hash/structure_validator.rb
csv2hash-0.6.3 lib/csv2hash/structure_validator.rb
csv2hash-0.6.2 lib/csv2hash/structure_validator.rb
csv2hash-0.6.1 lib/csv2hash/structure_validator.rb