Class: Anodator::Validator::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/anodator/validator/base.rb

Overview

Validator::Base is basic class for validations

Constant Summary collapse

@@values =

value of target for validation

nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_expression, options = { }) ⇒ Base

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.



100
101
102
103
104
105
106
107
108
109
# 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

Instance Attribute Details

#optionsObject (readonly)

option values



25
26
27
# File 'lib/anodator/validator/base.rb', line 25

def options
  @options
end

#targetObject (readonly)

target specify value



22
23
24
# File 'lib/anodator/validator/base.rb', line 22

def target
  @target
end

Class Method Details

.default_options(options = nil) ⇒ Object

set and/or get default options



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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

.valid_option_keys(*option_keys) ⇒ Object

set and/or get valid option keys



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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

.valuesObject

Get the data to be checked



86
87
88
# File 'lib/anodator/validator/base.rb', line 86

def values
  return @@values
end

.values=(values) ⇒ Object

Set the data to be checked.

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



77
78
79
80
81
82
83
# 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

Instance Method Details

#allow_blank?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/anodator/validator/base.rb', line 155

def allow_blank?
  return @options[:allow_blank]
end

#argument_value_at(name_or_index) ⇒ Object



151
152
153
# File 'lib/anodator/validator/base.rb', line 151

def argument_value_at(name_or_index)
  return @@values[name_or_index].to_s
end

#descriptionObject



159
160
161
# File 'lib/anodator/validator/base.rb', line 159

def description
  return @options[:description]
end

#target_valueObject

target_value

always return String object use to_s method



147
148
149
# File 'lib/anodator/validator/base.rb', line 147

def target_value
  return @@values[target].to_s
end

#to_s(level = 4, step = 2) ⇒ Object



168
169
170
# File 'lib/anodator/validator/base.rb', line 168

def to_s(level = 4, step = 2)
  (" " * level) + "- #{self.class}(#{self.description})"
end

#valid?Boolean

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.

Returns:

  • (Boolean)


125
126
127
# File 'lib/anodator/validator/base.rb', line 125

def valid?
  return validate
end

#validateObject

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.

Raises:

  • (NoMethodError)


117
118
119
# File 'lib/anodator/validator/base.rb', line 117

def validate
  raise NoMethodError.new("must define method 'validate'")
end

#validate_configurationObject



172
173
174
175
176
# 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