Class: SimpleParams::ValidationMatchers::RequiredParameterMatcher

Inherits:
ValidationMatcher
  • Object
show all
Defined in:
lib/simple_params/validation_matchers/required_parameter_matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ RequiredParameterMatcher

Returns a new instance of RequiredParameterMatcher.



10
11
12
13
14
15
# File 'lib/simple_params/validation_matchers/required_parameter_matcher.rb', line 10

def initialize(attribute)
  super(attribute)
  @default_value = nil
  @attribute = attribute
  @allowed_values = nil
end

Instance Attribute Details

#allowed_valuesObject

Returns the value of attribute allowed_values.



8
9
10
# File 'lib/simple_params/validation_matchers/required_parameter_matcher.rb', line 8

def allowed_values
  @allowed_values
end

#attributeObject

Returns the value of attribute attribute.



8
9
10
# File 'lib/simple_params/validation_matchers/required_parameter_matcher.rb', line 8

def attribute
  @attribute
end

#default_valueObject

Returns the value of attribute default_value.



8
9
10
# File 'lib/simple_params/validation_matchers/required_parameter_matcher.rb', line 8

def default_value
  @default_value
end

Instance Method Details

#descriptionObject



40
41
42
# File 'lib/simple_params/validation_matchers/required_parameter_matcher.rb', line 40

def description
  "require #{@attribute} to be set"
end

#failure_message_for_shouldObject



44
45
46
# File 'lib/simple_params/validation_matchers/required_parameter_matcher.rb', line 44

def failure_message_for_should
  "Expected #{@default_value} to be set and to be one of #{@allowed_values}"
end

#failure_message_for_should_notObject



48
49
50
# File 'lib/simple_params/validation_matchers/required_parameter_matcher.rb', line 48

def failure_message_for_should_not
  "Expected #{@default_value} not to be set and not to be one of #{@allowed_values}"
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/simple_params/validation_matchers/required_parameter_matcher.rb', line 27

def matches?(subject)
  super(subject)
  @expected_message ||= :blank

  if @default_value
    matches_default_value?
  elsif @allowed_values
    disallows_value_of(nil) && matches_allowed_values?
  else
    disallows_value_of(nil, @expected_message)
  end
end

#with_allowed_values(*values) ⇒ Object



22
23
24
25
# File 'lib/simple_params/validation_matchers/required_parameter_matcher.rb', line 22

def with_allowed_values(*values)
  @allowed_values = values
  self
end

#with_default(value) ⇒ Object



17
18
19
20
# File 'lib/simple_params/validation_matchers/required_parameter_matcher.rb', line 17

def with_default(value)
  @default_value = value
  self
end