Class: TorqueBox::Configuration::Validator

Inherits:
Object
  • Object
show all
Defined in:
configure/lib/torquebox/configuration/validator.rb

Instance Method Summary (collapse)

Constructor Details

- (Validator) initialize(ruleset, entry, options_to_validate)

Returns a new instance of Validator



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

def initialize(ruleset, entry, options_to_validate)
  @required = ruleset[:required] || []
  @allowed = @required + (ruleset[:optional] || [])
  @entry = entry
  @options_to_validate = options_to_validate
  validate
end

Instance Method Details

- (Object) keys_for(ary)



85
86
87
88
89
# File 'configure/lib/torquebox/configuration/validator.rb', line 85

def keys_for(ary)
  ary.collect do |value|
    (value.is_a?( Hash ) ? value.keys.first : value).to_s
  end
end

- (Object) message



38
39
40
41
42
43
44
45
46
47
48
49
# File 'configure/lib/torquebox/configuration/validator.rb', line 38

def message
  case messages.size
  when 0
    nil
  when 1
    messages.first
  else
    result = "There are multiple messages for this entry:"
    messages.each { |message| result << "\n  " << message }
    result
  end
end

- (Object) messages



52
53
54
# File 'configure/lib/torquebox/configuration/validator.rb', line 52

def messages
  @messages ||= []
end

- (Boolean) valid?

Returns:

  • (Boolean)


34
35
36
# File 'configure/lib/torquebox/configuration/validator.rb', line 34

def valid?
  !message
end

- (Object) validate



56
57
58
59
60
# File 'configure/lib/torquebox/configuration/validator.rb', line 56

def validate
  validate_required
  validate_allowed
  validate_values
end

- (Object) validate_allowed



68
69
70
71
72
# File 'configure/lib/torquebox/configuration/validator.rb', line 68

def validate_allowed
  @options_to_validate.keys.map(&:to_s).each do |option|
    messages << "Option :#{option} is not allowed on '#{@entry}'" unless keys_for( @allowed ).include?( option )
  end
end

- (Object) validate_required



62
63
64
65
66
# File 'configure/lib/torquebox/configuration/validator.rb', line 62

def validate_required
  keys_for( @required ).each do |required|
    messages << "Required option :#{required} is missing on '#{@entry}'" unless @options_to_validate.keys.map(&:to_s).include?( required )
  end
end

- (Object) validate_values



74
75
76
77
78
79
80
81
82
83
# File 'configure/lib/torquebox/configuration/validator.rb', line 74

def validate_values
  @allowed.each do |allowed|
    if allowed.is_a?( Hash )
      key = allowed.keys.first
      if @options_to_validate.has_key?( key ) && !allowed[key].include?( @options_to_validate[key] )
        messages << "#{@options_to_validate[key].inspect} is not a valid value for #{key.inspect} on '#{@entry}'. Valid values are #{allowed[key].inspect}"
      end
    end
  end
end