ruby-si-units 0.0.7 → 0.0.8

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: efa72132c423b8eeebd9edfb7be550c997713b49
4
- data.tar.gz: 7efa33641c700b3a2af0bc5d2665e30617e19274
3
+ metadata.gz: 4dd439cebc308b9ac78f93d5032f9b35451b04f5
4
+ data.tar.gz: 40936a1ce7817bb55c05103d17f533b568bc2d40
5
5
  SHA512:
6
- metadata.gz: af000424f61537a2c24d6bb758ba2f7de023b4e59aa40ff16e95b4b642c377f9de52c94db2271a669e2235ad2e86eb3b929a44724313bfcef9d945f04d7d7ea6
7
- data.tar.gz: e655905b2637defa6103898bbcc7c6773bc6ef09fd400274c03001559105c43e91674c1eb813cf6ca844f1c2ad6424ace13e82ea7013bd4a16983ade4cdabbaf
6
+ metadata.gz: b5872b30732861c26fc4f149256a64071d0a91ca4fee2eadfaf7a5b516c5b3e091aaecdf6dd863db1c59031662985910acf157213a401db0ccad384b71188047
7
+ data.tar.gz: b77c8c05ec713741d5ff8b55f4eae9d3366c7f35a4c7fb011707cc6b548c6f3e6ae9fda8de35f4af5629183d568224bdff4f3fbcbc079648a28c33cbb4e69d5d
@@ -2,12 +2,7 @@ class String
2
2
  # make a string into a unit
3
3
  # @return (see RubyUnits::Unit#initialize)
4
4
  def to_unit
5
- unit_reduced, prefix = *split_value(self)
6
- SIUnits::Unit.new(unit_reduced).convert_to(prefix)
7
- end
8
-
9
- def split_value(value)
10
- value.scan(/(\d+\.?\d*)(\w+)/).flatten
5
+ SIUnits::Unit.new(self)
11
6
  end
12
7
  end
13
8
 
data/lib/si_units/unit.rb CHANGED
@@ -8,7 +8,7 @@ module SIUnits
8
8
  class Unit
9
9
  include Comparable
10
10
 
11
- attr_reader :unit_value, :unit_kind
11
+ attr_reader :value, :kind
12
12
 
13
13
  # Definition of SI Prefix Units, with name, aliases and scale
14
14
  UNITS_DEFINITION = {
@@ -44,7 +44,7 @@ module SIUnits
44
44
  'yocto' => [%w{y Yocto yocto}, 1e-24],
45
45
  'zero' => [%w{zero}, 0.0]
46
46
  }
47
- UNIT_REGEX = /(\d+\.?\d*)(\w+)/
47
+ UNIT_REGEX = /\d+[\.?]\d+|\w+/
48
48
 
49
49
  # Create a new Unit object.
50
50
  # @return [Unit]
@@ -58,14 +58,14 @@ module SIUnits
58
58
  case options[0]
59
59
  when Numeric
60
60
  # Conversion use a scale
61
- @unit_value = options.first
62
- @unit_kind = parse_unit
61
+ @value = options.first
62
+ @kind = parse_unit
63
63
 
64
64
  when String
65
65
  value, prefix = *split_value(options[0])
66
- @unit_kind = who_is_my_prefix?(prefix)
66
+ @kind = who_is_my_prefix?(prefix)
67
67
  # Value is absolute, needs convert to scale of prefix
68
- @unit_value = value.to_f * unit_scale
68
+ @value = value.to_f * scale
69
69
 
70
70
  else
71
71
  raise ArgumentError, "Invalid Unit Format"
@@ -81,11 +81,11 @@ module SIUnits
81
81
  # Comparable units
82
82
  # => Compare with scale of kinds
83
83
  def <=>(comparison)
84
- UNITS_DEFINITION[unit_kind].last <=> UNITS_DEFINITION[comparison.unit_kind].last
84
+ UNITS_DEFINITION[kind].last <=> UNITS_DEFINITION[comparison.kind].last
85
85
  end
86
86
 
87
87
  def ==(comparison)
88
- unit_kind == comparison.unit_kind
88
+ kind == comparison.kind
89
89
  end
90
90
 
91
91
  # convert to a specified unit string or to the same unit as another Unit
@@ -106,7 +106,7 @@ module SIUnits
106
106
  alias :>> :convert_to
107
107
 
108
108
  def to_s
109
- [best_scale, unit_kind].join(' ')
109
+ [best_scale, kind].join(' ')
110
110
  end
111
111
 
112
112
  private
@@ -114,7 +114,7 @@ module SIUnits
114
114
  # Logic to convert the current unit value to a best form of representation
115
115
  # == Just remove the "e base" from value
116
116
  def best_value_with_scale
117
- (unit_value / unit_scale).round(4)
117
+ (value / scale).round(4)
118
118
  end
119
119
 
120
120
  # The parser
@@ -122,40 +122,65 @@ module SIUnits
122
122
  # @return String with the name of representation
123
123
  # @raise ArgumentError
