ruby-si-units 0.0.1
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 +7 -0
- data/.gitignore +20 -0
- data/.rspec +2 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +34 -0
- data/LICENSE +20 -0
- data/README.md +14 -0
- data/lib/si_units/distance.rb +34 -0
- data/lib/si_units/unit.rb +80 -0
- data/lib/si_units/version.rb +3 -0
- data/lib/unit.rb +5 -0
- data/ruby-si-unit.gemspec +25 -0
- data/spec/spec_helper.rb +17 -0
- data/spec/units/unit_spec.rb +2 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 37a53da30a5bd8a4abafa0232cc548833633d9cc
|
4
|
+
data.tar.gz: 044bf6506658ad712c4aa5b7744ff97d8d525584
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ad0d00333a12be8d708a16f30c1b556b81e57ea9a89add7c0057a49095c3df8fc0ae416850f759c2a612f8a7f815803a0c85eb193c5c3cd710ef1dac13b55e01
|
7
|
+
data.tar.gz: 6b4900ff045cc5ae37ac4ed026f33fffb3a91d98d8ee4966801418f0bcd54ce7ade3a3c8dd56f2646928dc0de890386b3a8e1fd7984063eec03fac259983e240
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ruby-si-units (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
awesome_print (1.1.0)
|
10
|
+
coderay (1.0.9)
|
11
|
+
diff-lcs (1.2.4)
|
12
|
+
method_source (0.8.2)
|
13
|
+
pry (0.9.12.2)
|
14
|
+
coderay (~> 1.0.5)
|
15
|
+
method_source (~> 0.8)
|
16
|
+
slop (~> 3.4)
|
17
|
+
rspec (2.14.1)
|
18
|
+
rspec-core (~> 2.14.0)
|
19
|
+
rspec-expectations (~> 2.14.0)
|
20
|
+
rspec-mocks (~> 2.14.0)
|
21
|
+
rspec-core (2.14.5)
|
22
|
+
rspec-expectations (2.14.2)
|
23
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
24
|
+
rspec-mocks (2.14.3)
|
25
|
+
slop (3.4.6)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
ruby
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
awesome_print (~> 1.1.0)
|
32
|
+
pry (~> 0.9.12.2)
|
33
|
+
rspec (~> 2.14.1)
|
34
|
+
ruby-si-units!
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 Kenner Kliemann
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
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, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
SI Prefix Units
|
2
|
+
====================
|
3
|
+
A SI prefix unit handling library for ruby
|
4
|
+
|
5
|
+
How it work's ?
|
6
|
+
===
|
7
|
+
<p>
|
8
|
+
Recognizes the scale and unit presets for a unit
|
9
|
+
</p>
|
10
|
+
|
11
|
+
|
12
|
+
Another useful resources
|
13
|
+
===
|
14
|
+
* (ruby-units)[https://github.com/olbrich/ruby-units]
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module SIUnits
|
2
|
+
|
3
|
+
class Distance
|
4
|
+
include Comparable
|
5
|
+
|
6
|
+
UNIT_REGEX = /(\d+)(\w+)/
|
7
|
+
|
8
|
+
attr_reader :value, :unit, :distance
|
9
|
+
|
10
|
+
def initialize(distance)
|
11
|
+
@distance = distance
|
12
|
+
@value, @unit = *split_value(distance)
|
13
|
+
end
|
14
|
+
|
15
|
+
def <=>(comparison)
|
16
|
+
u1 = Unit.new(@unit)
|
17
|
+
u2 = Unit.new(comparison.unit)
|
18
|
+
|
19
|
+
if u1 > u2
|
20
|
+
return 1
|
21
|
+
elsif u1 < u2
|
22
|
+
return -1
|
23
|
+
elsif u1 == u2
|
24
|
+
@value.to_i <=> comparison.value.to_i
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def split_value(value)
|
31
|
+
value.scan(UNIT_REGEX).flatten
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module SIUnits
|
2
|
+
class Unit
|
3
|
+
include Comparable
|
4
|
+
attr_reader :unit_value
|
5
|
+
|
6
|
+
#SI Prefix Units
|
7
|
+
UNITS_DEFINITION = {
|
8
|
+
'googol' => [%w{googol}, 1e100],
|
9
|
+
'yebi' => [%w{Yi Yebi yebi}, 2**80],
|
10
|
+
'zebi' => [%w{Zi Zebi zebi}, 2**70],
|
11
|
+
'exi' => [%w{Ei Exi exi}, 2**60],
|
12
|
+
'pebi' => [%w{Pi Pebi pebi}, 2**50],
|
13
|
+
'tebi' => [%w{Ti Tebi tebi}, 2**40],
|
14
|
+
'gibi' => [%w{Gi Gibi gibi}, 2**30],
|
15
|
+
'mebi' => [%w{Mi Mebi mebi}, 2**20],
|
16
|
+
'kibi' => [%w{Ki Kibi kibi}, 2**10],
|
17
|
+
'yotta' => [%w{Y Yotta yotta}, 1e24],
|
18
|
+
'zetta' => [%w{Z Zetta zetta}, 1e21],
|
19
|
+
'exa' => [%w{E Exa exa}, 1e18],
|
20
|
+
'peta' => [%w{P Peta peta}, 1e15],
|
21
|
+
'tera' => [%w{T Tera tera}, 1e12],
|
22
|
+
'giga' => [%w{G Giga giga}, 1e9],
|
23
|
+
'mega' => [%w{M Mega mega}, 1e6],
|
24
|
+
'kilo' => [%w{k kilo}, 1e3],
|
25
|
+
'hecto' => [%w{h Hecto hecto}, 1e2],
|
26
|
+
'deca' => [%w{da Deca deca deka}, 1e1],
|
27
|
+
'1' => [%w{const}, 1],
|
28
|
+
'deci' => [%w{d Deci deci}, 1e-1],
|
29
|
+
'centi' => [%w{c Centi centi}, 1e-2],
|
30
|
+
'milli' => [%w{m Milli milli}, 1e-3],
|
31
|
+
'micro' => [%w{u Micro micro}, 1e-6],
|
32
|
+
'nano' => [%w{n Nano nano}, 1e-9],
|
33
|
+
'pico' => [%w{p Pico pico}, 1e-12],
|
34
|
+
'femto' => [%w{f Femto femto}, 1e-15],
|
35
|
+
'atto' => [%w{a Atto atto}, 1e-18],
|
36
|
+
'zepto' => [%w{z Zepto zepto}, 1e-21],
|
37
|
+
'yocto' => [%w{y Yocto yocto}, 1e-24]
|
38
|
+
}
|
39
|
+
|
40
|
+
def initialize(unit)
|
41
|
+
@unit_value = unit
|
42
|
+
@unit_kind = parse_unit
|
43
|
+
end
|
44
|
+
|
45
|
+
def best_scale
|
46
|
+
@best_scale ||= best_value_with_scale
|
47
|
+
end
|
48
|
+
|
49
|
+
def <=>(comparison)
|
50
|
+
UNITS_DEFINITION.find_index(@unit_kind) <=> UNITS_DEFINITION.find_index(comparison.unit_kind)
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def best_value_with_scale
|
56
|
+
aliase, scalar = UNITS_DEFINITION[@unit_kind]
|
57
|
+
[(@unit_value / scalar), aliase.first].join
|
58
|
+
end
|
59
|
+
|
60
|
+
def parse_unit
|
61
|
+
case @unit_value
|
62
|
+
when 1e-15..1e-12 then return "pico"
|
63
|
+
when 1e-12..1e-9 then return "nano"
|
64
|
+
when 1e-9..1e-6 then return "micro"
|
65
|
+
when 1e-6..1e-3 then return "milli"
|
66
|
+
when 1e-3..1e-2 then return "centi"
|
67
|
+
when 1e-2..1e-1 then return "deci"
|
68
|
+
when 1e-1..0e1 then return "1"
|
69
|
+
when 0e1..1e1 then return "1"
|
70
|
+
when 1e1..1e2 then return "deca"
|
71
|
+
when 1e2..1e3 then return "hecto"
|
72
|
+
when 1e3..1e6 then return "kilo"
|
73
|
+
when 1e6..1e9 then return "mega"
|
74
|
+
when 1e9..1e12 then return "giga"
|
75
|
+
else raise "Unit out of range"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
end
|
data/lib/unit.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'si_units/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ruby-si-units"
|
8
|
+
spec.version = SIUnits::VERSION
|
9
|
+
spec.authors = ["Kenner Kliemann"]
|
10
|
+
spec.email = ["kenner.hp@gmail.com"]
|
11
|
+
spec.description = "A SI prefix unit handling library for ruby"
|
12
|
+
spec.summary = "A SI prefix unit handling library for ruby"
|
13
|
+
spec.homepage = "https://github.com/gnomex/ruby-si-units"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.add_development_dependency("awesome_print", "~> 1.1.0")
|
17
|
+
spec.add_development_dependency("pry", "~> 0.9.12.2")
|
18
|
+
spec.add_development_dependency("rspec", "~> 2.14.1")
|
19
|
+
|
20
|
+
spec.files = `git ls-files`.split($/)
|
21
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
22
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-si-units
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kenner Kliemann
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: awesome_print
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.1.0
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.9.12.2
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.9.12.2
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.14.1
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.14.1
|
55
|
+
description: A SI prefix unit handling library for ruby
|
56
|
+
email:
|
57
|
+
- kenner.hp@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .rspec
|
64
|
+
- Gemfile
|
65
|
+
- Gemfile.lock
|
66
|
+
- LICENSE
|
67
|
+
- README.md
|
68
|
+
- lib/si_units/distance.rb
|
69
|
+
- lib/si_units/unit.rb
|
70
|
+
- lib/si_units/version.rb
|
71
|
+
- lib/unit.rb
|
72
|
+
- ruby-si-unit.gemspec
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
- spec/units/unit_spec.rb
|
75
|
+
homepage: https://github.com/gnomex/ruby-si-units
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.0.3
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: A SI prefix unit handling library for ruby
|
99
|
+
test_files:
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
- spec/units/unit_spec.rb
|