class ActiveStorageValidations::Matchers::SizeValidatorMatcher

Public Class Methods

new(attribute_name) click to toggle source
# File lib/active_storage_validations/matchers/size_validator_matcher.rb, line 12
def initialize(attribute_name)
  @attribute_name = attribute_name
  @low = @high = nil
end

Public Instance Methods

between(range) click to toggle source
# File lib/active_storage_validations/matchers/size_validator_matcher.rb, line 41
def between(range)
  @low, @high = range.first, range.last
  self
end
description() click to toggle source
# File lib/active_storage_validations/matchers/size_validator_matcher.rb, line 17
def description
  "validate file size of #{@attribute_name}"
end
failure_message() click to toggle source
# File lib/active_storage_validations/matchers/size_validator_matcher.rb, line 51
def failure_message
  "is expected to validate file size of #{@attribute_name} to be between #{@low} and #{@high} bytes"
end
failure_message_when_negated() click to toggle source
# File lib/active_storage_validations/matchers/size_validator_matcher.rb, line 55
def failure_message_when_negated
  "is expected to not validate file size of #{@attribute_name} to be between #{@low} and #{@high} bytes"
end
greater_than(size) click to toggle source
# File lib/active_storage_validations/matchers/size_validator_matcher.rb, line 31
def greater_than(size)
  @low = size + 1.byte
  self
end
greater_than_or_equal_to(size) click to toggle source
# File lib/active_storage_validations/matchers/size_validator_matcher.rb, line 36
def greater_than_or_equal_to(size)
  @low = size
  self
end
less_than(size) click to toggle source
# File lib/active_storage_validations/matchers/size_validator_matcher.rb, line 21
def less_than(size)
  @high = size - 1.byte
  self
end
less_than_or_equal_to(size) click to toggle source
# File lib/active_storage_validations/matchers/size_validator_matcher.rb, line 26
def less_than_or_equal_to(size)
  @high = size
  self
end
matches?(subject) click to toggle source
# File lib/active_storage_validations/matchers/size_validator_matcher.rb, line 46
def matches?(subject)
  @subject = subject.is_a?(Class) ? subject.new : subject
  responds_to_methods && lower_than_low? && higher_than_low? && lower_than_high? && higher_than_high?
end

Protected Instance Methods

higher_than_high?() click to toggle source
# File lib/active_storage_validations/matchers/size_validator_matcher.rb, line 79
def higher_than_high?
  @high.nil? || @high == Float::INFINITY || !passes_validation_with_size(@high + 1)
end
higher_than_low?() click to toggle source
# File lib/active_storage_validations/matchers/size_validator_matcher.rb, line 71
def higher_than_low?
  @low.nil? || passes_validation_with_size(@low + 1)
end
lower_than_high?() click to toggle source
# File lib/active_storage_validations/matchers/size_validator_matcher.rb, line 75
def lower_than_high?
  @high.nil? || @high == Float::INFINITY || passes_validation_with_size(@high - 1)
end
lower_than_low?() click to toggle source
# File lib/active_storage_validations/matchers/size_validator_matcher.rb, line 67
def lower_than_low?
  @low.nil? || !passes_validation_with_size(@low - 1)
end
passes_validation_with_size(new_size) click to toggle source
# File lib/active_storage_validations/matchers/size_validator_matcher.rb, line 83
def passes_validation_with_size(new_size)
  io = Tempfile.new('Hello world!')
  Matchers.stub_method(io, :size, new_size) do
    @subject.public_send(@attribute_name).attach(io: io, filename: 'test.png', content_type: 'image/pg')
    @subject.validate
    @subject.errors.details[@attribute_name].all? { |error| error[:error] != :file_size_out_of_range }
  end
end
responds_to_methods() click to toggle source
# File lib/active_storage_validations/matchers/size_validator_matcher.rb, line 61
def responds_to_methods
  @subject.respond_to?(@attribute_name) &&
    @subject.public_send(@attribute_name).respond_to?(:attach) &&
    @subject.public_send(@attribute_name).respond_to?(:detach)
end