class AWS::Record::Validator

Base class for all validators @private

Constants

ACCEPTED_OPTIONS

these should be defined in subclasses

Attributes

attribute_names[R]

@return [String] Returns the name of the attribute this validator

will check on the record during validation.
options[R]

@return [Hash] Returns the hash of options for this validator.

Public Class Methods

new(record_class, *attribute_names, &block) click to toggle source
# File lib/aws/record/validator.rb, line 24
def initialize record_class, *attribute_names, &block

  @options = attribute_names.last.is_a?(Hash) ? attribute_names.pop : {}

  @attribute_names = attribute_names

  reject_unknown_options

  ensure_type([Symbol, Proc], :if, :unless)
  ensure_is([:save, :create, :update], :on) 

  setup(record_class)

end

Public Instance Methods

validate(record) click to toggle source
# File lib/aws/record/validator.rb, line 46
def validate record
  if 
    passes_on_condition?(record) and
    passes_if_condition?(record) and
    passes_unless_condition?(record)
  then
    validate_attributes(record)
  end
end

Protected Instance Methods

add_accessors(klass, *accessors) click to toggle source

@private

# File lib/aws/record/validator.rb, line 72
def add_accessors klass, *accessors

  methods = klass.instance_methods.collect{|m| m.to_s }

  accessors.each do |attr|

    setter = "#{attr}="
    getter = attr.to_s

    unless methods.include?(getter)
      klass.send(:attr_reader, attr)
      klass.send(:public, getter)
    end

    unless methods.include?(setter)
      klass.send(:attr_writer, attr) 
      klass.send(:public, setter)
    end

  end

end
each_value(value) { |v| ... } click to toggle source

@private

# File lib/aws/record/validator.rb, line 63
def each_value value, &block
  case value
  when Array, Set then value.each{|v| yield(v) }
  else yield(value)
  end
end
ensure_at_least_one(*keys) click to toggle source

@private

# File lib/aws/record/validator.rb, line 220
def ensure_at_least_one *keys
  found = keys.select{|k| options.has_key?(k) }
  unless found.count >= 1
    opts = keys.collect{|k| ":#{k}" }.join(', ')
    msg = "must provide at least one of the following options: #{opts}"
    raise ArgumentError, msg
  end
end
ensure_exclusive(*key_groups) click to toggle source

@private

# File lib/aws/record/validator.rb, line 194
def ensure_exclusive *key_groups
  key_groups.each do |key_group|
    others = key_groups - [key_group]
    Array(key_group).each do |key|
      next unless options.has_key?(key)
      conflicts = others.flatten.select{|other| options.has_key?(other) }
      unless conflicts.empty?
        msg = ":#{key} may not be used with :#{conflicts.first}"
        raise ArgumentError, msg
      end
    end
  end
end
ensure_is(value_or_values, *keys) click to toggle source
# File lib/aws/record/validator.rb, line 181
def ensure_is value_or_values, *keys
  values = Array(value_or_values)
  keys.each do |key|
    next unless options.has_key?(key)
    unless values.include?(options[key])
      valid = values.map{|v| v.is_a?(Symbol) ? ":#{v}" : v.to_s }.join(', ')
      raise ArgumentError, "expected :#{key} to be one of #{valid}"
    end
  end
end
ensure_present(*keys) click to toggle source

@private

# File lib/aws/record/validator.rb, line 210
def ensure_present *keys
  keys.each do |k|
    unless options.has_key?(k)
      raise ArgumentError, "missing required option :#{k}"
    end
  end
end
ensure_type(type_or_types, *keys) click to toggle source

@private

# File lib/aws/record/validator.rb, line 161
def ensure_type type_or_types, *keys

  types = Array(type_or_types)

  keys.each do |key|

    next unless options.has_key?(key)
    next unless types.none?{|type| options[key].is_a?(type) }

    expected = types.map{|type| type.to_s }
    if expected.count == 1
      raise ArgumentError, "expected option :#{key} to be a #{expected}"
    else
      msg = "expected :#{key} to be one of #{expected.join(', ')}"
      raise ArgumentError, msg
    end

  end
end
read_attribute_for_validation(record, attribute_name) click to toggle source

@private

# File lib/aws/record/validator.rb, line 107
def read_attribute_for_validation(record, attribute_name)
  record.send(attribute_name)
end
reject_unknown_options() click to toggle source

@private

# File lib/aws/record/validator.rb, line 112
def reject_unknown_options
  invalid_keys = options.keys - self.class::ACCEPTED_OPTIONS
  if invalid_keys.length == 1
    raise ArgumentError, "unknown option :#{invalid_keys.first}"
  elsif invalid_keys.length > 1
    bad = invalid_keys.collect{|k| ":#{k}" }.join(', ')
    raise ArgumentError, "unknown options #{bad}"
  end
end
set_default(key, value) click to toggle source
# File lib/aws/record/validator.rb, line 230
def set_default key, value
  options[key] = value unless options.has_key?(key)
end
setup(klass) click to toggle source

@private

# File lib/aws/record/validator.rb, line 58
def setup klass
end
validate_attributes(record) click to toggle source

@private

# File lib/aws/record/validator.rb, line 97
def validate_attributes record
  attribute_names.each do |attribute_name|
    value = read_attribute_for_validation(record, attribute_name)
    next if value.nil? and options[:allow_nil]
    validate_attribute(record, attribute_name, value)
  end
end