class SimpleParams::ValidationMatchers::OptionalParameterMatcher

Attributes

allowed_values[RW]
attribute[RW]
default_value[RW]
disallowed_values[RW]

Public Class Methods

new(attribute) click to toggle source
Calls superclass method
# File lib/simple_params/validation_matchers/optional_parameter_matcher.rb, line 10
def initialize(attribute)
  super(attribute)
  @default_value = nil
  @attribute = attribute
  @allowed_values = nil
  @disallowed_values = nil
end

Public Instance Methods

description() click to toggle source
# File lib/simple_params/validation_matchers/optional_parameter_matcher.rb, line 48
def description
  "allow #{@attribute} to be nil"
end
failure_message_for_should() click to toggle source
# File lib/simple_params/validation_matchers/optional_parameter_matcher.rb, line 52
def failure_message_for_should
  "Expected #{@default_value} either to be nil or one of #{@allowed_values}"
end
failure_message_for_should_not() click to toggle source
# File lib/simple_params/validation_matchers/optional_parameter_matcher.rb, line 56
def failure_message_for_should_not
  "Expected #{@default_value} not to be nil or to be one of #{@allowed_values}"
end
matches?(subject) click to toggle source
Calls superclass method
# File lib/simple_params/validation_matchers/optional_parameter_matcher.rb, line 33
def matches?(subject)
  super(subject)
  
  if @default_value
    matches_default_value?
  elsif @allowed_values
    allows_value_of(nil) && matches_allowed_values?
  elsif @disallowed_values
    allows_value_of(nil) && matches_disallowed_values?
  else
    allows_value_of(nil)
  end
  
end
with_allowed_values(*values) click to toggle source
# File lib/simple_params/validation_matchers/optional_parameter_matcher.rb, line 23
def with_allowed_values(*values)
  @allowed_values = values
  self
end
with_default(value) click to toggle source
# File lib/simple_params/validation_matchers/optional_parameter_matcher.rb, line 18
def with_default(value)
  @default_value = value
  self
end
with_disallowed_values(*values) click to toggle source
# File lib/simple_params/validation_matchers/optional_parameter_matcher.rb, line 28
def with_disallowed_values(*values)
  @disallowed_values = values
  self
end

Private Instance Methods

matches_allowed_values?() click to toggle source
# File lib/simple_params/validation_matchers/optional_parameter_matcher.rb, line 66
def matches_allowed_values?
  allowed_values.all? do |value|
    allows_value_of(value)
  end
end
matches_default_value?() click to toggle source
# File lib/simple_params/validation_matchers/optional_parameter_matcher.rb, line 62
def matches_default_value?
  @subject.send(@attribute) == @default_value 
end
matches_disallowed_values?() click to toggle source
# File lib/simple_params/validation_matchers/optional_parameter_matcher.rb, line 72
def matches_disallowed_values?
  disallowed_values.all? do |value|
    disallows_value_of(value)
  end
end