Code review: add source

This commit is contained in:
Guillaume Lazzara 2016-02-19 14:10:35 +01:00
parent 2c499d5c82
commit c4cfd9dd0a

View file

@ -1,6 +1,7 @@
# Source: https://github.com/gitlabhq/gitlabhq/blob/master/lib/file_size_validator.rb
class FileSizeValidator < ActiveModel::EachValidator class FileSizeValidator < ActiveModel::EachValidator
MESSAGES = { :is => :wrong_size, :minimum => :size_too_small, :maximum => :size_too_big }.freeze MESSAGES = { is: :wrong_size, minimum: :size_too_small, maximum: :size_too_big }.freeze
CHECKS = { :is => :==, :minimum => :>=, :maximum => :<= }.freeze CHECKS = { is: :==, minimum: :>=, maximum: :<= }.freeze
DEFAULT_TOKENIZER = lambda { |value| value.split(//) } DEFAULT_TOKENIZER = lambda { |value| value.split(//) }
RESERVED_OPTIONS = [:minimum, :maximum, :within, :is, :tokenizer, :too_short, :too_long] RESERVED_OPTIONS = [:minimum, :maximum, :within, :is, :tokenizer, :too_short, :too_long]
@ -25,8 +26,8 @@ class FileSizeValidator < ActiveModel::EachValidator
keys.each do |key| keys.each do |key|
value = options[key] value = options[key]
unless value.is_a?(Integer) && value >= 0 unless (value.is_a?(Integer) && value >= 0) || value.is_a?(Symbol)
raise ArgumentError, ":#{key} must be a nonnegative Integer" raise ArgumentError, ":#{key} must be a nonnegative Integer or symbol"
end end
end end
end end
@ -39,6 +40,14 @@ class FileSizeValidator < ActiveModel::EachValidator
CHECKS.each do |key, validity_check| CHECKS.each do |key, validity_check|
next unless check_value = options[key] next unless check_value = options[key]
check_value =
case check_value
when Integer
check_value
when Symbol
record.send(check_value)
end
value ||= [] if key == :maximum value ||= [] if key == :maximum
value_size = value.size value_size = value.size