lib/attached.rb in attached-0.0.9 vs lib/attached.rb in attached-0.1.0
- old
+ new
@@ -29,12 +29,13 @@
# Add an attachment to a class.
#
# Options:
#
- # * :styles -
- # * :storage -
+ # * :styles -
+ # * :storage -
+ # * :processor -
#
# Usage:
#
# has_attached :video
# has_attached :video, :storage => :s3
@@ -116,12 +117,12 @@
message ||= "size must be a maximum of :maximum" if minimum == zero
message ||= "size must be between :minimum and :maximum"
range = minimum..maximum
- message.gsub!(/:minimum/, number_to_human_size(minimum)) unless minimum == zero
- message.gsub!(/:maximum/, number_to_human_size(maximum)) unless maximum == infi
+ message.gsub!(/:minimum/, number_to_size(minimum)) unless minimum == zero
+ message.gsub!(/:maximum/, number_to_size(maximum)) unless maximum == infi
validates_inclusion_of :"#{name}_size", :in => range, :message => message,
:if => options[:if], :unless => options[:unless]
end
@@ -149,24 +150,32 @@
private
- SINGULAR = 1
+ # Convert a number to a human readable size.
+ #
+ # Usage:
+ #
+ # number_to_size(1) # 1 byte
+ # number_to_size(2) # 2 bytes
+ # number_to_size(1024) # 1 kilobyte
+ # number_to_size(2048) # 2 kilobytes
- def number_to_human_size(number, options = {})
+ def number_to_size(number, options = {})
return if number == 0.0 / 1.0
return if number == 1.0 / 0.0
- base = 1024
- units = ["byte", "kilobyte", "megabyte", "gigabyte", "terabyte", "petabyte"]
+ singular = options['singular'] || 1
+ base = options['base'] || 1024
+ units = options['units'] || ["byte", "kilobyte", "megabyte", "gigabyte", "terabyte", "petabyte"]
exponent = (Math.log(number) / Math.log(base)).floor
number /= base ** exponent
unit = units[exponent]
- number == SINGULAR ? unit.gsub!(/s$/, '') : unit.gsub!(/$/, 's')
+ number == singular ? unit.gsub!(/s$/, '') : unit.gsub!(/$/, 's')
"#{number} #{unit}"
end
\ No newline at end of file