module Anodator::Utils

Public Class Methods

load_input_spec_from_csv_file(file_path) click to toggle source

load input_spec from csv file

File encoding: UTF-8 Columns:

- column identification(string)
- column name(string)
- column type(STRING, NUMERIC or DATE, default is STRING)

Arguments:

- file_path: file path for input spec.

Return:

InputSpec instance
# File lib/anodator/utils.rb, line 16
def self.load_input_spec_from_csv_file(file_path)
  first = true
  header = nil
  spec = []
  CSV.open(file_path, "r") do |row|
    # skip header
    if first
      header = row
      first = false
      next
    end
    spec << { :number => row[0], :name => row[1], :type => row[2] }
  end

  return InputSpec.new(spec)
end
load_output_spec_from_csv_file(file_path, target = Anodator::OutputSpec::TARGET_ERROR, include_no_error = false) click to toggle source

load output_spec from csv file

When target is TARGET_ERROR generate output line by one error, when target is TARGET_DATA generate output line by one data. When set include_no_error is true and target is TARGET_DATA, generate output line has no error.

File encoding: UTF-8 Columns:

- output column identification(string)
- output special column(string)
  - target_numbers(for error)
  - target_names(for one error)
  - target_values(for one error)
  - error_message(for one error)
  - error_level(for one error)
  - error_count(for one data)
  - warning_count(for one data)
  - error_and_warning_count(for one data)

Arguments:

- file_path: file path for output spec.
- target: OutputSpec::TARGET_ERROR or OutputSpec::TARGET_DATA
- include_no_error: true or false(default false)

Return:

OutputSpec instance
# File lib/anodator/utils.rb, line 58
def self.load_output_spec_from_csv_file(file_path,
                                   target = Anodator::OutputSpec::TARGET_ERROR,
                                   include_no_error = false)
  first = true
  header = nil
  spec = []
  CSV.open(file_path, "r") do |row|
    # skip header
    if first
      header = row
      first = false
      next
    end
    if row.first.nil? || row.first.split(//).length.zero?
      spec << row.last.to_sym
    else
      spec << row.first.to_s
    end
  end

  return Anodator::OutputSpec.new(spec, :target => target, :include_no_error => include_no_error)
end
load_rule_from_csv_file(file_path, validators) click to toggle source

load rule_set from csv file

File encoding: UTF-8 Columns:

- rule identification(string)
- rule description(string)
- target expression(column identification or column name)
- validator identification
- prerequisite validator identification(allow blank and multiple validator identification)
- error level(ERROR or WARNING)
- error message holder(string)

Return:

RuleSet instance
# File lib/anodator/utils.rb, line 189
def self.load_rule_from_csv_file(file_path, validators)
  first = true
  header = nil
  rule_set = RuleSet.new

  CSV.open(file_path, "r") do |row|
    # skip header
    if first
      header = row
      first = false
      next
    end

    description = row[1]
    target_expression = row[2].split(",")
    validator = validators[row[3]]
    if !row[4].nil? && row[4].include?(",")
      prerequisite = row[4].split(",").map do |validator_id|
        raise "Unknown validator identifier '#{validator_id}'" if validators[validator_id].nil?
        next validators[validator_id]
      end
    else
      prerequisite = validators[row[4]]
    end
    if validator.nil?
      raise "Unknown validator identifier '#{row[3]}'"
    end
    if !row[4].nil? && prerequisite.nil?
      raise "Unknown validator identifier '#{row[4]}'"
    end
    if Rule::ERROR_LEVEL_NAMES.values.include?(row[5])
      level = Rule::ERROR_LEVELS[Rule::ERROR_LEVEL_NAMES.index(row[5])]
    else
      raise "Unknown error type '#{row[5]}'"
    end
    message = Message.new(row[6])

    rule_set <<
      Rule.new(target_expression,
               message,
               validator,
               prerequisite,
               level,
               description)
  end

  return rule_set
end
load_validators_from_csv_file(file_path) click to toggle source

load validators from csv file

File encoding: UTF-8 Columns:

- validator identification(string)
- validation name(string)
- validator type(string)
- target expression(string)
- options...

Return:

Hash for validators(key is identification, value is validator)
# File lib/anodator/utils.rb, line 120
def self.load_validators_from_csv_file(file_path)
  first = true
  header = nil
  validators = { }

  CSV.open(file_path, "r") do |row|
    # skip header
    if first
      header = row
      first = false
      next
    end

    if validators.keys.include?(row[0])
      raise ArgumentError.new("Duplicated validator number '#{row[0]}'!")
    end

    options = options_reader(row, { :description => row[1] })

    validator = nil

    case row[2]
    when "presence"
      validator = Validator::PresenceValidator
    when "blank"
      validator = Validator::BlankValidator
    when "date"
      validator = Validator::DateValidator
    when "format"
      validator = Validator::FormatValidator
    when "inclusion"
      validator = Validator::InclusionValidator
      unless options[:in].nil?
        options[:in] = options[:in].split(",")
      end
    when "length"
      validator = Validator::LengthValidator
    when "numeric"
      validator = Validator::NumericValidator
    when "complex"
      options[:validators] = row[3].split(",").map do |validator_number|
        validators[validator_number]
      end
      validators[row[0]] = Validator::ComplexValidator.new(options)
    else
      raise ArgumentError.new("Unknown validator type '#{row[2]}'!")
    end

    if validator
      validators[row[0]] = validator.new(row[3], options)
    end
  end

  return validators
end
options_reader(row, options = { }, options_index_from = 4) click to toggle source

options reader for option values for validator specs

When value is ‘true’ or ‘false’, convert true or false instance. The others set to string.

Arguments:

- row: validator line string
- options: default options
- options_index_from: index for option column be started on row(default: 4)

Return:

Hash
# File lib/anodator/utils.rb, line 92
def self.options_reader(row, options = { }, options_index_from = 4)
  if row.size > options_index_from
    row[options_index_from..-1].each do |column|
      next if column.nil?
      key, value = column.split(":", 2)
      case value
      when "true", "false"
        options[key.to_sym] = eval(value)
      else
        options[key.to_sym] = value
      end
    end
  end

  return options
end