Class: ActiveStorageValidations::Matchers::SizeValidatorMatcher

Inherits:
Object
  • Object
show all
Includes:
ActiveStorageable, AllowBlankable, Contextable, Messageable, Rspecable, Validatable
Defined in:
lib/active_storage_validations/matchers/size_validator_matcher.rb

Instance Method Summary collapse

Methods included from Rspecable

#failure_message_when_negated, #initialize_rspecable

Methods included from Messageable

#initialize_messageable, #with_message

Methods included from Contextable

#initialize_contextable, #on

Methods included from AllowBlankable

#allow_blank, #initialize_allow_blankable

Constructor Details

#initialize(attribute_name) ⇒ SizeValidatorMatcher

Returns a new instance of SizeValidatorMatcher.



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

def initialize(attribute_name)
  initialize_allow_blankable
  initialize_contextable
  initialize_messageable
  initialize_rspecable
  @attribute_name = attribute_name
  @min = @max = nil
end

Instance Method Details

#between(range) ⇒ Object



66
67
68
69
# File 'lib/active_storage_validations/matchers/size_validator_matcher.rb', line 66

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

#descriptionObject



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

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

#failure_messageObject



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

def failure_message
  message = ["is expected to validate file size of :#{@attribute_name}"]
  build_failure_message(message)
  message.join("\n")
end

#greater_than(size) ⇒ Object



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

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

#greater_than_or_equal_to(size) ⇒ Object



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

def greater_than_or_equal_to(size)
  @min = size
  self
end

#less_than(size) ⇒ Object



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

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

#less_than_or_equal_to(size) ⇒ Object



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

def less_than_or_equal_to(size)
  @max = size
  self
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/active_storage_validations/matchers/size_validator_matcher.rb', line 71

def matches?(subject)
  @subject = subject.is_a?(Class) ? subject.new : subject

  is_a_valid_active_storage_attribute? &&
    is_context_valid? &&
    is_allowing_blank? &&
    is_custom_message_valid? &&
    not_lower_than_min? &&
    higher_than_min? &&
    lower_than_max? &&
    not_higher_than_max?
end