Sha256: 631f9d3319c07192c5ea760ae009d12231764e944ab48b850e35a9a9a3c9fe26

Contents?: true

Size: 1.14 KB

Versions: 9

Compression:

Stored size: 1.14 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
      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

9 entries across 9 versions & 1 rubygems

Version Path
csv2hash-0.7.5 lib/csv2hash/structure_validator.rb
csv2hash-0.7.3 lib/csv2hash/structure_validator.rb
csv2hash-0.7.2 lib/csv2hash/structure_validator.rb
csv2hash-0.7.1 lib/csv2hash/structure_validator.rb
csv2hash-0.7.0 lib/csv2hash/structure_validator.rb
csv2hash-0.6.8 lib/csv2hash/structure_validator.rb
csv2hash-0.6.7 lib/csv2hash/structure_validator.rb
csv2hash-0.6.6 lib/csv2hash/structure_validator.rb
csv2hash-0.6.5 lib/csv2hash/structure_validator.rb