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
16
# 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
  @disallowed_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

#disallowed_valuesObject

Returns the value of attribute disallowed_values.



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

def disallowed_values
  @disallowed_values
end

Instance Method Details

#descriptionObject



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

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

#failure_message_for_shouldObject



52
53
54
# File 'lib/simple_params/validation_matchers/required_parameter_matcher.rb', line 52

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

#failure_message_for_should_notObject



56
57
58
# File 'lib/simple_params/validation_matchers/required_parameter_matcher.rb', line 56

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)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/simple_params/validation_matchers/required_parameter_matcher.rb', line 33

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

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

#with_allowed_values(*values) ⇒ Object



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

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

#with_default(value) ⇒ Object



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

def with_default(value)
  @default_value = value
  self
end

#with_disallowed_values(*values) ⇒ Object



28
29
30
31
# File 'lib/simple_params/validation_matchers/required_parameter_matcher.rb', line 28

def with_disallowed_values(*values) 
  @disallowed_values = values
  self
end