ruby-units 0.3.4 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
- data/{CHANGELOG → CHANGELOG.txt} +3 -1
- data/{LICENSE → LICENSE.txt} +0 -0
- data/Manifest.txt +9 -0
- data/{README → README.txt} +2 -1
- data/Rakefile +21 -0
- data/lib/ruby-units.rb +25 -17
- data/test/test_ruby-units.rb +21 -7
- metadata +22 -12
data/{CHANGELOG → CHANGELOG.txt}
RENAMED
data/{LICENSE → LICENSE.txt}
RENAMED
File without changes
|
data/Manifest.txt
ADDED
data/{README → README.txt}
RENAMED
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'hoe'
|
3
|
+
require './lib/ruby-units.rb'
|
4
|
+
require 'rcov/rcovtask'
|
5
|
+
|
6
|
+
Rcov::RcovTask.new do |t|
|
7
|
+
t.test_files = FileList['test/test*.rb']
|
8
|
+
#t.verbose = true # uncomment to see the executed command
|
9
|
+
end
|
10
|
+
|
11
|
+
Hoe.new('ruby-units', Unit::VERSION) do |p|
|
12
|
+
p.rubyforge_name = 'ruby-units'
|
13
|
+
p.summary = %q{A model that performs unit conversions and unit math}
|
14
|
+
p.email = 'kevin.olbrich+ruby_units@gmail.com'
|
15
|
+
p.url = 'http://rubyforge.org/projects/ruby-units'
|
16
|
+
p.description = "This library handles unit conversions and unit math"
|
17
|
+
p.changes = p.paragraphs_of('CHANGELOG.txt', 0..1).join("\n\n")
|
18
|
+
p.author = 'Kevin Olbrich, Ph.D'
|
19
|
+
end
|
20
|
+
|
21
|
+
# vim: syntax=Ruby
|
data/lib/ruby-units.rb
CHANGED
@@ -2,7 +2,7 @@ require 'mathn'
|
|
2
2
|
require 'rational'
|
3
3
|
require 'date'
|
4
4
|
require 'parsedate'
|
5
|
-
# = Ruby Units 0.3.
|
5
|
+
# = Ruby Units 0.3.5
|
6
6
|
#
|
7
7
|
# Copyright 2006 by Kevin C. Olbrich, Ph.D.
|
8
8
|
#
|
@@ -40,7 +40,7 @@ require 'parsedate'
|
|
40
40
|
class Unit < Numeric
|
41
41
|
require 'units'
|
42
42
|
# pre-generate hashes from unit definitions for performance.
|
43
|
-
VERSION = '0.3.
|
43
|
+
VERSION = '0.3.5'
|
44
44
|
@@USER_DEFINITIONS = {}
|
45
45
|
@@PREFIX_VALUES = {}
|
46
46
|
@@PREFIX_MAP = {}
|
@@ -130,6 +130,7 @@ class Unit < Numeric
|
|
130
130
|
@@PREFIX_REGEX = @@PREFIX_MAP.keys.sort_by {|prefix| prefix.length}.reverse.join('|')
|
131
131
|
@@UNIT_REGEX = @@UNIT_MAP.keys.sort_by {|unit| unit.length}.reverse.join('|')
|
132
132
|
@@UNIT_MATCH_REGEX = /(#{@@PREFIX_REGEX})*?(#{@@UNIT_REGEX})\b/
|
133
|
+
Unit.new(1)
|
133
134
|
end
|
134
135
|
|
135
136
|
|
@@ -176,6 +177,11 @@ class Unit < Numeric
|
|
176
177
|
# 8 lbs 8 oz -- recognized as 8 lbs + 8 ounces
|
177
178
|
#
|
178
179
|
def initialize(*options)
|
180
|
+
@scalar = nil
|
181
|
+
@base_scalar = nil
|
182
|
+
@unit_name = nil
|
183
|
+
@signature = nil
|
184
|
+
@output = nil
|
179
185
|
if options.size == 2
|
180
186
|
begin
|
181
187
|
cached = @@cached_units[options[1]] * options[0]
|
@@ -197,8 +203,6 @@ class Unit < Numeric
|
|
197
203
|
|
198
204
|
|
199
205
|
case options[0]
|
200
|
-
when "": raise ArgumentError, "No Unit Specified"
|
201
|
-
when String: parse(options[0])
|
202
206
|
when Hash:
|
203
207
|
@scalar = options[0][:scalar] || 1
|
204
208
|
@numerator = options[0][:numerator] || UNITY_ARRAY
|
@@ -218,6 +222,8 @@ class Unit < Numeric
|
|
218
222
|
@scalar = options[0].ajd
|
219
223
|
@numerator = ['<day>']
|
220
224
|
@denominator = UNITY_ARRAY
|
225
|
+
when "": raise ArgumentError, "No Unit Specified"
|
226
|
+
when String: parse(options[0])
|
221
227
|
else
|
222
228
|
raise ArgumentError, "Invalid Unit Format"
|
223
229
|
end
|
@@ -232,12 +238,7 @@ class Unit < Numeric
|
|
232
238
|
unless @@cached_units.keys.include?(unary_unit) || (unary_unit =~ /(temp|deg)(C|K|R|F)/) then
|
233
239
|
@@cached_units[unary_unit] = (self.scalar == 1 ? self : unary_unit.unit)
|
234
240
|
end
|
235
|
-
@scalar.freeze
|
236
|
-
@numerator.freeze
|
237
|
-
@denominator.freeze
|
238
|
-
@base_scalar.freeze
|
239
|
-
@signature.freeze
|
240
|
-
@is_base.freeze
|
241
|
+
[@scalar, @numerator, @denominator, @base_scalar, @signature, @is_base].each {|x| x.freeze}
|
241
242
|
self
|
242
243
|
end
|
243
244
|
|
@@ -248,7 +249,12 @@ class Unit < Numeric
|
|
248
249
|
def self.cached
|
249
250
|
return @@cached_units
|
250
251
|
end
|
251
|
-
|
252
|
+
|
253
|
+
def self.clear_cache
|
254
|
+
@@cached_units = {}
|
255
|
+
@@base_unit_cache = {}
|
256
|
+
end
|
257
|
+
|
252
258
|
def self.base_unit_cache
|
253
259
|
return @@base_unit_cache
|
254
260
|
end
|
@@ -325,10 +331,10 @@ class Unit < Numeric
|
|
325
331
|
else
|
326
332
|
case target_units
|
327
333
|
when :ft:
|
328
|
-
inches = self.to("in").scalar
|
334
|
+
inches = self.to("in").scalar.to_int
|
329
335
|
out = "#{(inches / 12).truncate}\'#{(inches % 12).round}\""
|
330
336
|
when :lbs:
|
331
|
-
ounces = self.to("oz").scalar
|
337
|
+
ounces = self.to("oz").scalar.to_int
|
332
338
|
out = "#{(ounces / 16).truncate} lbs, #{(ounces % 16).round} oz"
|
333
339
|
when String
|
334
340
|
begin #first try a standard format string
|
@@ -698,11 +704,10 @@ class Unit < Numeric
|
|
698
704
|
Unit.new(@scalar.floor, @numerator, @denominator)
|
699
705
|
end
|
700
706
|
|
701
|
-
# changes internal scalar to an integer, but retains the units
|
702
707
|
# if unitless, returns an int
|
703
708
|
def to_int
|
704
|
-
return @scalar.to_int if unitless?
|
705
|
-
|
709
|
+
return @scalar.to_int if self.unitless?
|
710
|
+
raise RuntimeError, 'Cannot convert to Integer, use Unit#scalar'
|
706
711
|
end
|
707
712
|
|
708
713
|
# Tries to make a Time object from current unit
|
@@ -711,7 +716,10 @@ class Unit < Numeric
|
|
711
716
|
end
|
712
717
|
alias :time :to_time
|
713
718
|
alias :to_i :to_int
|
714
|
-
|
719
|
+
|
720
|
+
def truncate
|
721
|
+
Unit.new(@scalar.truncate, @numerator, @denominator)
|
722
|
+
end
|
715
723
|
|
716
724
|
def to_datetime
|
717
725
|
DateTime.new(self.to('d').scalar)
|
data/test/test_ruby-units.rb
CHANGED
@@ -50,6 +50,7 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
50
50
|
@april_fools_datetime = DateTime.parse('2006-4-1 12:00')
|
51
51
|
Time.forced_now = @april_fools
|
52
52
|
DateTime.forced_now = @april_fools_datetime
|
53
|
+
#Unit.clear_cache
|
53
54
|
end
|
54
55
|
|
55
56
|
def teardown
|
@@ -361,7 +362,7 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
361
362
|
unit2 = Unit.new("1 in")
|
362
363
|
assert_nothing_raised {
|
363
364
|
unit3 = unit1 - unit2
|
364
|
-
assert_in_delta
|
365
|
+
assert_in_delta(-1.54, unit3.scalar, 0.01)
|
365
366
|
}
|
366
367
|
end
|
367
368
|
|
@@ -421,7 +422,7 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
421
422
|
unit1 = Unit.new("1 mm")
|
422
423
|
unit2 = Unit.new("2 mm")
|
423
424
|
assert_nothing_raised {
|
424
|
-
assert_equal
|
425
|
+
assert_equal(-1, (unit1-unit2).scalar)
|
425
426
|
}
|
426
427
|
end
|
427
428
|
|
@@ -517,9 +518,8 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
517
518
|
end
|
518
519
|
|
519
520
|
def test_to_int
|
520
|
-
|
521
|
-
|
522
|
-
assert_equal unit2, unit1.to_int
|
521
|
+
assert_raises(RuntimeError) {Unit.new("1.1 mm").to_i}
|
522
|
+
assert_nothing_raised {Unit.new(10.5).to_i}
|
523
523
|
end
|
524
524
|
|
525
525
|
def test_truncate
|
@@ -795,6 +795,20 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
795
795
|
assert_equal today,@april_fools
|
796
796
|
last_century = today - '150 years'.unit
|
797
797
|
assert_equal last_century.to_date, '1856-04-01'.to_date
|
798
|
-
|
799
798
|
end
|
800
|
-
|
799
|
+
|
800
|
+
def test_coercion
|
801
|
+
assert_nothing_raised { 1.0 * '1 mm'.unit}
|
802
|
+
assert_nothing_raised { '1 mm'.unit * 1.0}
|
803
|
+
end
|
804
|
+
|
805
|
+
def test_zero
|
806
|
+
assert_nothing_raised { Unit.new(0) }
|
807
|
+
end
|
808
|
+
|
809
|
+
def test_divide_results_in_unitless
|
810
|
+
a = '10 mg/ml'.unit
|
811
|
+
b = '10 mg/ml'.unit
|
812
|
+
assert_equal a/b, 1
|
813
|
+
end
|
814
|
+
end
|
metadata
CHANGED
@@ -3,15 +3,15 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: ruby-units
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.3.
|
7
|
-
date: 2006-
|
6
|
+
version: 0.3.5
|
7
|
+
date: 2006-11-30 00:00:00 -05:00
|
8
8
|
summary: A model that performs unit conversions and unit math
|
9
9
|
require_paths:
|
10
10
|
- lib
|
11
|
-
email: kevin.olbrich+
|
12
|
-
homepage: http://rubyforge.
|
11
|
+
email: kevin.olbrich+ruby_units@gmail.com
|
12
|
+
homepage: http://rubyforge.org/projects/ruby-units
|
13
13
|
rubyforge_project: ruby-units
|
14
|
-
description:
|
14
|
+
description: This library handles unit conversions and unit math
|
15
15
|
autorequire:
|
16
16
|
default_executable:
|
17
17
|
bindir: bin
|
@@ -27,13 +27,15 @@ signing_key:
|
|
27
27
|
cert_chain:
|
28
28
|
post_install_message:
|
29
29
|
authors:
|
30
|
-
- Kevin Olbrich
|
30
|
+
- Kevin Olbrich, Ph.D
|
31
31
|
files:
|
32
|
-
-
|
33
|
-
-
|
34
|
-
-
|
35
|
-
-
|
32
|
+
- CHANGELOG.txt
|
33
|
+
- Manifest.txt
|
34
|
+
- README.txt
|
35
|
+
- LICENSE.txt
|
36
|
+
- Rakefile
|
36
37
|
- lib/ruby-units.rb
|
38
|
+
- lib/ruby_units.rb
|
37
39
|
- lib/units.rb
|
38
40
|
- test/test_ruby-units.rb
|
39
41
|
test_files:
|
@@ -48,5 +50,13 @@ extensions: []
|
|
48
50
|
|
49
51
|
requirements: []
|
50
52
|
|
51
|
-
dependencies:
|
52
|
-
|
53
|
+
dependencies:
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: hoe
|
56
|
+
version_requirement:
|
57
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.1.6
|
62
|
+
version:
|