Class: Paperclip::Shoulda::Matchers::ValidateAttachmentSizeMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/paperclip/matchers/validate_attachment_size_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(attachment_name) ⇒ ValidateAttachmentSizeMatcher

Returns a new instance of ValidateAttachmentSizeMatcher



19
20
21
22
# File 'lib/paperclip/matchers/validate_attachment_size_matcher.rb', line 19

def initialize attachment_name
  @attachment_name = attachment_name
  @low, @high = 0, (1.0/0)
end

Instance Method Details

#descriptionObject



53
54
55
# File 'lib/paperclip/matchers/validate_attachment_size_matcher.rb', line 53

def description
  "validate the size of attachment #{@attachment_name}"
end

#failure_messageObject



45
46
47
# File 'lib/paperclip/matchers/validate_attachment_size_matcher.rb', line 45

def failure_message
  "Attachment #{@attachment_name} must be between #{@low} and #{@high} bytes"
end

#greater_than(size) ⇒ Object



29
30
31
32
# File 'lib/paperclip/matchers/validate_attachment_size_matcher.rb', line 29

def greater_than size
  @low = size
  self
end

#in(range) ⇒ Object



34
35
36
37
# File 'lib/paperclip/matchers/validate_attachment_size_matcher.rb', line 34

def in range
  @low, @high = range.first, range.last
  self
end

#less_than(size) ⇒ Object



24
25
26
27
# File 'lib/paperclip/matchers/validate_attachment_size_matcher.rb', line 24

def less_than size
  @high = size
  self
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
# File 'lib/paperclip/matchers/validate_attachment_size_matcher.rb', line 39

def matches? subject
  @subject = subject
  @subject = @subject.class unless Class === @subject
  lower_than_low? && higher_than_low? && lower_than_high? && higher_than_high?
end

#negative_failure_messageObject



49
50
51
# File 'lib/paperclip/matchers/validate_attachment_size_matcher.rb', line 49

def negative_failure_message
  "Attachment #{@attachment_name} cannot be between #{@low} and #{@high} bytes"
end