Class: Filesize
Constant Summary collapse
- TYPE_PREFIXES =
{ # Unit prefixes used for SI file sizes. :SI => %w{k M G T P E Z Y}, # Unit prefixes used for binary file sizes. :BINARY => %w{Ki Mi Gi Ti Pi Ei Zi Yi} }
- PREFIXES =
Deprecated.
Please use TYPE_PREFIXES instead
TYPE_PREFIXES[:SI]
- SI =
Set of rules describing file sizes according to SI units.
{ :regexp => /^([\d,.]+)?\s?([kmgtpezy]?)b$/i, :multiplier => 1000, :prefixes => TYPE_PREFIXES[:SI], :presuffix => '' # deprecated }
- BINARY =
Set of rules describing file sizes according to binary units.
{ :regexp => /^([\d,.]+)?\s?(?:([kmgtpezy])i)?b$/i, :multiplier => 1024, :prefixes => TYPE_PREFIXES[:BINARY], :presuffix => 'i' # deprecated }
- Floppy =
The size of a floppy disk
Filesize.from("1474 KiB")
- CD =
The size of a CD
Filesize.from("700 MB")
- DVD_5 =
The size of a common DVD
Filesize.from("4.38 GiB")
- DVD =
The same as a DVD 5
DVD_5- DVD_9 =
The size of a single-sided dual-layer DVD
Filesize.from("7.92 GiB")
- DVD_10 =
The size of a double-sided single-layer DVD
DVD_5 * 2
- DVD_14 =
The size of a double-sided DVD, combining a DVD-9 and a DVD-5
DVD_9 + DVD_5
- DVD_18 =
The size of a double-sided dual-layer DVD
DVD_14 * 2
- ZIP =
The size of a Zip disk
Filesize.from("100 MB")
Class Method Summary collapse
-
.from(arg) ⇒ Filesize
Parses a string, which describes a file size, and returns a Filesize object.
- .parse(string) ⇒ Hash<:prefix, :size, :type> private
Instance Method Summary collapse
- #*(other) ⇒ Filesize
- #+(other) ⇒ Filesize
- #-(other) ⇒ Filesize
- #/(other) ⇒ Filesize
- #<=>(other) ⇒ Object
- #coerce(other) ⇒ Array<self, other> private
-
#initialize(size, type = BINARY) ⇒ Filesize
constructor
A new instance of Filesize.
-
#pretty ⇒ String
Same as #to_s but with an automatic determination of the most sensible unit.
-
#to(unit = 'B') ⇒ Float
(also: #to_f)
Returns the size in a given unit.
-
#to_i ⇒ Number
(also: #to_int)
Returns the size in bytes.
-
#to_s(unit = 'B') ⇒ String
Same as #to_f, but as a string, with the unit appended.
Constructor Details
#initialize(size, type = BINARY) ⇒ Filesize
Returns a new instance of Filesize.
31 32 33 34 |
# File 'lib/dbfire/filesize.rb', line 31 def initialize(size, type = BINARY) @bytes = size.to_i @type = type end |
Class Method Details
.from(arg) ⇒ Filesize
Parses a string, which describes a file size, and returns a Filesize object.
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/dbfire/filesize.rb', line 129 def from(arg) parts = parse(arg) prefix = parts[:prefix] size = parts[:size] type = parts[:type] raise ArgumentError, "Unparseable filesize" unless type offset = (type[:prefixes].map { |s| s[0].chr.downcase }.index(prefix.downcase) || -1) + 1 new(size * (type[:multiplier] ** (offset)), type) end |
.parse(string) ⇒ Hash<:prefix, :size, :type>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/dbfire/filesize.rb', line 144 def parse(string) type = nil # in this order, so we prefer binary :) [BINARY, SI].each { |_type| if string =~ _type[:regexp] type = _type break end } prefix = $2 || '' size = ($1 || 0).to_f return { :prefix => prefix, :size => size, :type => type} end |
Instance Method Details
#*(other) ⇒ Filesize
98 99 100 |
# File 'lib/dbfire/filesize.rb', line 98 def *(other) self.class.new(@bytes * other.to_i, @type) end |
#+(other) ⇒ Filesize
88 89 90 |
# File 'lib/dbfire/filesize.rb', line 88 def +(other) self.class.new(@bytes + other.to_i, @type) end |
#-(other) ⇒ Filesize
93 94 95 |
# File 'lib/dbfire/filesize.rb', line 93 def -(other) self.class.new(@bytes - other.to_i, @type) end |
#/(other) ⇒ Filesize
103 104 105 106 107 108 109 110 |
# File 'lib/dbfire/filesize.rb', line 103 def /(other) result = @bytes / other.to_f if other.is_a? Filesize result else self.class.new(result, @type) end end |
#<=>(other) ⇒ Object
112 113 114 |
# File 'lib/dbfire/filesize.rb', line 112 def <=>(other) self.to_i <=> other.to_i end |
#coerce(other) ⇒ Array<self, other>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
118 119 120 |
# File 'lib/dbfire/filesize.rb', line 118 def coerce(other) return self, other end |
#pretty ⇒ String
Same as #to_s but with an automatic determination of the most sensible unit.
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/dbfire/filesize.rb', line 73 def pretty size = @bytes if size < @type[:multiplier] unit = "B" else pos = (Math.log(size) / Math.log(@type[:multiplier])).floor pos = @type[:prefixes].size-1 if pos > @type[:prefixes].size - 1 unit = @type[:prefixes][pos-1] + "B" end to_s(unit) end |
#to(unit = 'B') ⇒ Float Also known as: to_f
Returns the size in a given unit.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/dbfire/filesize.rb', line 44 def to(unit = 'B') to_parts = self.class.parse(unit) prefix = to_parts[:prefix] if prefix == 'B' or prefix.empty? return to_i.to_f end to_type = to_parts[:type] size = @bytes pos = (@type[:prefixes].map { |s| s[0].chr.downcase }.index(prefix.downcase) || -1) + 1 size = size/(to_type[:multiplier].to_f**(pos)) unless pos < 1 end |
#to_i ⇒ Number Also known as: to_int
Returns the size in bytes.
37 38 39 |
# File 'lib/dbfire/filesize.rb', line 37 def to_i @bytes end |
#to_s(unit = 'B') ⇒ String
Returns Same as #to_f, but as a string, with the unit appended.
64 65 66 |
# File 'lib/dbfire/filesize.rb', line 64 def to_s(unit = 'B') "%.2f %s" % [to(unit).to_f.to_s, unit] end |