ruby-si-units 0.0.4 → 0.0.5
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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/si_units/unit.rb +36 -3
- data/lib/si_units/version.rb +2 -1
- data/spec/units/string_spec.rb +3 -0
- data/spec/units/unit_spec.rb +6 -14
- metadata +4 -4
- data/spec/support/units_shared_example.rb +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86b3b3a059205dfbff5012f1a149d91fee0a7b8f
|
4
|
+
data.tar.gz: 6c35073b01b66dff3a23cfd95dcf3af7b88afde2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c2f66e138184bfa1c1d3494c30734f02964a03b63f056c19240e0acaba046080e9323840737c510184a8c7479931f93f7511af748c8172734b46d28438b1f3b
|
7
|
+
data.tar.gz: d9857e86333565c89cb4245f6bf196b066dd46eaa9f8922b4b466a80823659a293e6449a2cc764b5e140c217c8ffe9b7ea4d168a232f43461d7906957cc33e75
|
data/Gemfile.lock
CHANGED
data/lib/si_units/unit.rb
CHANGED
@@ -1,9 +1,15 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
# = SIUnits
|
3
|
+
# A SI prefix unit handling library for ruby
|
4
|
+
#
|
5
|
+
# @author Kenner Kliemann
|
6
|
+
# @see https://github.com/gnomex/ruby-si-units
|
1
7
|
module SIUnits
|
2
8
|
class Unit
|
3
9
|
include Comparable
|
4
10
|
attr_reader :unit_value
|
5
11
|
|
6
|
-
#SI Prefix Units
|
12
|
+
# Definition of SI Prefix Units, with name, aliases and scale
|
7
13
|
UNITS_DEFINITION = {
|
8
14
|
'googol' => [%w{googol}, 1e100],
|
9
15
|
'yebi' => [%w{Yi Yebi yebi}, 2**80],
|
@@ -34,18 +40,27 @@ module SIUnits
|
|
34
40
|
'femto' => [%w{f Femto femto}, 1e-15],
|
35
41
|
'atto' => [%w{a Atto atto}, 1e-18],
|
36
42
|
'zepto' => [%w{z Zepto zepto}, 1e-21],
|
37
|
-
'yocto' => [%w{y Yocto yocto}, 1e-24]
|
43
|
+
'yocto' => [%w{y Yocto yocto}, 1e-24],
|
44
|
+
'zero' => [%w{zero}, 0.0]
|
38
45
|
}
|
39
46
|
|
47
|
+
# Create a unit object
|
48
|
+
# Can be initialized with a number
|
49
|
+
# initialize a unit and parse, recognizing the scale of number
|
50
|
+
# @param [Fixnum|Float]
|
51
|
+
# @return Unit
|
40
52
|
def initialize(unit)
|
41
53
|
@unit_value = unit
|
42
54
|
@unit_kind = parse_unit
|
43
55
|
end
|
44
56
|
|
57
|
+
# Public call to get a unit best representation
|
58
|
+
# @return String
|
45
59
|
def best_scale
|
46
60
|
@best_scale ||= best_value_with_scale
|
47
61
|
end
|
48
62
|
|
63
|
+
#
|
49
64
|
def <=>(comparison)
|
50
65
|
UNITS_DEFINITION.find_index(@unit_kind) <=> UNITS_DEFINITION.find_index(comparison.unit_kind)
|
51
66
|
end
|
@@ -61,17 +76,28 @@ module SIUnits
|
|
61
76
|
|
62
77
|
private
|
63
78
|
|
79
|
+
#Logic to convert the absolute value to a best form of representation
|
64
80
|
def best_value_with_scale
|
65
81
|
aliase, scalar = UNITS_DEFINITION[@unit_kind]
|
66
82
|
[(@unit_value / scalar), aliase.first].join
|
67
83
|
end
|
68
84
|
|
85
|
+
# Logic to convert a value with scale
|
86
|
+
# @return Float
|
69
87
|
def convert_base_prefix_to_value(value, scalar)
|
70
88
|
value * scalar
|
71
89
|
end
|
72
90
|
|
91
|
+
# The parser
|
92
|
+
# => Finds that the number range is contained
|
93
|
+
# @return String with the name of representation
|
94
|
+
# @raise ArgumentError
|
73
95
|
def parse_unit
|
74
96
|
case @unit_value
|
97
|
+
when 0 then return "zero"
|
98
|
+
when 1e-24..1e-21 then return "yocto"
|
99
|
+
when 1e-21..1e-18 then return "atto"
|
100
|
+
when 1e-18..1e-15 then return "femto"
|
75
101
|
when 1e-15..1e-12 then return "pico"
|
76
102
|
when 1e-12..1e-9 then return "nano"
|
77
103
|
when 1e-9..1e-6 then return "micro"
|
@@ -84,10 +110,17 @@ module SIUnits
|
|
84
110
|
when 1e3..1e6 then return "kilo"
|
85
111
|
when 1e6..1e9 then return "mega"
|
86
112
|
when 1e9..1e12 then return "giga"
|
87
|
-
|
113
|
+
when 1e12..1e15 then return "tera"
|
114
|
+
when 1e15..1e18 then return "peta"
|
115
|
+
when 1e18..1e21 then return "exa"
|
116
|
+
when 1e21..1e24 then return "zetta"
|
117
|
+
else raise ArgumentError, "Unit out of range"
|
88
118
|
end
|
89
119
|
end
|
90
120
|
|
121
|
+
# Search on DEFINITIONS if the kind is a element
|
122
|
+
# @return Fixnum with scale of prefix
|
123
|
+
# @raise ArgumentError
|
91
124
|
def prefix_is_defined?(kind)
|
92
125
|
UNITS_DEFINITION.each { |key, value|
|
93
126
|
return value.last if value[0].include?(kind)
|
data/lib/si_units/version.rb
CHANGED
data/spec/units/unit_spec.rb
CHANGED
@@ -105,23 +105,15 @@ describe "it's a deci " do
|
|
105
105
|
end
|
106
106
|
|
107
107
|
describe "it's a const " do
|
108
|
-
xit "max limit" do
|
109
|
-
unit = SIUnits::Unit.new((1e-1) + 0.1)
|
110
|
-
expect(unit.best_scale).to match(/const/)
|
111
|
-
end
|
112
|
-
|
113
|
-
xit "zero" do
|
114
|
-
|
115
|
-
end
|
116
108
|
|
117
|
-
|
118
|
-
unit = SIUnits::Unit.new(
|
119
|
-
expect(unit.best_scale).to match(/
|
109
|
+
it "zero" do
|
110
|
+
unit = SIUnits::Unit.new(0)
|
111
|
+
expect(unit.best_scale).to match(/zero/)
|
120
112
|
end
|
121
113
|
|
122
|
-
|
123
|
-
unit = SIUnits::Unit.new(
|
124
|
-
expect(unit.best_scale).to_not match(/
|
114
|
+
it "not a zero" do
|
115
|
+
unit = SIUnits::Unit.new(0.1)
|
116
|
+
expect(unit.best_scale).to_not match(/zero/)
|
125
117
|
end
|
126
118
|
end
|
127
119
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-si-units
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kenner Kliemann
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-09-
|
11
|
+
date: 2013-09-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -100,7 +100,7 @@ files:
|
|
100
100
|
- lib/unit.rb
|
101
101
|
- ruby-si-unit.gemspec
|
102
102
|
- spec/spec_helper.rb
|
103
|
-
- spec/
|
103
|
+
- spec/units/string_spec.rb
|
104
104
|
- spec/units/unit_spec.rb
|
105
105
|
homepage: https://github.com/gnomex/ruby-si-units
|
106
106
|
licenses:
|
@@ -128,5 +128,5 @@ specification_version: 4
|
|
128
128
|
summary: A SI prefix unit handling library for ruby
|
129
129
|
test_files:
|
130
130
|
- spec/spec_helper.rb
|
131
|
-
- spec/
|
131
|
+
- spec/units/string_spec.rb
|
132
132
|
- spec/units/unit_spec.rb
|
@@ -1,17 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
require 'spec_helper'
|
3
|
-
|
4
|
-
shared_examples_for "is unit of" do |unit, expected_prefix|
|
5
|
-
|
6
|
-
it "is max limit on range" do
|
7
|
-
expect(:unit.to_s).to eql(expected_prefix)
|
8
|
-
end
|
9
|
-
|
10
|
-
it "is min limit on range" do
|
11
|
-
expect(:unit.to_s).to eql(expected_prefix)
|
12
|
-
end
|
13
|
-
|
14
|
-
it "out of unit range" do
|
15
|
-
expect(:unit.to_s).to_not eql(expected_prefix)
|
16
|
-
end
|
17
|
-
end
|