Class: ActiveStorageValidations::Matchers::SizeValidatorMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/active_storage_validations/matchers/size_validator_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(attribute_name) ⇒ SizeValidatorMatcher

Returns a new instance of SizeValidatorMatcher.



12
13
14
15
16
# File 'lib/active_storage_validations/matchers/size_validator_matcher.rb', line 12

def initialize(attribute_name)
  @attribute_name = attribute_name
  @min = @max = nil
  @custom_message = nil
end

Instance Method Details

#between(range) ⇒ Object



42
43
44
45
# File 'lib/active_storage_validations/matchers/size_validator_matcher.rb', line 42

def between(range)
  @min, @max = range.first, range.last
  self
end

#descriptionObject



18
19
20
# File 'lib/active_storage_validations/matchers/size_validator_matcher.rb', line 18

def description
  "validate file size of #{@attribute_name}"
end

#failure_messageObject



57
58
59
# File 'lib/active_storage_validations/matchers/size_validator_matcher.rb', line 57

def failure_message
  "is expected to validate file size of #{@attribute_name} to be between #{@min} and #{@max} bytes"
end

#failure_message_when_negatedObject



61
62
63
# File 'lib/active_storage_validations/matchers/size_validator_matcher.rb', line 61

def failure_message_when_negated
  "is expected to not validate file size of #{@attribute_name} to be between #{@min} and #{@max} bytes"
end

#greater_than(size) ⇒ Object



32
33
34
35
# File 'lib/active_storage_validations/matchers/size_validator_matcher.rb', line 32

def greater_than(size)
  @min = size + 1.byte
  self
end

#greater_than_or_equal_to(size) ⇒ Object



37
38
39
40
# File 'lib/active_storage_validations/matchers/size_validator_matcher.rb', line 37

def greater_than_or_equal_to(size)
  @min = size
  self
end

#less_than(size) ⇒ Object



22
23
24
25
# File 'lib/active_storage_validations/matchers/size_validator_matcher.rb', line 22

def less_than(size)
  @max = size - 1.byte
  self
end

#less_than_or_equal_to(size) ⇒ Object



27
28
29
30
# File 'lib/active_storage_validations/matchers/size_validator_matcher.rb', line 27

def less_than_or_equal_to(size)
  @max = size
  self
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/active_storage_validations/matchers/size_validator_matcher.rb', line 52

def matches?(subject)
  @subject = subject.is_a?(Class) ? subject.new : subject
  responds_to_methods && not_lower_than_min? && higher_than_min? && lower_than_max? && not_higher_than_max?
end

#with_message(message) ⇒ Object



47
48
49
50
# File 'lib/active_storage_validations/matchers/size_validator_matcher.rb', line 47

def with_message(message)
  @custom_message = message
  self
end