124
124
  def parse_unit
125
- case @unit_value
126
- when 0 then return "zero"
127
- when 1e-24..1e-21 then return "yocto"
128
- when 1e-21..1e-18 then return "atto"
129
- when 1e-18..1e-15 then return "femto"
130
- when 1e-15..1e-12 then return "pico"
131
- when 1e-12..1e-9 then return "nano"
132
- when 1e-9..1e-6 then return "micro"
133
- when 1e-6..1e-3 then return "milli"
134
- when 1e-3..1e-2 then return "centi"
135
- when 1e-2..1e-1 then return "deci"
136
- when 1e-1..1e1 then return "1"
137
- when 1e1..1e2 then return "deca"
138
- when 1e2..1e3 then return "hecto"
139
- when 1e3..1e6 then return "kilo"
140
- when 1e6..1e9 then return "mega"
141
- when 1e9..1e12 then return "giga"
142
- when 1e12..1e15 then return "tera"
143
- when 1e15..1e18 then return "peta"
144
- when 1e18..1e21 then return "exa"
145
- when 1e21..1e24 then return "zetta"
146
- else raise ArgumentError, "Unit out of range"
125
+ case @value
126
+ when 0
127
+ return "zero"
128
+ when 1e-24..1e-21
129
+ return "yocto"
130
+ when 1e-21..1e-18
131
+ return "atto"
132
+ when 1e-18..1e-15
133
+ return "femto"
134
+ when 1e-15..1e-12
135
+ return "pico"
136
+ when 1e-12..1e-9
137
+ return "nano"
138
+ when 1e-9..1e-6
139
+ return "micro"
140
+ when 1e-6..1e-3
141
+ return "milli"
142
+ when 1e-3..1e-2
143
+ return "centi"
144
+ when 1e-2..1e-1
145
+ return "deci"
146
+ when 1e-1..1e1
147
+ return "1"
148
+ when 1e1..1e2
149
+ return "deca"
150
+ when 1e2..1e3
151
+ return "hecto"
152
+ when 1e3..1e6
153
+ return "kilo"
154
+ when 1e6..1e9
155
+ return "mega"
156
+ when 1e9..1e12
157
+ return "giga"
158
+ when 1e12..1e15
159
+ return "tera"
160
+ when 1e15..1e18
161
+ return "peta"
162
+ when 1e18..1e21
163
+ return "exa"
164
+ when 1e21..1e24
165
+ return "zetta"
166
+ else
167
+ raise ArgumentError, "Unit out of range"
147
168
  end
148
169
  end
149
170
 
150
171
  def who_is_my_prefix?(prefix)
151
- UNITS_DEFINITION.each { |key, value|
172
+
173
+ prefix unless UNITS_DEFINITION.has_key?(prefix)
174
+
175
+ UNITS_DEFINITION.each do |key, value|
152
176
  return key if value[0].include?(prefix)
153
- }
177
+ end
178
+
154
179
  raise ArgumentError, "Unknown prefix"
155
180
  end
156
181
 
157
- def unit_scale
158
- @unit_scale ||= UNITS_DEFINITION[unit_kind].last
182
+ def scale
183
+ @scale ||= UNITS_DEFINITION[kind].last
159
184
  end
160
185
 
161
186
  def split_value(value)
@@ -1,4 +1,4 @@
1
1
  module SIUnits
2
- # Project Version
3
- VERSION = "0.0.7"
2
+ #GEM version
3
+ VERSION = "0.0.8"
4
4
  end
data/lib/unit.rb CHANGED
@@ -1,3 +1,3 @@
1
- require 'si_units/string'
2
- require 'si_units/unit'
3
- require 'si_units/version'
1
+ require_relative 'si_units/string'
2
+ require_relative 'si_units/unit'
3
+ require_relative 'si_units/version'
@@ -4,47 +4,13 @@ require "unit"
4
4
 
5
5
  describe SIUnits::Unit do
6
6
 
7
- context "it's a pico " do
8
-
9
- end
10
-
11
- context "it's a nano " do
12
-
13
- end
14
-
15
- context "it's a micro " do
16
-
17
- end
18
-
19
- context "it's a milli " do
20
-
21
- end
22
-
23
- context "it's a const " do
24
-
25
- end
26
-
27
- context "it's a kilo " do
28
- end
29
-
30
- context "it's a mega " do
31
-
32
- end
33
-
34
- context "it's a giga " do
35
-
36
- end
37
-
38
- context "Its comparable" do
39
- it "Compare with"
40
- end
7
+ end
41
8
 
42
- context "Convert to" do
43
- it "Convert to"
44
- end
45
9
 
46
- context "Convert string to unit" do
47
- it "string to unit"
48
- end
10
+ describe "Convert to" do
11
+ it "Convert to"
12
+ end
49
13
 
14
+ describe "Convert string to unit" do
15
+ it "string to unit"
50
16
  end
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.7
4
+ version: 0.0.8
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-26 00:00:00.000000000 Z
11
+ date: 2013-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler