Class: Easy::Matchers::Validations::ValidateLengthOfMatcher

Inherits:
BaseValidationMatcher show all
Defined in:
lib/matchers/validations/length_of.rb

Instance Attribute Summary

Attributes inherited from BaseValidationMatcher

#attributes, #failure_message, #failure_message_when_negated, #options, #subject, #type

Instance Method Summary collapse

Methods inherited from BaseValidationMatcher

#class_name

Constructor Details

#initialize(attributes) ⇒ ValidateLengthOfMatcher

Returns a new instance of ValidateLengthOfMatcher.



9
10
11
# File 'lib/matchers/validations/length_of.rb', line 9

def initialize(attributes)
  super(attributes, :length)
end

Instance Method Details

#allow_blank(value = true) ⇒ Object



50
51
52
53
# File 'lib/matchers/validations/length_of.rb', line 50

def allow_blank(value = true)
  options[:allow_blank] = value
  self
end

#allow_nil(value = true) ⇒ Object



45
46
47
48
# File 'lib/matchers/validations/length_of.rb', line 45

def allow_nil(value = true)
  options[:allow_nil] = value
  self
end

#descriptionObject

def is_equal_to

@is = value
self

end



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/matchers/validations/length_of.rb', line 75

def description
  description = "ensure #{ attributes.join(', ') } has a length "
#   options_desc = []
#   options_desc << "with minimum of #{ @minimum }" if @minimum
#   options_desc << "with maximum of #{ @maximum }" if @maximum
#   # options_desc << "within the range of #{ @within }" if @within
#   # options_desc << "as exactly #{ @is }" if @is
#   options_desc << "with message '#{ @expected_message }'" if @expected_message
#   super << " #{ options_desc.to_sentence }"
  description
end

#if(symbol_name) ⇒ Object



30
31
32
33
# File 'lib/matchers/validations/length_of.rb', line 30

def if(symbol_name)
  options[:if] = symbol_name
  self
end

#is_at_least(value) ⇒ Object



65
66
67
68
# File 'lib/matchers/validations/length_of.rb', line 65

def is_at_least(value)
  options[:minimum] = value
  self
end

#is_at_most(value) ⇒ Object



60
61
62
63
# File 'lib/matchers/validations/length_of.rb', line 60

def is_at_most(value)
  options[:maximum] = value
  self
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/matchers/validations/length_of.rb', line 13

def matches?(subject)
  super(subject)

  matched_validator = class_name.validators.detect do |validator|
    validator.kind == type &&
      validator.attributes.sort == attributes.sort &&
      validator.options.sort == options.sort
  end

  true if matched_validator
end

#on(context) ⇒ Object



40
41
42
43
# File 'lib/matchers/validations/length_of.rb', line 40

def on(context)
  options[:on] = context
  self
end

#strict(value) ⇒ Object



55
56
57
58
# File 'lib/matchers/validations/length_of.rb', line 55

def strict(value)
  options[:strict] = value
  self
end

#unless(symbol_name) ⇒ Object



35
36
37
38
# File 'lib/matchers/validations/length_of.rb', line 35

def unless(symbol_name)
  options[:unless] = symbol_name
  self
end

#with_message(message) ⇒ Object



25
26
27
28
# File 'lib/matchers/validations/length_of.rb', line 25

def with_message(message)
  options[:message] = message
  self
end