class Anodator::Validator::Base

Validator::Base is basic class for validations

Attributes

options[R]

option values

target[R]

target specify value

Public Class Methods

default_options(options = nil) click to toggle source

set and/or get default options

# File lib/anodator/validator/base.rb, line 52
def default_options(options = nil)
  # initialize from superclass
  if @default_options.nil?
    @default_options = self.superclass.default_options
  end

  unless options.nil?
    unless options.is_a? Hash
      raise ArgumentError.new("default_options must call with Hash")
    end
    options.each do |option, default_value|
      if @valid_option_keys.include?(option)
        @default_options[option] = default_value
      else
        raise ArgumentError.new("Unknown option '#{option}'")
      end
    end
  end

  return @default_options.dup
end
new(target_expression, options = { }) click to toggle source

initializer

Specify options as an additional parameter to hold the verification and other options for specifying the target_expression be verified. The default options are optional and will be an empty hash. Key to use additional parameters are stored in class variable valid_option_keys. If necessary add additional parameters for the new validator is defined in the inherited class to add.

# File lib/anodator/validator/base.rb, line 100
def initialize(target_expression, options = { })
  if target_expression.to_s.length.zero?
    raise ArgumentError.new("target cannot be nil or blank")
  else
    @target = target_expression.to_s
  end
  @options = { }
  merge_options!(self.class.default_options)
  merge_options!(options)
end
valid_option_keys(*option_keys) click to toggle source

set and/or get valid option keys

# File lib/anodator/validator/base.rb, line 32
def valid_option_keys(*option_keys)
  # initialize from superclass
  if @valid_option_keys.nil?
    @valid_option_keys = self.superclass.valid_option_keys
  end

  unless option_keys.size.zero?
    option_keys.each do |key|
      if @valid_option_keys.include?(key)
        raise ArgumentError.new("Validator already has option for '#{key}'")
      else
        @valid_option_keys << key
      end
    end
  end

  return @valid_option_keys.dup
end
values() click to toggle source

Get the data to be checked

# File lib/anodator/validator/base.rb, line 86
def values
  return @@values
end
values=(values) click to toggle source

Set the data to be checked.

Data to be checked can be set to any object that responds to [].

# File lib/anodator/validator/base.rb, line 77
def values=(values)
  if values.respond_to?(:[])
    @@values = values
  else
    raise ArgumentError.new("values must be respond to [] method for validations.")
  end
end

Public Instance Methods

allow_blank?() click to toggle source
# File lib/anodator/validator/base.rb, line 155
def allow_blank?
  return @options[:allow_blank]
end
argument_value_at(name_or_index) click to toggle source
# File lib/anodator/validator/base.rb, line 151
def argument_value_at(name_or_index)
  return @@values[name_or_index].to_s
end
description() click to toggle source
# File lib/anodator/validator/base.rb, line 159
def description
  return @options[:description]
end
target_value() click to toggle source

target_value

always return String object use to_s method

# File lib/anodator/validator/base.rb, line 147
def target_value
  return @@values[target].to_s
end
to_s(level = 4, step = 2) click to toggle source
# File lib/anodator/validator/base.rb, line 168
def to_s(level = 4, step = 2)
  (" " * level) + "- #{self.class}(#{self.description})"
end
valid?() click to toggle source

Call the validate method to return a boolean value accordingly

If any exception occurs in the validate method displays the contents to standard error. Then raise same error.

# File lib/anodator/validator/base.rb, line 125
def valid?
  return validate
end
validate() click to toggle source

validation method

validate method must be defined in a class that inherits. In the validate method implements the verification method. Can be implemented to return a boolean value to the final, valid? The method used is called.

# File lib/anodator/validator/base.rb, line 117
def validate
  raise NoMethodError.new("must define method 'validate'")
end
validate_configuration() click to toggle source
# File lib/anodator/validator/base.rb, line 172
def validate_configuration
  @@values.spec_item_by_expression(@target)
rescue UnknownTargetExpressionError => e
  raise InvalidConfiguration.new(e.to_s)
end

Private Instance Methods

merge_options!(options) click to toggle source

merge options

If the key parameter is illegal to throw an ArgumentError.

# File lib/anodator/validator/base.rb, line 132
def merge_options!(options)
  options.each do |key, value|
    if self.class.valid_option_keys.include?(key)
      @options[key] = value
    else
      raise ArgumentError.new("Unknown option key '#{key}'.")
    end
  end
end
proxy_value(target) click to toggle source
# File lib/anodator/validator/base.rb, line 163
def proxy_value(target)
  ValueProxy.new(target, self)
end