Module: FailFast::CheckValue

Defined in:
lib/fail_fast/extensions/check_value.rb

Instance Method Summary collapse

Instance Method Details

#has_value_for(key, *params) ⇒ Object

Usage

has_value_for 'str_key'
has_value_for :sym_key, /localhost/

returns

true if succesful, false otherwise


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fail_fast/extensions/check_value.rb', line 25

def has_value_for(key, *params)
  p = key_value_regexp_options(key, params)
  key, options = p.key, p.options

  nof_errors = errors.size
  message = options[:message]
  if blank?(p.value)
    add_error ErrorDetails.new(key, :missing_value, nil, message)

  elsif p.regexp
    add_error ErrorDetails.new(key, :value_does_not_match, p.value, message) unless p.value =~ p.regexp

  elsif options.is_a?(Hash) && options[:in].is_a?(Range)
    add_error ErrorDetails.new(key, :value_not_in_range,   p.value, message) unless options[:in].include?(p.value)

  elsif options.is_a?(Hash) && options[:in].is_a?(Array)
    add_error ErrorDetails.new(key, :value_not_in_array,   p.value, message) unless options[:in].include?(p.value)
  end
  no_new_error = nof_errors == errors.size
end

#has_values_for(*keys) ⇒ Object

Usage

has_values_for :sym_key, 'str_key'


15
16
17
# File 'lib/fail_fast/extensions/check_value.rb', line 15

def has_values_for(*keys)
  keys.each{|key| has_value_for(key)}
end

#value_of(key) ⇒ Object

Usage

nda_file = value_of(:nda_file)


7
8
9
# File 'lib/fail_fast/extensions/check_value.rb', line 7

def value_of(key)
  key_value_regexp_options(key, []).value
end