size_units 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/ChangeLog +0 -0
- data/Gemfile +17 -0
- data/Guardfile +8 -0
- data/LICENSE +22 -0
- data/README.md +22 -0
- data/Rakefile +18 -0
- data/lib/size_units.rb +3 -0
- data/lib/size_units/size.rb +56 -0
- data/lib/size_units/units.rb +15 -0
- data/lib/size_units/version.rb +3 -0
- data/size_units.gemspec +15 -0
- data/specs/spec_helper.rb +25 -0
- data/specs/unit/size_spec.rb +26 -0
- data/specs/unit/units_spec.rb +22 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 82727be11a4559dc9b12d1dc8707ff1f6dc10aca
|
4
|
+
data.tar.gz: 5453daf4b4de78e89c59b1c9204718e986c43920
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 75bc86fda4b54f59748f757a986f1bb90e902687591996a2919ef0de004bb77b2b4248c19336a90064f9c9a2199a7510af6d4d0ed2ef7f2077dcefd5087317e7
|
7
|
+
data.tar.gz: fdc21bb0f89bb3fcd9d6638c720ddfd800ee5d51f5b461eada442e842619edcb5b4d59bfe0a140297cb0acc749c1429acb82589a2f176e6a8ab5306a0760bf2d
|
data/.gitignore
ADDED
data/ChangeLog
ADDED
File without changes
|
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
gem 'rake'
|
6
|
+
|
7
|
+
group(:test) do
|
8
|
+
gem 'schmurfy-bacon'
|
9
|
+
gem 'mocha', '~> 0.10.0'
|
10
|
+
gem 'factory_girl'
|
11
|
+
|
12
|
+
gem 'simplecov'
|
13
|
+
gem 'guard'
|
14
|
+
gem 'guard-bacon'
|
15
|
+
gem 'rb-fsevent'
|
16
|
+
gem 'growl'
|
17
|
+
end
|
data/Guardfile
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
|
2
|
+
# parameters:
|
3
|
+
# output => the formatted to use
|
4
|
+
# backtrace => number of lines, nil = everything
|
5
|
+
guard 'bacon', :output => "BetterOutput", :backtrace => nil do
|
6
|
+
watch(%r{^lib/size_units/(.+)\.rb$}) { |m| "specs/unit/#{m[1]}_spec.rb" }
|
7
|
+
watch(%r{specs/.+\.rb$})
|
8
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Julien Ammous
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# SizeUnits
|
2
|
+
|
3
|
+
Just a simple alternative to the activesupport helpers if you don't want to
|
4
|
+
add activesupport as dependency to your gem/application.
|
5
|
+
|
6
|
+
It works the same way:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
2.kilobytes
|
10
|
+
1.bytes
|
11
|
+
3.gigabytes
|
12
|
+
```
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
# Human size
|
17
|
+
|
18
|
+
Another activesupport alternative
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
1500.human_size => "2 bytes"
|
22
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
|
5
|
+
task :default => :test
|
6
|
+
|
7
|
+
task :test do
|
8
|
+
require 'bacon'
|
9
|
+
|
10
|
+
ENV['COVERAGE'] = "1"
|
11
|
+
|
12
|
+
Dir[File.expand_path('../specs/**/*_spec.rb', __FILE__)].each do |file|
|
13
|
+
puts "File: #{file}:"
|
14
|
+
load(file)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
data/lib/size_units.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
|
2
|
+
module HumanSize
|
3
|
+
class << self
|
4
|
+
attr_accessor :units
|
5
|
+
end
|
6
|
+
|
7
|
+
self.units = [
|
8
|
+
[1.terabytes, :terabytes, "TB"],
|
9
|
+
[1.gigabytes, :gigabytes, "GB"],
|
10
|
+
[1.megabytes, :megabytes, "MB"],
|
11
|
+
[1.kilobyte, :kilobyes, "KB"],
|
12
|
+
[1, :seconds, "B"]
|
13
|
+
]
|
14
|
+
|
15
|
+
##
|
16
|
+
# Format a size for display.
|
17
|
+
#
|
18
|
+
# @param [Integer,nil] limit if set this will limit the
|
19
|
+
# number of fields shown (ex: 2 = 2 MB, 3B)
|
20
|
+
#
|
21
|
+
# @return [String] the formatted duration
|
22
|
+
#
|
23
|
+
def human_size(limit = nil)
|
24
|
+
ret = []
|
25
|
+
diff = Hash.new(0)
|
26
|
+
t = self.to_i
|
27
|
+
|
28
|
+
while t > 0
|
29
|
+
HumanSize.units.each do |(limit, field)|
|
30
|
+
if t >= limit
|
31
|
+
diff[field] += 1
|
32
|
+
t -= limit
|
33
|
+
break
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
ret = []
|
40
|
+
|
41
|
+
HumanSize.units.each.with_index do |unit|
|
42
|
+
unit_name = unit[1]
|
43
|
+
if diff[unit_name] > 0
|
44
|
+
ret << "#{diff[unit_name]}#{unit[2]}"
|
45
|
+
|
46
|
+
if ret.size == limit
|
47
|
+
break
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
ret.join(', ')
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
Numeric.send(:include, HumanSize)
|
@@ -0,0 +1,15 @@
|
|
1
|
+
|
2
|
+
module SizeUnits
|
3
|
+
def bytes() self end
|
4
|
+
def kilobytes() 1024 * self end
|
5
|
+
def megabytes() 1024**2 * self end
|
6
|
+
def gigabytes() 1024**3 * self end
|
7
|
+
def terabytes() 1024**4 * self end
|
8
|
+
|
9
|
+
instance_methods.select{|m| m !~ /__/}.each do |plural|
|
10
|
+
singular = plural.to_s.chop
|
11
|
+
alias_method singular, plural
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
Numeric.send(:include, SizeUnits)
|
data/size_units.gemspec
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/size_units/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Julien Ammous"]
|
6
|
+
gem.email = ["schmurfy@gmail.com"]
|
7
|
+
gem.summary = %q{Some size helpers}
|
8
|
+
gem.homepage = "https://github.com/schmurfy/size_units"
|
9
|
+
|
10
|
+
gem.files = `git ls-files`.split($\)
|
11
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
|
+
gem.name = "size_units"
|
13
|
+
gem.require_paths = ["lib"]
|
14
|
+
gem.version = SizeUnits::VERSION
|
15
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
require 'bacon'
|
5
|
+
|
6
|
+
if ENV['COVERAGE']
|
7
|
+
Bacon.allow_focused_run = false
|
8
|
+
|
9
|
+
require 'simplecov'
|
10
|
+
SimpleCov.start do
|
11
|
+
add_filter ".*_spec"
|
12
|
+
add_filter "/helpers/"
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
$LOAD_PATH.unshift( File.expand_path('../../lib' , __FILE__) )
|
18
|
+
require 'size_units'
|
19
|
+
|
20
|
+
# require 'bacon/ext/mocha'
|
21
|
+
# require 'bacon/ext/em'
|
22
|
+
|
23
|
+
# Thread.abort_on_exception = true
|
24
|
+
|
25
|
+
Bacon.summary_on_exit()
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
describe 'TimeDuration' do
|
4
|
+
before do
|
5
|
+
@b1 = 1
|
6
|
+
@kb1 = 1024
|
7
|
+
@mb1 = 1024**2
|
8
|
+
@gb1 = 1024**3
|
9
|
+
end
|
10
|
+
|
11
|
+
should 'works' do
|
12
|
+
12.human_size.should == "12B"
|
13
|
+
1024.human_size.should == "1KB"
|
14
|
+
2048.human_size.should == "2KB"
|
15
|
+
1044.human_size.should == "1KB, 20B"
|
16
|
+
|
17
|
+
(20*@kb1 + 36).human_size.should == "20KB, 36B"
|
18
|
+
(2*@gb1 + 200*@mb1 + 780).human_size.should == "2GB, 200MB, 780B"
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
should 'limite output' do
|
23
|
+
(2*@gb1 + 200*@mb1 + 780).human_size(1).should == "2GB"
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
describe 'SizeUnits' do
|
4
|
+
should 'works' do
|
5
|
+
1.bytes.should == 1
|
6
|
+
1024.bytes.should == 1024
|
7
|
+
2048.bytes.should == 2048
|
8
|
+
|
9
|
+
1.kilobyte.should == 1024
|
10
|
+
1.kilobytes.should == 1024
|
11
|
+
|
12
|
+
1.megabyte.should == 1024 ** 2
|
13
|
+
2.megabytes.should == 2* 1024 ** 2
|
14
|
+
|
15
|
+
1.gigabyte.should == 1024 ** 3
|
16
|
+
2.gigabytes.should == 2 * 1024 ** 3
|
17
|
+
|
18
|
+
1.terabyte.should == 1024**4
|
19
|
+
1.terabytes.should == 1024**4
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: size_units
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Julien Ammous
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-14 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- schmurfy@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- .gitignore
|
21
|
+
- ChangeLog
|
22
|
+
- Gemfile
|
23
|
+
- Guardfile
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/size_units.rb
|
28
|
+
- lib/size_units/size.rb
|
29
|
+
- lib/size_units/units.rb
|
30
|
+
- lib/size_units/version.rb
|
31
|
+
- size_units.gemspec
|
32
|
+
- specs/spec_helper.rb
|
33
|
+
- specs/unit/size_spec.rb
|
34
|
+
- specs/unit/units_spec.rb
|
35
|
+
homepage: https://github.com/schmurfy/size_units
|
36
|
+
licenses: []
|
37
|
+
metadata: {}
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 2.0.3
|
55
|
+
signing_key:
|
56
|
+
specification_version: 4
|
57
|
+
summary: Some size helpers
|
58
|
+
test_files: []
|