Class: MiniDefender::Rules::Size

Inherits:
MiniDefender::Rule show all
Defined in:
lib/mini_defender/rules/size.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MiniDefender::Rule

#active?, available?, #bails?, #coerce, #default_value, #defaults?, #error_message, #excluded?, #force_coerce?, #implicit?, #priority, #stops?, #with_message

Constructor Details

#initialize(size) ⇒ Size

Returns a new instance of Size.

Raises:

  • (ArgumentError)


6
7
8
9
10
# File 'lib/mini_defender/rules/size.rb', line 6

def initialize(size)
  raise ArgumentError, 'Size must be an integer.' unless size.is_a?(Integer)

  @size = size
end

Class Method Details

.make(args) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
# File 'lib/mini_defender/rules/size.rb', line 16

def self.make(args)
  raise ArgumentError, 'Expected exactly one argument.' unless args.length == 1

  self.new(args[0].to_i)
end

.signatureObject



12
13
14
# File 'lib/mini_defender/rules/size.rb', line 12

def self.signature
  'size'
end

Instance Method Details

#message(attribute, value, validator) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/mini_defender/rules/size.rb', line 35

def message(attribute, value, validator)
  case value
  when ActionDispatch::Http::UploadedFile
    "The file size must be equal to #{@size} bytes."
  when Numeric
    "The value must be equal to #{@size}."
  else
    "The value length must be equal to #{@size}."
  end
end

#passes?(attribute, value, validator) ⇒ Boolean

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mini_defender/rules/size.rb', line 22

def passes?(attribute, value, validator)
  case value
  when String, Array, Hash
    value.length == @size
  when ActionDispatch::Http::UploadedFile
    value.size == @size
  when Numeric
    value == @size
  else
    false
  end
end