Class: Alias::Validator

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

Overview

Creates validations for use with Alias::Creator.valid.

Defined Under Namespace

Classes: InvalidValidatorError, MissingConditionError

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Validator

Options are described in Alias::Creator.valid.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
# File 'lib/alias/validator.rb', line 9

def initialize(options={})
  raise ArgumentError unless options[:key] && options[:creator]
  @condition = options[:if] || options[:unless] || raise(MissingConditionError)
  inherits(Validator.validators[@condition]) if Validator.validators[@condition]
  raise InvalidValidatorError unless @condition.is_a?(Proc)
  @optional = options[:optional] || false
  @validation_proc = options[:unless] ? lambda {|e| ! @condition.clone.call(e) } : @condition
  @options = options
  @message = options[:message] if options[:message]
  @message = default_message unless @message.is_a?(Proc)
end

Class Attribute Details

.validatorsObject (readonly)

Hash of registered validators.



55
56
57
# File 'lib/alias/validator.rb', line 55

def validators
  @validators
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



7
8
9
# File 'lib/alias/validator.rb', line 7

def message
  @message
end

#validation_procObject (readonly)

Returns the value of attribute validation_proc.



7
8
9
# File 'lib/alias/validator.rb', line 7

def validation_proc
  @validation_proc
end

Class Method Details

.any_const_get(name) ⇒ Object

:stopdoc:



79
80
81
# File 'lib/alias/validator.rb', line 79

def any_const_get(name)
  Util.any_const_get(name)
end

.class_method?(klass, method) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/alias/validator.rb', line 87

def class_method?(klass, method)
  (klass = any_const_get(klass)) && klass.respond_to?(method, true)
end

.default_validatorsObject

Default validators are :constant, :class, :instance_method and :class_method.



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/alias/validator.rb', line 66

def default_validators
  [
    {:key=>:constant, :if=>lambda {|e| any_const_get(e) }, :message=>lambda {|e| "Constant '#{e}' not created since it doesn't exist"}},
    {:key=>:class, :if=>lambda {|e| ((klass = any_const_get(e)) && klass.is_a?(Module)) },
      :message=>lambda {|e| "Alias for class '#{e}' not created since the class doesn't exist"}},
    {:key=>:instance_method, :if=> lambda {|e| instance_method?(*e) },
      :message=>lambda {|e| "Alias for instance method '#{e[0]}.#{e[1]}' not created since it doesn't exist" }},
    {:key=>:class_method, :if=>lambda {|e| class_method?(*e) },
      :message=>lambda {|e| "Alias for class method '#{e[0]}.#{e[1]}' not created since it doesn't exist" }}
  ]
end

.instance_method?(klass, method) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/alias/validator.rb', line 83

def instance_method?(klass, method)
  (klass = any_const_get(klass)) && (klass.method_defined?(method) || klass.private_method_defined?(method))
end

.register_validators(validators) ⇒ Object

Registers validators which creators can reference as symbols in Alias::Creator.valid.



58
59
60
61
62
63
# File 'lib/alias/validator.rb', line 58

def register_validators(validators)
  @validators ||= {}
  validators.each do |e|
    @validators[e[:key]] ||= Validator.new(e.merge(:creator=>self))
  end
end

Instance Method Details

#create_message(arg) ⇒ Object



47
48
49
50
# File 'lib/alias/validator.rb', line 47

def create_message(arg)
  result = @message.call(arg)
  @options[:unless] ? result.gsub("doesn't exist", 'already exists') : result
end

#create_proc_arg(alias_hash, current_attribute) ⇒ Object

:nodoc:



43
44
45
# File 'lib/alias/validator.rb', line 43

def create_proc_arg(alias_hash, current_attribute) #:nodoc:
  @options[:with] ? @options[:with].map {|f| alias_hash[f] } : (alias_hash[current_attribute] || alias_hash)
end

#default_messageObject



39
40
41
# File 'lib/alias/validator.rb', line 39

def default_message
  lambda {|e| "Validation failed for #{@options[:creator]}'s #{@options[:key]} since it doesn't exist"}
end

#inherits(parent_validator) ⇒ Object

:stopdoc:



34
35
36
37
# File 'lib/alias/validator.rb', line 34

def inherits(parent_validator)
  @condition = parent_validator.validation_proc.clone
  @message = parent_validator.message.clone
end

#validate(current_creator, alias_hash, current_attribute) ⇒ Object

Validates a given alias hash with the validator proc defined by :if or :unless in Alias::Creator.valid. Default arguments that these procs receive works as follows: If the validation key is the same name as any of the keys in the alias hash, then only the value of that that alias key is passed to the procs. If not, then the whole alias hash is passed.



25
26
27
28
29
30
31
# File 'lib/alias/validator.rb', line 25

def validate(current_creator, alias_hash, current_attribute)
  return true if @optional && current_creator.force
  arg = create_proc_arg(alias_hash, current_attribute)
  result = !!@validation_proc.call(arg)
  puts create_message(arg) if result != true && current_creator.verbose
  result
end