quantify 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +1 -0
- data/Gemfile +14 -0
- data/README +10 -10
- data/Rakefile +65 -0
- data/VERSION +1 -0
- data/lib/quantify/config.rb +10 -14
- data/lib/quantify/dimensions.rb +1 -1
- data/lib/quantify/quantify.rb +2 -35
- data/lib/quantify/unit/base_unit.rb +29 -25
- data/lib/quantify/unit/compound_base_unit.rb +21 -22
- data/lib/quantify/unit/compound_base_unit_list.rb +260 -0
- data/lib/quantify/unit/compound_unit.rb +78 -218
- data/lib/quantify/unit/unit.rb +119 -4
- data/lib/quantify.rb +1 -0
- data/quantify.gemspec +90 -0
- data/spec/compound_unit_spec.rb +124 -13
- data/spec/quantify_spec.rb +0 -14
- data/spec/quantity_spec.rb +6 -6
- data/spec/unit_spec.rb +57 -39
- metadata +130 -32
data/lib/quantify/unit/unit.rb
CHANGED
@@ -20,17 +20,132 @@ module Quantify
|
|
20
20
|
# prefixes) at any time and either used in place or loaded into the known
|
21
21
|
# system.
|
22
22
|
|
23
|
-
# Make the @units instance array readable
|
24
23
|
class << self
|
25
24
|
attr_reader :units
|
25
|
+
#attr_reader :symbol_denominator_delimiter, :symbol_unit_delimiter
|
26
|
+
#attr_reader :use_symbol_parentheses, :use_symbol_denominator_syntax
|
26
27
|
end
|
27
28
|
|
29
|
+
# Instance variable containing system of known units
|
30
|
+
@units = []
|
31
|
+
|
32
|
+
# Default configuration for unit symbols
|
33
|
+
#@use_symbol_denominator_syntax = true
|
34
|
+
#@use_symbol_parentheses = false
|
35
|
+
#@symbol_denominator_delimiter = "/"
|
36
|
+
#@symbol_unit_delimiter = " "
|
37
|
+
|
28
38
|
def self.configure(&block)
|
29
39
|
self.class_eval(&block) if block
|
30
40
|
end
|
31
41
|
|
32
|
-
#
|
33
|
-
|
42
|
+
# Set the default string which is used to delimit numerator and denominator
|
43
|
+
# strings in compound unit symbol representations. Defaults to "/".
|
44
|
+
#
|
45
|
+
def self.symbol_denominator_delimiter=(string)
|
46
|
+
@symbol_denominator_delimiter = string
|
47
|
+
refresh_all_unit_attributes!
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.symbol_denominator_delimiter
|
51
|
+
@symbol_denominator_delimiter ||= "/"
|
52
|
+
end
|
53
|
+
|
54
|
+
# Set the default string which is used to delimit unit symbol strings in
|
55
|
+
# compound unit symbol representations. Defaults to " ".
|
56
|
+
#
|
57
|
+
def self.symbol_unit_delimiter=(string)
|
58
|
+
@symbol_unit_delimiter = string
|
59
|
+
refresh_all_unit_attributes!
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.symbol_unit_delimiter
|
63
|
+
@symbol_unit_delimiter ||= " "
|
64
|
+
end
|
65
|
+
|
66
|
+
# Specify whether parentheses should be used to group multiple units in
|
67
|
+
# compound unit symbol representations. Set either <tt>true</tt> (e.g.
|
68
|
+
# "kg/(t km)") or <tt>false</tt> (e.g. "kg/t km"). Defaults to <tt>false</tt>
|
69
|
+
#
|
70
|
+
def self.use_symbol_parentheses=(true_or_false)
|
71
|
+
@use_symbol_parentheses = true_or_false
|
72
|
+
refresh_all_unit_attributes!
|
73
|
+
end
|
74
|
+
|
75
|
+
# Shorthand bang! method for configuring parentheses in compound unit symbol
|
76
|
+
# representations
|
77
|
+
#
|
78
|
+
def self.use_symbol_parentheses!
|
79
|
+
@use_symbol_parentheses = true
|
80
|
+
end
|
81
|
+
|
82
|
+
# Returns <tt>true</tt> if parentheses are configured for use within compound
|
83
|
+
# unit symbol representations. Otherwise returns <tt>false</tt>.
|
84
|
+
#
|
85
|
+
def self.use_symbol_parentheses?
|
86
|
+
@use_symbol_parentheses.nil? ? false : @use_symbol_parentheses
|
87
|
+
end
|
88
|
+
|
89
|
+
# Shorthand bang! method for configuring denominator structures in compound
|
90
|
+
# unit symbol representations (e.g. "kg/t km")
|
91
|
+
#
|
92
|
+
def self.use_symbol_denominator_syntax!
|
93
|
+
self.use_symbol_denominator_syntax = true
|
94
|
+
end
|
95
|
+
|
96
|
+
# Shorthand bang! method for configuring index-only structures in compound
|
97
|
+
# unit symbol representations (e.g. "kg t^-1 km^-1")
|
98
|
+
#
|
99
|
+
def self.use_symbol_indices_only!
|
100
|
+
self.use_symbol_denominator_syntax = false
|
101
|
+
end
|
102
|
+
|
103
|
+
# Specify whether denominator structures should be used in compound unit
|
104
|
+
# symbol representations. Set either <tt>true</tt> (e.g. "kg/t km") or
|
105
|
+
# <tt>false</tt> (e.g. "kg t^-1 km^-1"). Defaults to <tt>true</tt>
|
106
|
+
#
|
107
|
+
def self.use_symbol_denominator_syntax=(true_or_false)
|
108
|
+
@use_symbol_denominator_syntax = true_or_false
|
109
|
+
refresh_all_unit_attributes!
|
110
|
+
end
|
111
|
+
|
112
|
+
# Returns <tt>true</tt> if denominator structures are configured for use
|
113
|
+
# within compound unit symbol representations. Otherwise returns
|
114
|
+
# <tt>false</tt>.
|
115
|
+
#
|
116
|
+
def self.use_symbol_denominator_syntax?
|
117
|
+
@use_symbol_denominator_syntax.nil? ? true : @use_symbol_denominator_syntax
|
118
|
+
end
|
119
|
+
|
120
|
+
# Check whether superscript characters are turned on.
|
121
|
+
def self.use_superscript_characters?
|
122
|
+
@use_superscript_characters.nil? ? true : @use_superscript_characters
|
123
|
+
end
|
124
|
+
|
125
|
+
# Shorthand method for ::use_superscript_characters=true
|
126
|
+
def self.use_superscript_characters!
|
127
|
+
self.use_superscript_characters = true
|
128
|
+
end
|
129
|
+
|
130
|
+
# Declare whether superscript characters should be used for unit names, symbols
|
131
|
+
# and labels - i.e. "²" and "³" rather than "^2" and "^3". Set to either true or
|
132
|
+
# false. If not set, superscript characters are used by default.
|
133
|
+
#
|
134
|
+
def self.use_superscript_characters=(true_or_false)
|
135
|
+
raise Exceptions::InvalidArgumentError,
|
136
|
+
"Argument must be true or false" unless true_or_false == true || true_or_false == false
|
137
|
+
@use_superscript_characters = true_or_false
|
138
|
+
refresh_all_unit_attributes!
|
139
|
+
end
|
140
|
+
|
141
|
+
# Switch all unit identifiers (name, symbol, label) to use the currently
|
142
|
+
# configured system for superscripts.
|
143
|
+
#
|
144
|
+
def self.refresh_all_unit_attributes!
|
145
|
+
Unit.units.replace(
|
146
|
+
Unit.units.map { |unit| unit.refresh_attributes; unit }
|
147
|
+
)
|
148
|
+
end
|
34
149
|
|
35
150
|
# Load a new unit into they system of known units
|
36
151
|
def self.load(unit)
|
@@ -168,7 +283,7 @@ module Quantify
|
|
168
283
|
when :name then string_or_symbol.remove_underscores.singularize.downcase
|
169
284
|
else string_or_symbol.to_s
|
170
285
|
end
|
171
|
-
|
286
|
+
Unit.use_superscript_characters? ?
|
172
287
|
string_or_symbol.with_superscript_characters : string_or_symbol.without_superscript_characters
|
173
288
|
end
|
174
289
|
|
data/lib/quantify.rb
CHANGED
@@ -22,6 +22,7 @@ require 'quantify/unit/unit'
|
|
22
22
|
require 'quantify/unit/base_unit'
|
23
23
|
require 'quantify/unit/si_unit'
|
24
24
|
require 'quantify/unit/non_si_unit'
|
25
|
+
require 'quantify/unit/compound_base_unit_list'
|
25
26
|
require 'quantify/unit/compound_base_unit'
|
26
27
|
require 'quantify/unit/compound_unit'
|
27
28
|
require 'quantify/quantity'
|
data/quantify.gemspec
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{quantify}
|
8
|
+
s.version = "1.2.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Andrew Berkeley"]
|
12
|
+
s.date = %q{2011-08-04}
|
13
|
+
s.description = %q{A gem to support physical quantities and unit conversions}
|
14
|
+
s.email = %q{andrew.berkeley.is@googlemail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".rvmrc",
|
20
|
+
"COPYING",
|
21
|
+
"Gemfile",
|
22
|
+
"README",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"lib/quantify.rb",
|
26
|
+
"lib/quantify/config.rb",
|
27
|
+
"lib/quantify/core_extensions/numeric.rb",
|
28
|
+
"lib/quantify/core_extensions/string.rb",
|
29
|
+
"lib/quantify/core_extensions/symbol.rb",
|
30
|
+
"lib/quantify/dimensions.rb",
|
31
|
+
"lib/quantify/exception.rb",
|
32
|
+
"lib/quantify/inflections.rb",
|
33
|
+
"lib/quantify/quantify.rb",
|
34
|
+
"lib/quantify/quantity.rb",
|
35
|
+
"lib/quantify/unit/base_unit.rb",
|
36
|
+
"lib/quantify/unit/compound_base_unit.rb",
|
37
|
+
"lib/quantify/unit/compound_base_unit_list.rb",
|
38
|
+
"lib/quantify/unit/compound_unit.rb",
|
39
|
+
"lib/quantify/unit/non_si_unit.rb",
|
40
|
+
"lib/quantify/unit/prefix/base_prefix.rb",
|
41
|
+
"lib/quantify/unit/prefix/non_si_prefix.rb",
|
42
|
+
"lib/quantify/unit/prefix/prefix.rb",
|
43
|
+
"lib/quantify/unit/prefix/si_prefix.rb",
|
44
|
+
"lib/quantify/unit/si_unit.rb",
|
45
|
+
"lib/quantify/unit/unit.rb",
|
46
|
+
"quantify.gemspec",
|
47
|
+
"spec/compound_unit_spec.rb",
|
48
|
+
"spec/dimension_spec.rb",
|
49
|
+
"spec/quantify_spec.rb",
|
50
|
+
"spec/quantity_spec.rb",
|
51
|
+
"spec/string_spec.rb",
|
52
|
+
"spec/unit_spec.rb"
|
53
|
+
]
|
54
|
+
s.homepage = %q{https://github.com/spatchcock/quantify}
|
55
|
+
s.licenses = ["MIT"]
|
56
|
+
s.require_paths = ["lib"]
|
57
|
+
s.rubygems_version = %q{1.6.0}
|
58
|
+
s.summary = %q{Support for handling physical quantities, unit conversions, etc}
|
59
|
+
|
60
|
+
if s.respond_to? :specification_version then
|
61
|
+
s.specification_version = 3
|
62
|
+
|
63
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
64
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
|
65
|
+
s.add_runtime_dependency(%q<i18n>, [">= 0"])
|
66
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
67
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
68
|
+
s.add_development_dependency(%q<rspec>, ["= 1.3.0"])
|
69
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
70
|
+
s.add_development_dependency(%q<rspec_spinner>, ["= 1.1.3"])
|
71
|
+
else
|
72
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
73
|
+
s.add_dependency(%q<i18n>, [">= 0"])
|
74
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
75
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
76
|
+
s.add_dependency(%q<rspec>, ["= 1.3.0"])
|
77
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
78
|
+
s.add_dependency(%q<rspec_spinner>, ["= 1.1.3"])
|
79
|
+
end
|
80
|
+
else
|
81
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
82
|
+
s.add_dependency(%q<i18n>, [">= 0"])
|
83
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
84
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
85
|
+
s.add_dependency(%q<rspec>, ["= 1.3.0"])
|
86
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
87
|
+
s.add_dependency(%q<rspec_spinner>, ["= 1.1.3"])
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
data/spec/compound_unit_spec.rb
CHANGED
@@ -3,6 +3,79 @@ include Quantify
|
|
3
3
|
|
4
4
|
describe Unit do
|
5
5
|
|
6
|
+
describe "compound unit symbols" do
|
7
|
+
|
8
|
+
after :all do
|
9
|
+
Unit.use_symbol_denominator_syntax!
|
10
|
+
Unit.use_symbol_parentheses = false
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should use denominator syntax by default" do
|
14
|
+
unit = Unit.m*Unit.m*Unit.s*Unit.kg/(Unit.m*Unit.m*Unit.m*Unit.s)
|
15
|
+
unit.symbol.should == "m² s kg/m³ s"
|
16
|
+
Unit.metre_per_second.symbol.should eql "m/s"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should use indices only with bang! method" do
|
20
|
+
Unit.use_symbol_indices_only!
|
21
|
+
unit = Unit.m*Unit.m*Unit.s*Unit.kg/(Unit.m*Unit.m*Unit.m*Unit.s)
|
22
|
+
unit.symbol.should == "m² s kg s^-1 m^-3"
|
23
|
+
Unit.metre_per_second.symbol.should eql "m s^-1"
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should use denominator syntax with bang! method" do
|
27
|
+
Unit.use_symbol_denominator_syntax!
|
28
|
+
unit = Unit.m*Unit.m*Unit.s*Unit.kg/(Unit.m*Unit.m*Unit.m*Unit.s)
|
29
|
+
unit.symbol.should == "m² s kg/m³ s"
|
30
|
+
Unit.metre_per_second.symbol.should eql "m/s"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should use indices only with setter method" do
|
34
|
+
Unit.use_symbol_denominator_syntax = false
|
35
|
+
unit = Unit.m*Unit.m*Unit.s*Unit.kg/(Unit.m*Unit.m*Unit.m*Unit.s)
|
36
|
+
unit.symbol.should == "m² s kg s^-1 m^-3"
|
37
|
+
Unit.metre_per_second.symbol.should eql "m s^-1"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should use denominator syntax with setter method" do
|
41
|
+
Unit.use_symbol_denominator_syntax = true
|
42
|
+
unit = Unit.m*Unit.m*Unit.s*Unit.kg/(Unit.m*Unit.m*Unit.m*Unit.s)
|
43
|
+
unit.symbol.should == "m² s kg/m³ s"
|
44
|
+
Unit.metre_per_second.symbol.should eql "m/s"
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should use parentheses with setter method" do
|
48
|
+
Unit.use_symbol_parentheses = true
|
49
|
+
unit = Unit.m*Unit.m*Unit.s*Unit.kg/(Unit.m*Unit.m*Unit.m*Unit.s)
|
50
|
+
unit.symbol.should == "(m² s kg)/(m³ s)"
|
51
|
+
unit = Unit.m*Unit.m/(Unit.m*Unit.m*Unit.m*Unit.s)
|
52
|
+
unit.symbol.should == "m²/(m³ s)"
|
53
|
+
unit = Unit.m*Unit.m*Unit.s*Unit.kg/(Unit.m*Unit.m*Unit.m)
|
54
|
+
unit.symbol.should == "(m² s kg)/m³"
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should not use parentheses with setter method" do
|
58
|
+
Unit.use_symbol_parentheses = false
|
59
|
+
unit = Unit.m*Unit.m*Unit.s*Unit.kg/(Unit.m*Unit.m*Unit.m*Unit.s)
|
60
|
+
unit.symbol.should == "m² s kg/m³ s"
|
61
|
+
unit = Unit.m*Unit.m/(Unit.m*Unit.m*Unit.m*Unit.s)
|
62
|
+
unit.symbol.should == "m²/m³ s"
|
63
|
+
unit = Unit.m*Unit.m*Unit.s*Unit.kg/(Unit.m*Unit.m*Unit.m)
|
64
|
+
unit.symbol.should == "m² s kg/m³"
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should use parentheses with bang! method" do
|
68
|
+
Unit.use_symbol_parentheses!
|
69
|
+
unit = Unit.m*Unit.m*Unit.s*Unit.kg/(Unit.m*Unit.m*Unit.m*Unit.s)
|
70
|
+
unit.symbol.should == "(m² s kg)/(m³ s)"
|
71
|
+
unit = Unit.m*Unit.m/(Unit.m*Unit.m*Unit.m*Unit.s)
|
72
|
+
unit.symbol.should == "m²/(m³ s)"
|
73
|
+
unit = Unit.m*Unit.m*Unit.s*Unit.kg/(Unit.m*Unit.m*Unit.m)
|
74
|
+
unit.symbol.should == "(m² s kg)/m³"
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
6
79
|
describe "compound unit naming algorithms" do
|
7
80
|
|
8
81
|
it "should return pluralised unit name" do
|
@@ -56,7 +129,7 @@ describe Unit do
|
|
56
129
|
describe "specific compound unit operations" do
|
57
130
|
|
58
131
|
it "should find equivalent unit for compound unit" do
|
59
|
-
(Unit.m*Unit.m).equivalent_known_unit.name.should == '
|
132
|
+
(Unit.kg*Unit.m*Unit.m/Unit.s/Unit.s).equivalent_known_unit.name.should == 'joule'
|
60
133
|
(Unit.km*Unit.lb).equivalent_known_unit.should == nil
|
61
134
|
end
|
62
135
|
|
@@ -68,28 +141,28 @@ describe Unit do
|
|
68
141
|
|
69
142
|
it "should consolidate across all base units" do
|
70
143
|
unit = Unit.m*Unit.m*Unit.s*Unit.kg/(Unit.m*Unit.m*Unit.m*Unit.s)
|
71
|
-
unit.symbol.should == "m² s kg s
|
144
|
+
unit.symbol.should == "m² s kg/m³ s"
|
72
145
|
unit.base_units.size.should == 5
|
73
146
|
unit.consolidate_base_units!
|
74
|
-
unit.symbol.should == "kg
|
147
|
+
unit.symbol.should == "kg/m"
|
75
148
|
unit.base_units.size.should == 2
|
76
149
|
end
|
77
150
|
|
78
151
|
it "should cancel base units with one argument which is a symbol" do
|
79
152
|
unit = Unit.m*Unit.m*Unit.s*Unit.kg/(Unit.m*Unit.m*Unit.m*Unit.s)
|
80
|
-
unit.symbol.should == "m² s kg s
|
153
|
+
unit.symbol.should == "m² s kg/m³ s"
|
81
154
|
unit.base_units.size.should == 5
|
82
155
|
unit.cancel_base_units! :m
|
83
|
-
unit.symbol.should == "s kg
|
156
|
+
unit.symbol.should == "s kg/m s"
|
84
157
|
unit.base_units.size.should == 4
|
85
158
|
end
|
86
159
|
|
87
160
|
it "should cancel base units with multiple arguments including unit objects and strings" do
|
88
161
|
unit = Unit.m*Unit.m*Unit.s*Unit.kg/(Unit.m*Unit.m*Unit.m*Unit.s)
|
89
|
-
unit.symbol.should == "m² s kg s
|
162
|
+
unit.symbol.should == "m² s kg/m³ s"
|
90
163
|
unit.base_units.size.should == 5
|
91
164
|
unit.cancel_base_units! Unit.m, 's'
|
92
|
-
unit.symbol.should == "kg
|
165
|
+
unit.symbol.should == "kg/m"
|
93
166
|
unit.base_units.size.should == 2
|
94
167
|
end
|
95
168
|
|
@@ -108,7 +181,7 @@ describe Unit do
|
|
108
181
|
base1 = Unit::CompoundBaseUnit.new Unit.h, -1
|
109
182
|
base2 = Unit::CompoundBaseUnit.new Unit.mi
|
110
183
|
compound_unit = Unit::Compound.new base1, base2
|
111
|
-
compound_unit.symbol.should == "mi
|
184
|
+
compound_unit.symbol.should == "mi/h"
|
112
185
|
end
|
113
186
|
|
114
187
|
it "should initialize compound unit with multiple individual units" do
|
@@ -136,7 +209,7 @@ describe Unit do
|
|
136
209
|
base1 = [Unit.h, -1]
|
137
210
|
base2 = [Unit.m, 2]
|
138
211
|
compound_unit = Unit::Compound.new base1, base2
|
139
|
-
compound_unit.symbol.should == "m
|
212
|
+
compound_unit.symbol.should == "m²/h"
|
140
213
|
end
|
141
214
|
|
142
215
|
it "should initialize compound unit with variable arguments" do
|
@@ -189,17 +262,55 @@ describe Unit do
|
|
189
262
|
unit = Unit.yard*Unit.foot
|
190
263
|
unit.rationalize_base_units!.label.should eql 'yd²'
|
191
264
|
unit = Unit.metre*Unit.centimetre/Unit.inch
|
192
|
-
unit.rationalize_base_units
|
265
|
+
unit.rationalize_base_units!.label.should eql 'm²/m'
|
193
266
|
unit.consolidate_base_units!.label.should eql 'm'
|
194
267
|
end
|
195
268
|
|
196
269
|
it "should rationalize base units with specified unit" do
|
197
270
|
unit = Unit.yard*Unit.foot
|
198
|
-
unit.
|
271
|
+
unit.rationalize_numerator_and_denominator_units!(:yd).label.should eql 'yd²'
|
199
272
|
unit = Unit.metre*Unit.centimetre/Unit.inch
|
200
|
-
unit.rationalize_base_units!(:
|
201
|
-
unit.consolidate_base_units!.label.should eql '
|
273
|
+
unit.rationalize_base_units!(:cm).label.should eql 'cm²/cm'
|
274
|
+
unit.consolidate_base_units!.label.should eql 'cm'
|
275
|
+
end
|
276
|
+
|
277
|
+
it "should rationalize only numerator and denominator base units" do
|
278
|
+
unit = Unit.yard*Unit.foot
|
279
|
+
unit.rationalize_numerator_and_denominator_units!.label.should eql 'yd²'
|
280
|
+
unit = Unit.metre*Unit.centimetre/Unit.inch
|
281
|
+
unit.rationalize_numerator_and_denominator_units!.label.should eql 'm²/in'
|
282
|
+
unit.consolidate_base_units!.label.should eql 'm²/in'
|
202
283
|
end
|
203
284
|
|
204
285
|
end
|
286
|
+
|
287
|
+
describe "adding prefixes" do
|
288
|
+
|
289
|
+
it "should have no valid prefixes with multiple base units" do
|
290
|
+
(Unit.kg*Unit.m*Unit.m/Unit.s/Unit.s).valid_prefixes.should be_empty
|
291
|
+
end
|
292
|
+
|
293
|
+
it "should return SI prefixes with single SI unit" do
|
294
|
+
prefixes = (Unit.kg**3).valid_prefixes
|
295
|
+
prefixes.should_not be_empty
|
296
|
+
prefixes.first.should be_a Unit::Prefix::SI
|
297
|
+
end
|
298
|
+
|
299
|
+
it "should return SI prefixes with single SI unit" do
|
300
|
+
prefixes = (Unit.lb**3).valid_prefixes
|
301
|
+
prefixes.should be_empty # no NonSI prefixes defined
|
302
|
+
end
|
303
|
+
|
304
|
+
it "should refuse to add prefix to multi-unit compound unit" do
|
305
|
+
lambda{(Unit.kg*Unit.m*Unit.m/Unit.s/Unit.s).with_prefix(:giga)}.should raise_error
|
306
|
+
end
|
307
|
+
|
308
|
+
it "should prefix a single unit compound unit" do
|
309
|
+
unit = (Unit.m**2).with_prefix(:kilo)
|
310
|
+
unit.name.should eql "square kilometre"
|
311
|
+
unit.factor.should == 1000000
|
312
|
+
end
|
313
|
+
|
314
|
+
|
315
|
+
end
|
205
316
|
end
|
data/spec/quantify_spec.rb
CHANGED
@@ -3,18 +3,4 @@ include Quantify
|
|
3
3
|
|
4
4
|
describe Quantify do
|
5
5
|
|
6
|
-
it "should initialize superscript format" do
|
7
|
-
Quantify.use_superscript_characters?.should be_true
|
8
|
-
end
|
9
|
-
|
10
|
-
it "should set superscript usage to true" do
|
11
|
-
Quantify.use_superscript_characters=true
|
12
|
-
Quantify.use_superscript_characters?.should be_true
|
13
|
-
end
|
14
|
-
|
15
|
-
it "should set superscript usage to false" do
|
16
|
-
Quantify.use_superscript_characters=false
|
17
|
-
Quantify.use_superscript_characters?.should be_false
|
18
|
-
end
|
19
|
-
|
20
6
|
end
|
data/spec/quantity_spec.rb
CHANGED
@@ -154,7 +154,7 @@ describe Quantity do
|
|
154
154
|
speed.class.should == Quantity
|
155
155
|
speed.value.should be_close 27.1143792976291, 0.00000001
|
156
156
|
speed.to_s(:name).should == "27.1143792976291 miles per hour"
|
157
|
-
speed.to_s.should == "27.1143792976291 mi
|
157
|
+
speed.to_s.should == "27.1143792976291 mi/h"
|
158
158
|
end
|
159
159
|
|
160
160
|
it "coerce method should handle inverted syntax" do
|
@@ -185,7 +185,7 @@ describe Quantity do
|
|
185
185
|
|
186
186
|
it "should convert compound units correctly" do
|
187
187
|
speed = Quantity.new 100, (Unit.km/Unit.h)
|
188
|
-
speed.to_mi.round(2).to_s.should == "62.14 mi
|
188
|
+
speed.to_mi.round(2).to_s.should == "62.14 mi/h"
|
189
189
|
end
|
190
190
|
|
191
191
|
it "should convert to SI unit correctly" do
|
@@ -224,9 +224,9 @@ describe Quantity do
|
|
224
224
|
unit = 50.ft ** -1
|
225
225
|
unit.to_s.should == "0.02 ft^-1"
|
226
226
|
unit = (10.m/1.s)**2
|
227
|
-
unit.to_s.should == "100.0 m²
|
227
|
+
unit.to_s.should == "100.0 m²/s²"
|
228
228
|
unit = (10.m/1.s)**-1
|
229
|
-
unit.to_s.should == "0.1 s
|
229
|
+
unit.to_s.should == "0.1 s/m"
|
230
230
|
lambda{ ((10.m/1.s)** 0.5) }.should raise_error
|
231
231
|
end
|
232
232
|
|
@@ -234,8 +234,8 @@ describe Quantity do
|
|
234
234
|
(50.ft.pow! 2).to_s.should == "2500.0 ft²"
|
235
235
|
(50.ft.pow! 3).to_s.should == "125000.0 ft³"
|
236
236
|
(50.ft.pow! -1).to_s.should == "0.02 ft^-1"
|
237
|
-
((10.m/1.s).pow! 2).to_s.should == "100.0 m²
|
238
|
-
((10.m/1.s).pow! -1).to_s.should == "0.1 s
|
237
|
+
((10.m/1.s).pow! 2).to_s.should == "100.0 m²/s²"
|
238
|
+
((10.m/1.s).pow! -1).to_s.should == "0.1 s/m"
|
239
239
|
lambda{ ((10.m/1.s).pow! 0.5) }.should raise_error
|
240
240
|
end
|
241
241
|
|