human_bytes 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 183acd226d1223244610b305c2acd982a7e67ed3
4
+ data.tar.gz: 21d1bbd52bda1348a9df0288715ae33f3ed4425d
5
+ SHA512:
6
+ metadata.gz: 96f5c4fb99eaa98a69f583cebc7880dd80e385929f997d9622607776134bbb186f87a0a4a41a38b75c171f26b69054bcac559d6e7c5441fd398094de42e777d3
7
+ data.tar.gz: c2c648247c87e915d9a67a295e0195f923f7533f1b11473f3436e4c130467a831c9193a334d32cc6f6133284c02af369e9d9b1adfc7637c5a81a8e475b62d5f3
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in human_bytes.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,41 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec feature)
6
+
7
+ ## Uncomment to clear the screen before every task
8
+ # clearing :on
9
+
10
+ ## Guard internally checks for changes in the Guardfile and exits.
11
+ ## If you want Guard to automatically start up again, run guard in a
12
+ ## shell loop, e.g.:
13
+ ##
14
+ ## $ while bundle exec guard; do echo "Restarting Guard..."; done
15
+ ##
16
+ ## Note: if you are using the `directories` clause above and you are not
17
+ ## watching the project directory ('.'), the you will want to move the Guardfile
18
+ ## to a watched dir and symlink it back, e.g.
19
+ #
20
+ # $ mkdir config
21
+ # $ mv Guardfile config/
22
+ # $ ln -s config/Guardfile .
23
+ #
24
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
25
+
26
+ # Note: The cmd option is now required due to the increasing number of ways
27
+ # rspec may be run, below are examples of the most common uses.
28
+ # * bundler: 'bundle exec rspec'
29
+ # * bundler binstubs: 'bin/rspec'
30
+ # * spring: 'bin/rspec' (This will use spring if running and you have
31
+ # installed the spring binstubs per the docs)
32
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
33
+ # * 'just' rspec: 'rspec'
34
+
35
+ guard :rspec, cmd: "bundle exec rspec" do
36
+ watch('spec/spec_helper.rb') { "spec" }
37
+ watch(%r{^spec/.+_spec\.rb})
38
+ watch(%r{^lib/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
39
+ end
40
+
41
+
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Petr Skocik
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # HumanBytes
2
+
3
+ Convert bytesizes into a human-readable format
4
+
5
+ Either uses decimal byte prefixes or prefixes based on powers of 2 (Ki, Mi, etc.).
6
+ The functionality is exposed as a:
7
+
8
+ - module function named `HumanBytes.human_bytes`
9
+ - includable submodule `HumanBytes::MethodVersion` for numeric types
10
+ - simple executable named `human_bytes`
11
+
12
+ Arbitrary precision arithmetic is used so that even astronomical bytesizes are handled correctly.
13
+
14
+ ## Module function
15
+ include HumanBytes
16
+ Humanbytes.human_bytes(1024) #=> '1.00 KiB'
17
+ human_bytes(1050, places: 10) #=> '1.0253906250 KiB'
18
+ human_bytes(1050, places: 3, i: false) #=> '1.050 KB'
19
+
20
+ ## Includable submodule
21
+
22
+ Numeric.include(HumanBytes::MethodVersion)
23
+ #Same as: HumanBytes.monkey_patch!(Numeric)
24
+ 1024.human_bytes #=> '1.00 KiB'
25
+ 1050.human_bytes(places: 10) #=> '1.0253906250 KiB'
26
+ 1050.human_bytes(places: 3, i: false) #=> '1.050 KB'
27
+
28
+ ## Executable
29
+ $ echo 1024 | human_bytes -d #=> 1.00 KiB
30
+ $ echo 352853503285093280958325083205832| human_bytes #=> 291873576.99 YiB
31
+
32
+ ## Installation
33
+
34
+ Add this line to your application's Gemfile:
35
+
36
+ ```ruby
37
+ gem 'human_bytes'
38
+ ```
39
+
40
+ And then execute:
41
+
42
+ $ bundle
43
+
44
+ Or install it yourself as:
45
+
46
+ $ gem install human_bytes
47
+
48
+ ## Usage
49
+
50
+ TODO: Improve the executable.
51
+
52
+ ## Development
53
+
54
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
55
+
56
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it ( https://github.com/[my-github-username]/human_bytes/fork )
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create a new Pull Request
65
+
66
+ Copyright © Petr Skočík, 2015
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "human_bytes"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/human_bytes ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ require 'human_bytes'
5
+
6
+ decimal = ARGV.delete('-d')
7
+ places = ARGV[0] ? Integer(ARGV[0]) : nil
8
+
9
+ STDIN.each do |num_str|
10
+ puts HumanBytes.human_bytes(Integer(num_str), i: !decimal, places: (places || 2) )
11
+ end
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/exe/human_bytes ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ require 'human_bytes'
5
+
6
+ decimal = ARGV.delete('-d')
7
+ places = ARGV[0] ? Integer(ARGV[0]) : nil
8
+
9
+ STDIN.each do |num_str|
10
+ puts HumanBytes.human_bytes(Integer(num_str), i: !decimal, places: (places || 2) )
11
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'human_bytes/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "human_bytes"
8
+ spec.version = HumanBytes::VERSION
9
+ spec.authors = ["Petr Skocik"]
10
+ spec.email = ["pskocik@gmail.com"]
11
+
12
+ spec.summary = %q{Convert bytesizes into a human-readable format}
13
+ spec.description = %q{Either uses decimal byte prefixes or prefixes based on powers of 2 (Ki, Mi, etc.).}
14
+ spec.homepage = "https://github.com/pjump/human_bytes"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'flt'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "guard-rspec"
27
+ spec.add_development_dependency "rspec-mocks"
28
+ end
@@ -0,0 +1,3 @@
1
+ module HumanBytes
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,72 @@
1
+ require "human_bytes/version"
2
+ require 'flt'
3
+
4
+ module HumanBytes
5
+ UNIT_SETS = [
6
+ {
7
+ B: 1,
8
+ KiB: 2**10,
9
+ MiB: 2**20,
10
+ GiB: 2**30,
11
+ TiB: 2**40,
12
+ PiB: 2**50,
13
+ EiB: 2**60,
14
+ ZiB: 2**70,
15
+ YiB: 2**80
16
+ },
17
+ {
18
+ B: 1,
19
+ KB: 10**3,
20
+ MB: 10**6,
21
+ GB: 10**9,
22
+ TB: 10**12,
23
+ PB: 10**15,
24
+ EB: 10**18,
25
+ ZB: 10**21,
26
+ YB: 10**24,
27
+ }
28
+ ]
29
+ B = 1
30
+ UNIT_SETS.each do |prefix_set|
31
+ prefix_set.each_pair do |prefix,value|
32
+ self.const_set(prefix,value) unless prefix == :B
33
+ end
34
+ end
35
+
36
+ ##
37
+ # places (Integer): number of places after the decimal point
38
+ # i (Boolean): use IT prefixes based on powers of 2 rather than 10
39
+
40
+ DEFAULTS = { places: 2, i: true }
41
+ def human_bytes(byte_size, opts={})
42
+ opts = DEFAULTS.merge(opts)
43
+ places = opts[:places]
44
+ i = opts[:i]
45
+
46
+ unit_sizes = UNIT_SETS[ i ? 0 : 1]
47
+
48
+ last_unit = :B
49
+ unit_sizes.each_pair do |unit,unit_size|
50
+ break if byte_size < unit_size
51
+ last_unit = unit
52
+ end
53
+ last_unit_size = unit_sizes[last_unit]
54
+
55
+ qty = (Flt::DecNum(byte_size)/(last_unit_size)).round(places: places)
56
+ "#{qty} #{last_unit}"
57
+ end
58
+ module_function :human_bytes
59
+
60
+ module MethodVersion
61
+ def human_bytes(opts={})
62
+ HumanBytes.human_bytes(self, opts)
63
+ end
64
+ end
65
+
66
+ def self.monkey_patch!(klass)
67
+ klass.class_eval do
68
+ self.include HumanBytes::MethodVersion
69
+ end
70
+ end
71
+
72
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: human_bytes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Petr Skocik
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: flt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-mocks
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Either uses decimal byte prefixes or prefixes based on powers of 2 (Ki,
98
+ Mi, etc.).
99
+ email:
100
+ - pskocik@gmail.com
101
+ executables:
102
+ - human_bytes
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - ".rspec"
108
+ - ".travis.yml"
109
+ - Gemfile
110
+ - Guardfile
111
+ - LICENSE.txt
112
+ - README.md
113
+ - Rakefile
114
+ - bin/console
115
+ - bin/human_bytes
116
+ - bin/setup
117
+ - exe/human_bytes
118
+ - human_bytes.gemspec
119
+ - lib/human_bytes.rb
120
+ - lib/human_bytes/version.rb
121
+ homepage: https://github.com/pjump/human_bytes
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.2.1
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Convert bytesizes into a human-readable format
145
+ test_files: []