filesize 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1 @@
1
+ v0.0.1. first version. enjoy!
data/Manifest ADDED
@@ -0,0 +1,5 @@
1
+ Rakefile
2
+ README.markdown
3
+ CHANGELOG
4
+ lib/filesize.rb
5
+ Manifest
data/README.markdown ADDED
@@ -0,0 +1,37 @@
1
+ ## filesize
2
+ filesize.rb provides a class for easily working with file sizes.
3
+ That means:
4
+
5
+ * Parsing strings (e.g. "1 GiB") and saving it internally as bytes
6
+ * Handling both SI and binary prefixes
7
+ * Converting from any type and unit to any other (SI to SI, SI to Binary and so on)
8
+ * doing calculatings with filesizes (in a smart way, see Usage for more)
9
+ * filesize.rb also provides some default sizes, like the ones of DVDs
10
+
11
+ ## Usage
12
+ ### Parsing a string
13
+ Filesize.from("1 GiB") # => #<Filesize:0x93c06c8 @bytes=1073741824, @type={:regexp=>/^([\d,.]+)?\s?(?:([kmgtpezy])i)?b$/i, :multiplier=>1024, :presuffix=>"i"}>
14
+
15
+ ### Converting filesizes
16
+ Filesize.from("1 GiB").to_f('KiB') # => 1048576.0
17
+ Filesize.from("1 GiB").to_f('KB') # => 1073741.824
18
+ Filesize.from("1 GB").to_i # => 1000000000
19
+
20
+ ### Outputting filesizes
21
+ Filesize.from("12502343 B").to_s('GiB') # => "0.01 GiB"
22
+ Filesize.from("12502343 B").pretty # => "11.92 MiB"
23
+
24
+ ### Calculating with filesizes
25
+ #### The file size on the left side sets the type
26
+ (Filesize.from("1400 MB") + Filesize.from("1400 MiB")).pretty # => "2.87 GB"
27
+ (Filesize.from("1400 MiB") + Filesize.from("1400 MB")).pretty # => "2.67 GiB"
28
+
29
+ #### Filesizes can also be coerced
30
+ (Filesize.from("1400 MiB") + 1024).pretty # => "1.37 GiB"
31
+ (1024 + Filesize.from("1400 MB")).pretty # => "1.40 GB"
32
+
33
+ #### filesize.rb is smart about the return value
34
+ Filesize.from("1400 MiB") / Filesize.from("700 MiB") # => 2.0
35
+
36
+ #### One can also use predefined sizes
37
+ Filesize::DVD / Filesize::CD # => 6.13566756571429
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require 'echoe'
2
+ Echoe.new('filesize') do |p|
3
+ p.author = "Dominik Honnef"
4
+ p.email = "dominikho@gmx.net"
5
+
6
+ p.description = %q{filesize is a small class for handling filesizes with both the SI and binary prefixes, allowing conversion from any size to any other size.}
7
+ p.summary = %q{filesize is a small class for handling filesizes with both the SI and binary prefixes, allowing conversion from any size to any other size.}
8
+
9
+ p.url = "http://filesize.rubyforge.org/"
10
+ p.docs_host = "rubyforge.org:/var/www/gforge-projects/"
11
+
12
+ p.ruby_version = '>= 1.8.6'
13
+
14
+ p.require_signed = true
15
+
16
+ p.project = 'filesize'
17
+ end
data/filesize.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{filesize}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Dominik Honnef"]
9
+ s.cert_chain = ["/home/dominikh/.rubyforge/gem-public_cert.pem"]
10
+ s.date = %q{2009-07-26}
11
+ s.description = %q{filesize is a small class for handling filesizes with both the SI and binary prefixes, allowing conversion from any size to any other size.}
12
+ s.email = %q{dominikho@gmx.net}
13
+ s.extra_rdoc_files = ["README.markdown", "CHANGELOG", "lib/filesize.rb"]
14
+ s.files = ["Rakefile", "README.markdown", "CHANGELOG", "lib/filesize.rb", "Manifest", "filesize.gemspec"]
15
+ s.has_rdoc = true
16
+ s.homepage = %q{http://filesize.rubyforge.org/}
17
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Filesize", "--main", "README.markdown"]
18
+ s.require_paths = ["lib"]
19
+ s.required_ruby_version = Gem::Requirement.new(">= 1.8.6")
20
+ s.rubyforge_project = %q{filesize}
21
+ s.rubygems_version = %q{1.3.1}
22
+ s.signing_key = %q{/home/dominikh/.rubyforge/gem-private_key.pem}
23
+ s.summary = %q{filesize is a small class for handling filesizes with both the SI and binary prefixes, allowing conversion from any size to any other size.}
24
+
25
+ if s.respond_to? :specification_version then
26
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
27
+ s.specification_version = 2
28
+
29
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
30
+ else
31
+ end
32
+ else
33
+ end
34
+ end
data/lib/filesize.rb ADDED
@@ -0,0 +1,121 @@
1
+ class Filesize
2
+ SI = {:regexp => /^([\d,.]+)?\s?([kmgtpezy]?)b$/i, :multiplier => 1000, :presuffix => ''}
3
+ BINARY = {:regexp => /^([\d,.]+)?\s?(?:([kmgtpezy])i)?b$/i, :multiplier => 1024, :presuffix => 'i'}
4
+ PREFIXES = %w{k M G T P E Z Y}
5
+
6
+ def initialize(size, type = BINARY)
7
+ @bytes = size.to_i
8
+ @type = type
9
+ end
10
+
11
+ # Returns the size in bytes
12
+ def to_i
13
+ @bytes
14
+ end
15
+ alias_method :to_int, :to_i
16
+
17
+ # Returns the size in the given unit, as a float
18
+ def to(unit = 'B')
19
+ to_parts = self.class.parse(unit)
20
+ prefix = to_parts[:prefix]
21
+
22
+ if prefix == 'B' or prefix.empty?
23
+ return to_i.to_f
24
+ end
25
+
26
+ to_type = to_parts[:type]
27
+ size = @bytes
28
+
29
+ pos = (PREFIXES.map{|s|s.downcase}.index(prefix.downcase) || -1) + 1
30
+
31
+ size = size/(to_type[:multiplier].to_f**(pos)) unless pos < 1
32
+ end
33
+ alias_method :to_f, :to
34
+
35
+ # Same as to_f, but as a string, with the unit appended
36
+ def to_s(unit = 'B')
37
+ "%.2f %s" % [to(unit).to_f.to_s, unit]
38
+ end
39
+
40
+ # Same as to_s but with an automatic determinition of the most
41
+ # sensible unit
42
+ def pretty
43
+ size = @bytes
44
+ if size < @type[:multiplier]
45
+ unit = "B"
46
+ else
47
+ pos = (Math.log(size) / Math.log(@type[:multiplier])).floor
48
+ pos = PREFIXES.size-1 if pos > PREFIXES.size - 1
49
+
50
+ unit = PREFIXES[pos-1].to_s + @type[:presuffix] + "B"
51
+ end
52
+
53
+ to_s(unit)
54
+ end
55
+
56
+ def +(other)
57
+ self.class.new(@bytes + other.to_i, @type)
58
+ end
59
+
60
+ def -(other)
61
+ self.class.new(@bytes - other.to_i, @type)
62
+ end
63
+
64
+ def *(other)
65
+ self.class.new(@bytes * other.to_i, @type)
66
+ end
67
+
68
+ def /(other)
69
+ result = @bytes / other.to_f
70
+ if other.is_a? Filesize
71
+ result
72
+ else
73
+ self.class.new(result, @type)
74
+ end
75
+ end
76
+
77
+ def coerce(other)
78
+ return self, other
79
+ end
80
+
81
+ class << self
82
+ def from(arg)
83
+ parts = parse(arg)
84
+ prefix = parts[:prefix]
85
+ size = parts[:size]
86
+ type = parts[:type]
87
+
88
+ raise ArgumentError, "Unparseable filesize" unless type
89
+
90
+ offset = (PREFIXES.map{|s|s.downcase}.index(prefix.downcase) || -1) + 1
91
+
92
+ new(size * (type[:multiplier] ** (offset)), type)
93
+ end
94
+
95
+ def parse(string)
96
+ type = nil
97
+ # in this order, so we prefer binary :)
98
+ [BINARY, SI].each { |_type|
99
+ if string =~ _type[:regexp]
100
+ type = _type
101
+ break
102
+ end
103
+ }
104
+
105
+ prefix = $2 || ''
106
+ size = ($1 || "0").gsub(", ", "").to_i
107
+
108
+ return { :prefix => prefix, :size => size, :type => type}
109
+ end
110
+ end
111
+
112
+ Floppy = Filesize.from("1474 KiB")
113
+ CD = Filesize.from("700 MB")
114
+ DVD_5 = Filesize.from("4.38 GiB")
115
+ DVD = DVD_5
116
+ DVD_9 = Filesize.from("7.92 GiB")
117
+ DVD_10 = DVD_5 * 2
118
+ DVD_14 = Filesize.from("7.92 GiB") + DVD_5
119
+ DVD_18 = DVD_14 * 2
120
+ ZIP = Filesize.from("100 MB")
121
+ end
data.tar.gz.sig ADDED
@@ -0,0 +1,3 @@
1
+ ��[�f�;k����9��M b����TF�;�8i�P���R_������N���S"T�o-A������k;m�D�D'��m{G��OE��G�i��֟��y� �^� ��la����ʘ�4#�
2
+ �RA��ae���5��B� �5��鴄
3
+ `"�v�8G� ;�償u�R�ba�#Ĩ$U�k,��4�����-�S�qH��&���q9�"� �.e�, �
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: filesize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dominik Honnef
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDMDCCAhigAwIBAgIBADANBgkqhkiG9w0BAQUFADA+MRIwEAYDVQQDDAlkb21p
14
+ bmlraG8xEzARBgoJkiaJk/IsZAEZFgNnbXgxEzARBgoJkiaJk/IsZAEZFgNuZXQw
15
+ HhcNMDkwNzI0MjE1NDQ3WhcNMTAwNzI0MjE1NDQ3WjA+MRIwEAYDVQQDDAlkb21p
16
+ bmlraG8xEzARBgoJkiaJk/IsZAEZFgNnbXgxEzARBgoJkiaJk/IsZAEZFgNuZXQw
17
+ ggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCgfogFNoAcdrR5mK0t5NiN
18
+ MbWCH2QVLqOvpuaHN6uiaE30R9fT+1CcF97PSboMHMKaQFgiyAcBj8c2uGw5Sk7+
19
+ Kv9TE2TIIG8l8oslnf9PVqrHSz823IjzbXo4WET4khI2v02pE2wx/souJ4PRgnEr
20
+ gxQaOF7fqJRT2eYiZAMQye1y9tNSShRoKP3JIlp51KBXUMzGdzq4Mopp32zhEcio
21
+ yEzcp1PYqu6D1Pr00fS/Alurnp/My5o3oIk0vBfJ6Q8j/6OSk+ck5xev2Z2Y9AUK
22
+ iwipCxKrAsfOjpPc/Y66okMeTmKxx5N/8h/xNp/HiUApHQwjbYgFOAwmht5UzoPp
23
+ AgMBAAGjOTA3MAkGA1UdEwQCMAAwHQYDVR0OBBYEFLlluBMEZ3rFvxFcB+ULjUNd
24
+ V+7WMAsGA1UdDwQEAwIEsDANBgkqhkiG9w0BAQUFAAOCAQEAoDJGicQwBqMnhMkC
25
+ JY7bgsrIwxoTMQT6B1keUtt9VQ3deywQu0ogawNmee5wy5oFqnMZFcSCXcJE/zIE
26
+ iuSYU5t87iF09BIsSHWg2OlHAB4+GTUL0PkVsCVBDFukqEiXpNcqMUjTr8U+fe7V
27
+ oD/qvzyMG1ooAFuTDfPknIQxTcavdnmUmO9Sfw10yXkomX8jjwaGduF/JHDIufub
28
+ /f4b22rpLBhrOD6dqHCl7WuAfAhpobLqc7sa6Ro8caSUnpq9blOrT/PRtiTXwN6h
29
+ p3eI3JV0LC6/tMHoeLAbHqauVbWUqDB+AfFw24sgZBqFA+MtMkDpqZ+BKiwiHl+S
30
+ YEfDWg==
31
+ -----END CERTIFICATE-----
32
+
33
+ date: 2009-07-26 00:00:00 +02:00
34
+ default_executable:
35
+ dependencies: []
36
+
37
+ description: filesize is a small class for handling filesizes with both the SI and binary prefixes, allowing conversion from any size to any other size.
38
+ email: dominikho@gmx.net
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - README.markdown
45
+ - CHANGELOG
46
+ - lib/filesize.rb
47
+ files:
48
+ - Rakefile
49
+ - README.markdown
50
+ - CHANGELOG
51
+ - lib/filesize.rb
52
+ - Manifest
53
+ - filesize.gemspec
54
+ has_rdoc: true
55
+ homepage: http://filesize.rubyforge.org/
56
+ post_install_message:
57
+ rdoc_options:
58
+ - --line-numbers
59
+ - --inline-source
60
+ - --title
61
+ - Filesize
62
+ - --main
63
+ - README.markdown
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 1.8.6
71
+ version:
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "1.2"
77
+ version:
78
+ requirements: []
79
+
80
+ rubyforge_project: filesize
81
+ rubygems_version: 1.3.1
82
+ signing_key:
83
+ specification_version: 2
84
+ summary: filesize is a small class for handling filesizes with both the SI and binary prefixes, allowing conversion from any size to any other size.
85
+ test_files: []
86
+
metadata.gz.sig ADDED
@@ -0,0 +1,3 @@
1
+ J>M���y�+}�ɻ�� &����<�Ł{�} *�G͘��j�xn���s����K�}�Z��T�Lvt�8�����(j�����y0j�7'���0�v�i�jM�
2
+ �|FG��cz!_g��µr&L�-�/[pZ~��֜�m������<��0Úͼ5_y�Ғ��/?2YaZҝy�R�&8�K��M�7���7W}�_���������<6�e���g��uu
3
+ �29����
4
+ �a�� @8�&P� y��r