quantify 1.0.4 → 1.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.
- data/lib/quantify/core_extensions.rb +1 -1
- data/lib/quantify/dimensions.rb +5 -6
- data/lib/quantify/quantify.rb +2 -0
- data/lib/quantify/quantity.rb +21 -8
- data/lib/quantify/unit/base_unit.rb +69 -35
- data/lib/quantify/unit/compound_base_unit.rb +12 -11
- data/lib/quantify/unit/compound_unit.rb +4 -0
- data/lib/quantify/unit/non_si_unit.rb +5 -2
- data/lib/quantify/unit/unit.rb +8 -0
- data/spec/quantity_spec.rb +69 -0
- data/spec/unit_spec.rb +83 -6
- metadata +4 -4
data/lib/quantify/dimensions.rb
CHANGED
@@ -318,7 +318,7 @@ module Quantify
|
|
318
318
|
#
|
319
319
|
def si_base_units(by=nil)
|
320
320
|
self.to_hash.map do |dimension,index|
|
321
|
-
Unit.
|
321
|
+
Unit.base_quantity_si_units.select do |unit|
|
322
322
|
unit.measures == dimension.standardize
|
323
323
|
end.first.clone ** index
|
324
324
|
end.map(&by)
|
@@ -383,6 +383,8 @@ module Quantify
|
|
383
383
|
def multiply(other)
|
384
384
|
Dimensions.new(self.to_hash).multiply! other
|
385
385
|
end
|
386
|
+
alias :times :multiply
|
387
|
+
alias :* :multiply
|
386
388
|
|
387
389
|
# Similar to #multiply! but performs a division of self by the specified
|
388
390
|
# Dimensions object.
|
@@ -399,6 +401,7 @@ module Quantify
|
|
399
401
|
def divide(other)
|
400
402
|
Dimensions.new(self.to_hash).divide! other
|
401
403
|
end
|
404
|
+
alias :/ :divide
|
402
405
|
|
403
406
|
# Raises self to the power provided. As with multiply and divide, the
|
404
407
|
# #get_description method is invoked to attempt to find a suitable
|
@@ -422,6 +425,7 @@ module Quantify
|
|
422
425
|
def pow(power)
|
423
426
|
Dimensions.new(self.to_hash).pow!(power)
|
424
427
|
end
|
428
|
+
alias :** :pow
|
425
429
|
|
426
430
|
# Inverts self, returning a representation of 1/self. This is equivalent to
|
427
431
|
# raising to the power -1. The #get_description method is invoked to attempt
|
@@ -443,11 +447,6 @@ module Quantify
|
|
443
447
|
Dimensions.new(self.to_hash).reciprocalize!
|
444
448
|
end
|
445
449
|
|
446
|
-
alias :times :multiply
|
447
|
-
alias :* :multiply
|
448
|
-
alias :/ :divide
|
449
|
-
alias :** :pow
|
450
|
-
|
451
450
|
protected
|
452
451
|
|
453
452
|
# Returns an array containing the names of the instance variables which
|
data/lib/quantify/quantify.rb
CHANGED
data/lib/quantify/quantity.rb
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
module Quantify
|
4
4
|
class Quantity
|
5
5
|
|
6
|
+
include Comparable
|
7
|
+
|
6
8
|
# The quantity class represents quantities. Quantities are represented by a
|
7
9
|
# numeric value - of class Numeric - and a unit - of class Unit::Base.
|
8
10
|
#
|
@@ -174,9 +176,9 @@ module Quantify
|
|
174
176
|
end
|
175
177
|
|
176
178
|
def convert_compound_unit_to_si!
|
177
|
-
until self.unit.
|
179
|
+
until self.unit.is_base_quantity_si_unit? do
|
178
180
|
unit = self.unit.base_units.find do |base|
|
179
|
-
!base.
|
181
|
+
!base.is_base_quantity_si_unit?
|
180
182
|
end.unit
|
181
183
|
self.convert_compound_unit_to_non_equivalent_unit!(unit.si_unit)
|
182
184
|
end
|
@@ -241,28 +243,27 @@ module Quantify
|
|
241
243
|
def add(other)
|
242
244
|
Quantity.new(value,unit).add!(other)
|
243
245
|
end
|
246
|
+
alias :+ :add
|
244
247
|
|
245
248
|
def subtract(other)
|
246
249
|
Quantity.new(value,unit).subtract!(other)
|
247
250
|
end
|
251
|
+
alias :- :subtract
|
248
252
|
|
249
253
|
def multiply(other)
|
250
254
|
Quantity.new(value,unit).multiply!(other)
|
251
255
|
end
|
256
|
+
alias :times :multiply
|
257
|
+
alias :* :multiply
|
252
258
|
|
253
259
|
def divide(other)
|
254
260
|
Quantity.new(value,unit).divide!(other)
|
255
261
|
end
|
262
|
+
alias :/ :divide
|
256
263
|
|
257
264
|
def pow(power)
|
258
265
|
Quantity.new(value,unit).pow!(power)
|
259
266
|
end
|
260
|
-
|
261
|
-
alias :times :multiply
|
262
|
-
alias :* :multiply
|
263
|
-
alias :+ :add
|
264
|
-
alias :- :subtract
|
265
|
-
alias :/ :divide
|
266
267
|
alias :** :pow
|
267
268
|
|
268
269
|
def rationalize_units
|
@@ -300,6 +301,18 @@ module Quantify
|
|
300
301
|
rounded_quantity.round! decimal_places
|
301
302
|
end
|
302
303
|
|
304
|
+
def <=>(other)
|
305
|
+
raise Exceptions::InvalidArgumentError unless other.is_a? Quantity
|
306
|
+
raise Exceptions::InvalidArgumentError unless other.unit.is_alternative_for?(unit)
|
307
|
+
other = other.to unit
|
308
|
+
value.to_f <=> other.value.to_f
|
309
|
+
end
|
310
|
+
|
311
|
+
def ===(range)
|
312
|
+
raise Exceptions::InvalidArgumentError unless range.is_a? Range
|
313
|
+
range.include? self
|
314
|
+
end
|
315
|
+
|
303
316
|
# Enables shorthand for reciprocal of quantity, e.g.
|
304
317
|
#
|
305
318
|
# quantity = 2.m
|
@@ -11,9 +11,8 @@ module Quantify
|
|
11
11
|
# Create a new instance of self (i.e. Base or an inherited class) and load
|
12
12
|
# into the system of known units. See initialize for details of options
|
13
13
|
#
|
14
|
-
def self.load(options)
|
15
|
-
|
16
|
-
unit.load
|
14
|
+
def self.load(options=nil,&block)
|
15
|
+
self.new(options,&block).load
|
17
16
|
end
|
18
17
|
|
19
18
|
def self.construct_and_load(unit,&block)
|
@@ -50,8 +49,8 @@ module Quantify
|
|
50
49
|
return new_unit
|
51
50
|
end
|
52
51
|
|
53
|
-
# Syntactic sugar for defining the known
|
54
|
-
# associated units to be loaded at runtime, e.g.
|
52
|
+
# Syntactic sugar for defining the units known to the system, enabling the
|
53
|
+
# required associated units to be loaded at runtime, e.g.
|
55
54
|
#
|
56
55
|
# Unit::[Base|SI|NonSI].configure do |config|
|
57
56
|
#
|
@@ -65,9 +64,9 @@ module Quantify
|
|
65
64
|
class_eval &block if block
|
66
65
|
end
|
67
66
|
|
68
|
-
attr_accessor :name, :symbol, :label
|
69
|
-
|
70
|
-
|
67
|
+
attr_accessor :name, :symbol, :label, :factor
|
68
|
+
attr_reader :dimensions
|
69
|
+
attr_reader :acts_as_alternative_unit, :acts_as_equivalent_unit
|
71
70
|
|
72
71
|
# Create a new Unit::Base instance.
|
73
72
|
#
|
@@ -101,52 +100,69 @@ module Quantify
|
|
101
100
|
# representation in the Dimensions class. This dimensions attribute is to
|
102
101
|
# provide much of the unit functionality
|
103
102
|
#
|
104
|
-
def initialize(options=nil)
|
103
|
+
def initialize(options=nil)
|
104
|
+
@acts_as_alternative_unit = true
|
105
|
+
@acts_as_equivalent_unit = false
|
106
|
+
@factor = 1.0
|
107
|
+
@symbol = nil
|
108
|
+
@label = nil
|
105
109
|
if options.is_a? Hash
|
110
|
+
self.dimensions = options[:dimensions] || options[:physical_quantity]
|
106
111
|
@name = options[:name].standardize.singularize.downcase
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
elsif options[:dimensions].is_a? String or options[:dimensions].is_a? Symbol
|
111
|
-
@dimensions = Dimensions.for options[:dimensions]
|
112
|
-
else
|
113
|
-
raise Exceptions::InvalidArgumentError, "Unknown physical_quantity specified"
|
114
|
-
end
|
115
|
-
@factor = options[:factor].nil? ? 1.0 : options[:factor].to_f
|
116
|
-
@symbol = options[:symbol].nil? ? nil : options[:symbol].standardize
|
117
|
-
@label = options[:label].nil? ? nil : options[:label].to_s
|
118
|
-
@acts_as_alternative_unit = true
|
119
|
-
@acts_as_equivalent_unit = false
|
112
|
+
@factor = options[:factor].to_f if options[:factor]
|
113
|
+
@symbol = options[:symbol].standardize if options[:symbol]
|
114
|
+
@label = options[:label].to_s if options[:label]
|
120
115
|
end
|
121
116
|
yield self if block_given?
|
122
117
|
valid?
|
123
118
|
end
|
124
119
|
|
120
|
+
def dimensions=(dimensions)
|
121
|
+
if dimensions.is_a? Dimensions
|
122
|
+
@dimensions = dimensions
|
123
|
+
elsif dimensions.is_a? String or dimensions.is_a? Symbol
|
124
|
+
@dimensions = Dimensions.for dimensions
|
125
|
+
else
|
126
|
+
raise Exceptions::InvalidArgumentError, "Unknown physical_quantity specified"
|
127
|
+
end
|
128
|
+
end
|
129
|
+
alias :physical_quantity= :dimensions=
|
130
|
+
|
125
131
|
# Permits a block to be used, operating on self. This is useful for modifying
|
126
132
|
# the attributes of an already instantiated unit, especially when defining
|
127
133
|
# units on the basis of operation on existing units for adding specific
|
128
134
|
# (rather than derived) names or symbols, e.g.
|
129
135
|
#
|
130
|
-
# (Unit.pound_force/(Unit.in**2)).
|
136
|
+
# (Unit.pound_force/(Unit.in**2)).configure do |unit|
|
131
137
|
# unit.symbol = 'psi'
|
132
138
|
# unit.label = 'psi'
|
133
139
|
# unit.name = 'pound per square inch'
|
134
140
|
# end
|
135
141
|
#
|
136
|
-
def
|
142
|
+
def configure
|
137
143
|
yield self if block_given?
|
138
144
|
return self if valid?
|
139
145
|
end
|
140
146
|
|
147
|
+
# Similar to #configure but makes the new unit configuration the canonical
|
148
|
+
# unit for self.label
|
149
|
+
#
|
150
|
+
def configure_as_canonical &block
|
151
|
+
unload if loaded?
|
152
|
+
configure &block if block_given?
|
153
|
+
make_canonical
|
154
|
+
end
|
155
|
+
|
141
156
|
# Load an initialized Unit into the system of known units.
|
142
157
|
#
|
143
|
-
# If a block is given, the unit can be
|
144
|
-
# similar to way to the #
|
158
|
+
# If a block is given, the unit can be configured prior to loading, in a
|
159
|
+
# similar to way to the #configure method.
|
145
160
|
#
|
146
161
|
def load
|
147
162
|
yield self if block_given?
|
148
163
|
raise Exceptions::InvalidArgumentError, "A unit with the same label: #{self.name}) already exists" if loaded?
|
149
164
|
Quantify::Unit.units << self if valid?
|
165
|
+
return self
|
150
166
|
end
|
151
167
|
|
152
168
|
# Remove from system of known units.
|
@@ -159,8 +175,16 @@ module Quantify
|
|
159
175
|
Unit.units.any? { |unit| self.has_same_identity_as? unit }
|
160
176
|
end
|
161
177
|
|
178
|
+
# Make self the canonical representation of the unit defined by self#label
|
162
179
|
def make_canonical
|
163
|
-
unload
|
180
|
+
unload if loaded?
|
181
|
+
load
|
182
|
+
end
|
183
|
+
|
184
|
+
# Set the canonical unit label - the unique unit identifier - to a new value
|
185
|
+
def canonical_label=(new_label)
|
186
|
+
unload if loaded?
|
187
|
+
self.label = new_label
|
164
188
|
load
|
165
189
|
end
|
166
190
|
|
@@ -203,11 +227,21 @@ module Quantify
|
|
203
227
|
self.name.pluralize
|
204
228
|
end
|
205
229
|
|
206
|
-
|
230
|
+
# Determine if the unit represents one of the base quantities, length,
|
231
|
+
# mass, time, temperature, etc.
|
232
|
+
#
|
207
233
|
def is_base_unit?
|
208
234
|
Dimensions::BASE_QUANTITIES.map(&:standardize).include? self.measures
|
209
235
|
end
|
210
236
|
|
237
|
+
# Determine if the unit is THE canonical SI unit for a base quantity (length,
|
238
|
+
# mass, time, etc.). This method ignores prefixed versions of SI base units,
|
239
|
+
# returning true only for metre, kilogram, second, Kelvin, etc.
|
240
|
+
#
|
241
|
+
def is_base_quantity_si_unit?
|
242
|
+
is_si_unit? and is_base_unit? and is_benchmark_unit?
|
243
|
+
end
|
244
|
+
|
211
245
|
# Determine is the unit is a derived unit - that is, a unit made up of more
|
212
246
|
# than one of the base quantities
|
213
247
|
#
|
@@ -225,7 +259,8 @@ module Quantify
|
|
225
259
|
# Determine if the unit is one of the units against which all other units
|
226
260
|
# of the same physical quantity are defined. These units are almost entirely
|
227
261
|
# equivalent to the non-prefixed, SI units, but the one exception is the
|
228
|
-
# kilogram,
|
262
|
+
# kilogram, which is an oddity in being THE canonical SI unit for mass, yet
|
263
|
+
# containing a prefix. This oddity makes this method useful/necessary.
|
229
264
|
#
|
230
265
|
def is_benchmark_unit?
|
231
266
|
self.factor == 1.0
|
@@ -277,7 +312,7 @@ module Quantify
|
|
277
312
|
self.send(attr) == other.send(attr)
|
278
313
|
end
|
279
314
|
end
|
280
|
-
|
315
|
+
|
281
316
|
alias :== :is_same_as?
|
282
317
|
|
283
318
|
# Check if unit has the identity as another, i.e. the same label. This is
|
@@ -375,6 +410,8 @@ module Quantify
|
|
375
410
|
other.instance_of?(Unit::Compound) ? options += other.base_units : options << other
|
376
411
|
Unit::Compound.new(*options)
|
377
412
|
end
|
413
|
+
alias :times :multiply
|
414
|
+
alias :* :multiply
|
378
415
|
|
379
416
|
# Divide one unit by another. This results in the generation of a compound
|
380
417
|
# unit.
|
@@ -393,6 +430,7 @@ module Quantify
|
|
393
430
|
end
|
394
431
|
Unit::Compound.new(*options)
|
395
432
|
end
|
433
|
+
alias :/ :divide
|
396
434
|
|
397
435
|
# Raise a unit to a power. This results in the generation of a compound
|
398
436
|
# unit, e.g. m^3.
|
@@ -412,17 +450,13 @@ module Quantify
|
|
412
450
|
end
|
413
451
|
return new_unit
|
414
452
|
end
|
453
|
+
alias :** :pow
|
415
454
|
|
416
455
|
# Return new unit representing the reciprocal of self, i.e. 1/self
|
417
456
|
def reciprocalize
|
418
457
|
Unit.unity / self
|
419
458
|
end
|
420
459
|
|
421
|
-
alias :times :multiply
|
422
|
-
alias :* :multiply
|
423
|
-
alias :/ :divide
|
424
|
-
alias :** :pow
|
425
|
-
|
426
460
|
# Apply a prefix to self. Returns new unit according to the prefixed version
|
427
461
|
# of self, complete with modified name, symbol, factor, etc..
|
428
462
|
#
|
@@ -67,20 +67,21 @@ module Quantify
|
|
67
67
|
@index < 0
|
68
68
|
end
|
69
69
|
|
70
|
-
|
71
|
-
|
70
|
+
# The following methods refer only to the unit of the CompoundBaseUnit
|
71
|
+
# object, rather than the unit *together with its index*
|
72
|
+
|
73
|
+
Unit_Methods = [ :base_quantity_si_unit, :base_unit, :benchmark_unit, :si_unit,
|
74
|
+
:non_si_unit, :prefixed_unit, :derived_unit ]
|
75
|
+
|
76
|
+
Unit_Methods.each do |method|
|
77
|
+
method = "is_#{method.to_s}?"
|
78
|
+
define_method(method) do
|
79
|
+
@unit.send method.to_sym
|
80
|
+
end
|
72
81
|
end
|
73
82
|
|
74
|
-
def is_non_si_unit?
|
75
|
-
@unit.is_non_si_unit?
|
76
|
-
end
|
77
|
-
|
78
|
-
# Physical quantity represented by self. This refers only to the unit, rather
|
79
|
-
# than the unit together with the index. Is used to match base units with
|
80
|
-
# similar units of same physical quantity
|
81
|
-
#
|
82
83
|
def measures
|
83
|
-
@unit.
|
84
|
+
@unit.measures
|
84
85
|
end
|
85
86
|
|
86
87
|
def initialize_copy(source)
|
@@ -131,6 +131,10 @@ module Quantify
|
|
131
131
|
@base_units.any? { |base| base.is_non_si_unit? }
|
132
132
|
end
|
133
133
|
|
134
|
+
def is_base_quantity_si_unit?
|
135
|
+
@base_units.all? { |base| base.is_base_quantity_si_unit? }
|
136
|
+
end
|
137
|
+
|
134
138
|
# Consolidate base units. A 'full' consolidation is performed, i.e.
|
135
139
|
# consolidation across numerator and denominator. This is equivalent to the
|
136
140
|
# automatic partial consolidation AND a cancelling of units (i.e.
|
@@ -10,8 +10,11 @@ module Quantify
|
|
10
10
|
# These are required in order to perform conversion, e.g. kelvin => celsius
|
11
11
|
# and therefore become and additional attribute to NonSI units
|
12
12
|
#
|
13
|
-
def initialize(options)
|
14
|
-
@scaling =
|
13
|
+
def initialize(options=nil)
|
14
|
+
@scaling = 0.0
|
15
|
+
if options.is_a? Hash and options[:scaling]
|
16
|
+
@scaling = options.delete(:scaling).to_f
|
17
|
+
end
|
15
18
|
super(options)
|
16
19
|
end
|
17
20
|
|
data/lib/quantify/unit/unit.rb
CHANGED
@@ -187,6 +187,14 @@ module Quantify
|
|
187
187
|
end
|
188
188
|
end
|
189
189
|
|
190
|
+
# This returns the suite of units which represents THE SI units for each of
|
191
|
+
# the base dimensions, i.e. metre, kilogram, second, etc. but not prefixed
|
192
|
+
# versions of the same unit
|
193
|
+
#
|
194
|
+
def self.base_quantity_si_units
|
195
|
+
@units.select {|unit| unit.is_base_quantity_si_unit? }
|
196
|
+
end
|
197
|
+
|
190
198
|
# This can be replicated by method missing approach, but explicit method provided
|
191
199
|
# given importance in #match (and #for) methods regexen
|
192
200
|
#
|
data/spec/quantity_spec.rb
CHANGED
@@ -200,6 +200,11 @@ describe Quantity do
|
|
200
200
|
speed.to_si.to_s(:name).should == "44.704 metres per second"
|
201
201
|
end
|
202
202
|
|
203
|
+
it "should convert compound units to SI correctly" do
|
204
|
+
speed = Quantity.new 100, (Unit.km/Unit.h)
|
205
|
+
speed.to_si.to_s(:name).should == "27.7777777777778 metres per second"
|
206
|
+
end
|
207
|
+
|
203
208
|
it "should convert compound units to SI correctly" do
|
204
209
|
pressure = Quantity.new 100, (Unit.pound_force_per_square_inch)
|
205
210
|
pressure.to_si.round.to_s(:name).should == "689476 pascals"
|
@@ -261,5 +266,69 @@ describe Quantity do
|
|
261
266
|
quantity.rationalize_units!
|
262
267
|
quantity.to_s.should eql "144.0 yd^2"
|
263
268
|
end
|
269
|
+
|
270
|
+
it "should be greater than" do
|
271
|
+
(20.ft > 1.m).should be_true
|
272
|
+
end
|
273
|
+
|
274
|
+
it "should be greater than" do
|
275
|
+
(20.ft > 7.m).should be_false
|
276
|
+
end
|
277
|
+
|
278
|
+
it "should be less than" do
|
279
|
+
(20.ft/1.h < 8.yd/60.min).should be_true
|
280
|
+
end
|
281
|
+
|
282
|
+
it "should be equal" do
|
283
|
+
(1.yd == 3.ft).should be_true
|
284
|
+
end
|
285
|
+
|
286
|
+
it "should be between with same units" do
|
287
|
+
(25.ft.between? 1.ft,30.ft).should be_true
|
288
|
+
end
|
289
|
+
|
290
|
+
it "should be between even with different units" do
|
291
|
+
(25.ft.between? 1.ft,10.m).should be_true
|
292
|
+
end
|
293
|
+
|
294
|
+
it "comparison with non quantity should raise error" do
|
295
|
+
lambda{20.ft > 3}.should raise_error
|
296
|
+
end
|
297
|
+
|
298
|
+
it "comparison with non compatible quantity should raise error" do
|
299
|
+
lambda{20.ft > 4.K}.should raise_error
|
300
|
+
end
|
301
|
+
|
302
|
+
it "should be range" do
|
303
|
+
(2.ft..20.ft).should be_a Range
|
304
|
+
end
|
305
|
+
|
306
|
+
it "should return between value from range" do
|
307
|
+
(2.ft..20.ft).include?(3.ft).should be_true
|
308
|
+
end
|
309
|
+
|
310
|
+
it "should return between value from range with different units" do
|
311
|
+
(2.ft..4.m).include?(200.cm).should be_true
|
312
|
+
(1.ly..1.parsec).include?(2.ly).should be_true
|
313
|
+
(1.ly..1.parsec).include?(2.in).should be_false
|
314
|
+
end
|
315
|
+
|
316
|
+
it "should return between value from range using === operator" do
|
317
|
+
(3.ft === (2.ft..20.ft)).should be_true
|
318
|
+
end
|
319
|
+
|
320
|
+
it "should return between value from range with different units using === operator" do
|
321
|
+
(200.cm === (2.ft..4.m)).should be_true
|
322
|
+
(2.ly === (1.ly..1.parsec)).should be_true
|
323
|
+
(2.in === (1.ly..1.parsec)).should be_false
|
324
|
+
end
|
325
|
+
|
326
|
+
it "range comparison with non compatible quantity should raise error" do
|
327
|
+
lambda{20.ft === (1.ft..3.K)}.should raise_error
|
328
|
+
end
|
329
|
+
|
330
|
+
it "range comparison with non quantity should raise error" do
|
331
|
+
lambda{20.ft === (1.ft..3)}.should raise_error
|
332
|
+
end
|
264
333
|
end
|
265
334
|
|
data/spec/unit_spec.rb
CHANGED
@@ -168,16 +168,16 @@ describe Unit do
|
|
168
168
|
end}.should raise_error
|
169
169
|
end
|
170
170
|
|
171
|
-
it "should modify unit attributes with block and #
|
172
|
-
unit = (Unit.kg/Unit.kWh).
|
171
|
+
it "should modify unit attributes with block and #configure" do
|
172
|
+
unit = (Unit.kg/Unit.kWh).configure do |u|
|
173
173
|
u.name = 'electricity emissions factor'
|
174
174
|
end
|
175
175
|
unit.class.should == Quantify::Unit::Compound
|
176
176
|
unit.name = 'electricity emissions factor'
|
177
177
|
end
|
178
178
|
|
179
|
-
it "should raise error with block and #
|
180
|
-
lambda{(Unit.kg/Unit.kWh).
|
179
|
+
it "should raise error with block and #configure which removes name" do
|
180
|
+
lambda{(Unit.kg/Unit.kWh).configure do |u|
|
181
181
|
u.name = ""
|
182
182
|
end}.should raise_error
|
183
183
|
end
|
@@ -286,15 +286,65 @@ describe Unit do
|
|
286
286
|
it "should make new unit configuration canonical" do
|
287
287
|
unit = Unit.psi
|
288
288
|
unit.name.should == 'pound force per square inch'
|
289
|
-
unit.
|
289
|
+
unit.configure {|unit| unit.name = 'PSI'}
|
290
290
|
unit.name.should == 'PSI'
|
291
291
|
Unit.psi.name.should == 'pound force per square inch'
|
292
292
|
unit.make_canonical
|
293
293
|
Unit.psi.name.should == 'PSI'
|
294
|
-
unit.
|
294
|
+
unit.configure {|unit| unit.name = 'pound force per square inch'}
|
295
295
|
unit.make_canonical
|
296
296
|
end
|
297
297
|
|
298
|
+
it "should change the label of canonical unit representation" do
|
299
|
+
unit = Unit.cubic_metre
|
300
|
+
unit.label.should eql "m^3"
|
301
|
+
unit.canonical_label = "m3"
|
302
|
+
unit.label.should eql "m3"
|
303
|
+
Unit.cubic_metre.label.should eql "m3"
|
304
|
+
end
|
305
|
+
|
306
|
+
it "should configure on canonical unit" do
|
307
|
+
unit = Unit.kg.configure_as_canonical do |unit|
|
308
|
+
unit.name = 'killogram'
|
309
|
+
end
|
310
|
+
unit.name.should eql 'killogram'
|
311
|
+
unit.symbol.should eql 'kg'
|
312
|
+
Unit.kg.name.should eql 'killogram'
|
313
|
+
(Unit.killogram).should be_a Unit::Base
|
314
|
+
|
315
|
+
unit = Unit.kg.configure_as_canonical do |unit|
|
316
|
+
unit.name = 'kilogram'
|
317
|
+
end
|
318
|
+
end
|
319
|
+
|
320
|
+
it "should configure on canonical unit even if changing label" do
|
321
|
+
unit = Unit.barn.configure_as_canonical do |unit|
|
322
|
+
unit.label = 'BARN'
|
323
|
+
end
|
324
|
+
unit.label.should eql 'BARN'
|
325
|
+
unit.symbol.should eql 'b'
|
326
|
+
Unit.barn.label.should eql 'BARN'
|
327
|
+
(Unit.BARN).should be_a Unit::Base
|
328
|
+
|
329
|
+
unit = Unit.barn.configure_as_canonical do |unit|
|
330
|
+
unit.label = 'b'
|
331
|
+
end
|
332
|
+
end
|
333
|
+
|
334
|
+
it "should configure on canonical unit even if changing label using canonical_label=" do
|
335
|
+
unit = Unit.barn.configure_as_canonical do |unit|
|
336
|
+
unit.canonical_label = 'BARN'
|
337
|
+
end
|
338
|
+
unit.label.should eql 'BARN'
|
339
|
+
unit.symbol.should eql 'b'
|
340
|
+
Unit.barn.label.should eql 'BARN'
|
341
|
+
(Unit.BARN).should be_a Unit::Base
|
342
|
+
|
343
|
+
unit = Unit.barn.configure_as_canonical do |unit|
|
344
|
+
unit.label = 'b'
|
345
|
+
end
|
346
|
+
end
|
347
|
+
|
298
348
|
end
|
299
349
|
|
300
350
|
describe "unit initialization" do
|
@@ -322,6 +372,33 @@ describe Unit do
|
|
322
372
|
it "should load unit into module array with class method" do
|
323
373
|
unit = Unit::NonSI.load :name => 'a name', :physical_quantity => :energy, :factor => 10, :symbol => 'anm', :label => 'a_name'
|
324
374
|
Unit.non_si_units_by_name.should include 'a name'
|
375
|
+
Unit.unload(unit)
|
376
|
+
end
|
377
|
+
|
378
|
+
it "should load unit into module array with class method and block" do
|
379
|
+
Unit::NonSI.load do |unit|
|
380
|
+
unit.name = 'a name'
|
381
|
+
unit.physical_quantity = :energy
|
382
|
+
unit.factor = 10
|
383
|
+
unit.symbol = 'anm'
|
384
|
+
unit.label = 'a_name'
|
385
|
+
end
|
386
|
+
Unit.non_si_units_by_name.should include 'a name'
|
387
|
+
Unit.a_name.measures.should eql 'energy'
|
388
|
+
Unit.unload('a_name')
|
389
|
+
end
|
390
|
+
|
391
|
+
it "should load unit into module array with class method and block and #dimensions method" do
|
392
|
+
Unit::NonSI.load do |unit|
|
393
|
+
unit.name = 'a name'
|
394
|
+
unit.dimensions = :energy
|
395
|
+
unit.factor = 10
|
396
|
+
unit.symbol = 'anm'
|
397
|
+
unit.label = 'a_name'
|
398
|
+
end
|
399
|
+
Unit.non_si_units_by_name.should include 'a name'
|
400
|
+
Unit.a_name.measures.should eql 'energy'
|
401
|
+
Unit.unload('a_name')
|
325
402
|
end
|
326
403
|
|
327
404
|
it "should derive compound unit correctly" do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: quantify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 5
|
10
|
+
version: 1.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Andrew Berkeley
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-06-21 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|