Class: ActiveStorageValidations::Matchers::DimensionValidatorMatcher

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

Instance Method Summary collapse

Constructor Details

#initialize(attribute_name) ⇒ DimensionValidatorMatcher

Returns a new instance of DimensionValidatorMatcher.



10
11
12
13
# File 'lib/active_storage_validations/matchers/dimension_validator_matcher.rb', line 10

def initialize(attribute_name)
  @attribute_name = attribute_name
  @width_min = @width_max = @height_min = @height_max = nil
end

Instance Method Details

#descriptionObject



15
16
17
# File 'lib/active_storage_validations/matchers/dimension_validator_matcher.rb', line 15

def description
  "validate image dimensions of #{@attribute_name}"
end

#failure_messageObject



66
67
68
69
70
71
72
# File 'lib/active_storage_validations/matchers/dimension_validator_matcher.rb', line 66

def failure_message
  <<~MESSAGE
    is expected to validate dimensions of #{@attribute_name}
      width between #{@width_min} and #{@width_max}
      height between #{@height_min} and #{@height_max}
  MESSAGE
end

#height(height) ⇒ Object



54
55
56
57
# File 'lib/active_storage_validations/matchers/dimension_validator_matcher.rb', line 54

def height(height)
  @height_min = @height_max = height
  self
end

#height_between(range) ⇒ Object



49
50
51
52
# File 'lib/active_storage_validations/matchers/dimension_validator_matcher.rb', line 49

def height_between(range)
  @height_min, @height_max = range.first, range.last
  self
end

#height_max(height) ⇒ Object



39
40
41
42
# File 'lib/active_storage_validations/matchers/dimension_validator_matcher.rb', line 39

def height_max(height)
  @height_max = height
  self
end

#height_min(height) ⇒ Object



34
35
36
37
# File 'lib/active_storage_validations/matchers/dimension_validator_matcher.rb', line 34

def height_min(height)
  @height_min = height
  self
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
# File 'lib/active_storage_validations/matchers/dimension_validator_matcher.rb', line 59

def matches?(subject)
  @subject = subject.is_a?(Class) ? subject.new : subject
  responds_to_methods &&
    width_smaller_than_min? && width_larger_than_min? && width_smaller_than_max? && width_larger_than_max? && width_equals? &&
    height_smaller_than_min? && height_larger_than_min? && height_smaller_than_max? && height_larger_than_max? && height_equals?
end

#width(width) ⇒ Object



29
30
31
32
# File 'lib/active_storage_validations/matchers/dimension_validator_matcher.rb', line 29

def width(width)
  @width_min = @width_max = width
  self
end

#width_between(range) ⇒ Object



44
45
46
47
# File 'lib/active_storage_validations/matchers/dimension_validator_matcher.rb', line 44

def width_between(range)
  @width_min, @width_max = range.first, range.last
  self
end

#width_max(width) ⇒ Object



24
25
26
27
# File 'lib/active_storage_validations/matchers/dimension_validator_matcher.rb', line 24

def width_max(width)
  @width_max = width
  self
end

#width_min(width) ⇒ Object



19
20
21
22
# File 'lib/active_storage_validations/matchers/dimension_validator_matcher.rb', line 19

def width_min(width)
  @width_min = width
  self
end