quantify 3.1.2 → 3.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -13,58 +13,58 @@ Homepage: https://github.com/spatchcock/quantify
13
13
  Quick introduction
14
14
  ------------------
15
15
 
16
- Operating on quantities
16
+ # Operating on quantities
17
17
 
18
- 12.feet + 12.feet => "24.0 feet"
18
+ 12.feet + 12.feet #=> "24.0 feet"
19
19
 
20
- 6.m ** 2 => "36.0 m²"
20
+ 6.m ** 2 #=> "36.0 m²"
21
21
 
22
- 100.km / 2.h => "50 kilometers per hour"
22
+ 100.km / 2.h #=> "50 kilometers per hour"
23
23
 
24
- 5000.L.to_bbl => "31.4490528488754 barrels"
24
+ 5000.L.to_bbl #=> "31.4490528488754 barrels"
25
25
 
26
- 1.5.lb.to_si.round(2) => "0.68 kg"
26
+ 1.5.lb.to_si.round(2) #=> "0.68 kg"
27
27
 
28
- Unit.ratio :kg, :ton => "1.016047 kilograms per long ton"
28
+ Unit.ratio :kg, :ton #=> "1016.047 kilograms per long ton"
29
29
 
30
30
  Note: these results are string representations of the actual objects
31
31
  which result from these operations, using the Quantity#to_s method which
32
32
  renders quantities using either the unit name or symbol.
33
33
 
34
- Handling units
34
+ # Handling units
35
35
 
36
- Unit.ton.name => "long ton"
36
+ Unit.ton.name #=> "long ton"
37
37
 
38
- Unit.ton.label => "ton_uk"
38
+ Unit.ton.label #=> "ton_uk"
39
39
 
40
- Unit.ton.measures => "mass"
40
+ Unit.ton.measures #=> "mass"
41
41
 
42
- Unit.ton.alternatives :name => [ "kilogram",
43
- "gram",
44
- "carat",
45
- "electron mass",
46
- "grain",
47
- "hundredweight long",
48
- "hundredweight short",
49
- "ounce",
50
- "pennyweight",
51
- "pound",
52
- "short ton",
53
- "stone",
54
- "tonne",
55
- "unified atomic mass" ]
42
+ Unit.ton.alternatives :name #=> [ "kilogram",
43
+ "gram",
44
+ "carat",
45
+ "electron mass",
46
+ "grain",
47
+ "hundredweight long",
48
+ "hundredweight short",
49
+ "ounce",
50
+ "pennyweight",
51
+ "pound",
52
+ "short ton",
53
+ "stone",
54
+ "tonne",
55
+ "unified atomic mass" ]
56
+
57
+ Unit.ton.si_unit #=> 'kg'
56
58
 
57
- Unit.ton.si_unit => 'kg'
59
+ Unit.ton.dimensions #=> <Quantify::Dimensions:0xb75467c8 ... >
58
60
 
59
- Unit.ton.dimensions => <Quantify::Dimensions:0xb75467c8 ... >
61
+ Unit.ton.dimensions.describe #=> "mass"
60
62
 
61
- Unit.ton.dimensions.describe => "mass"
63
+ Unit.ton.dimensions.mass #=> 1
62
64
 
63
- Unit.ton.dimensions.mass => 1
65
+ Unit.ton.dimensions.length #=> nil
64
66
 
65
- Unit.ton.dimensions.length => nil
66
-
67
-
67
+
68
68
  General introduction
69
69
  --------------------
70
70
 
@@ -73,89 +73,83 @@ Quantify represents physical quantities using the Quantify::Quantity class.
73
73
  A Quantity object holds both a value (Numeric) and a unit (of the class
74
74
  Quantify::Unit::Base), for example a Quantity object might represent 12 kgs (value, 12; unit, kilogram).
75
75
 
76
- Quantities can be manipulated and operated on, in all of the ways that might be required for real physical quantities. Operations include, addition, subtraction, multiplying and dividing by scalar values or other quantities, raising to powers, converting into alternative unit representations (e.g. kgs to lbs, miles per hour to metres per second), and rounding of values. Quantify handles the converting of both values and units so that the result is a an accurate representation of the
77
- operation. For example, multiplying 10 metres by 10 metres will result in a quanity of square metres, whereas dividing, say, 10 metres by 2 seconds will result in a quantity in metres per second.
76
+ Quantities can be manipulated and operated on, in all of the ways that might be required for real physical quantities. Operations include, addition, subtraction, multiplying and dividing by scalar values or other quantities, raising to powers, converting into alternative unit representations (e.g. kgs to lbs, miles per hour to metres per second), and rounding of values. Quantify handles the converting of both values and units so that the result is a an accurate representation of the operation. For example, multiplying 10 metres by 10 metres will result in a quanity of square metres, whereas dividing, say, 10 metres by 2 seconds will result in a quantity in metres per second.
78
77
 
79
78
  In all cases the results of operations are a changes to the Quantity instance or a new instance of a Quantity object. The new value or unit can be accessed using the #value and #unit attributes, or the #to_s method which renders the quantity in string form.
80
79
 
81
80
  There are several ways to initialize a quantity object
82
81
 
83
- mass = Quantity.new(100,:lb) => <Quantify::Quantity:0xb7332bbc ... >
84
- mass = Quantity.new(100,'pound') => <Quantify::Quantity:0xb7332bbc ... >
85
- mass = 100.lb => <Quantify::Quantity:0xb7332bbc ... >
86
- mass = 100.pound => <Quantify::Quantity:0xb7332bbc ... >
87
- mass = Quantity.parse "100 lb" => <Quantify::Quantity:0xb7332bbc ... >
88
- mass = "100 lb".to_q => <Quantify::Quantity:0xb7332bbc ... >
82
+ mass = Quantity.new(100,:lb) #=> <Quantify::Quantity:0xb7332bbc ... >
83
+ mass = Quantity.new(100,'pound') #=> <Quantify::Quantity:0xb7332bbc ... >
84
+ mass = 100.lb #=> <Quantify::Quantity:0xb7332bbc ... >
85
+ mass = 100.pound #=> <Quantify::Quantity:0xb7332bbc ... >
86
+ mass = Quantity.parse "100 lb" #=> <Quantify::Quantity:0xb7332bbc ... >
87
+ mass = "100 lb".to_q #=> <Quantify::Quantity:0xb7332bbc ... >
89
88
 
90
- Quantity object attributes
89
+ Quantity object can be insterrogated for a range of attributes
91
90
 
92
- mass.value => 100.0
93
- mass.unit => <Quantify::Unit::NonSI:0xb7332b08 ... >
94
- mass.unit.name => "pound"
95
- mass.unit.symbol => "lb"
91
+ mass.value #=> 100.0
92
+ mass.unit #=> <Quantify::Unit::NonSI:0xb7332b08 ... >
93
+ mass.unit.name #=> "pound"
94
+ mass.unit.symbol #=> "lb"
96
95
 
97
- # unique identifier, follows JScience
98
- mass.unit.label => "lb"
99
- mass.unit.pluralized_name => "pounds"
100
- mass.to_s => "100 lb"
101
- mass.to_s(:name) => "100 pounds"
96
+ # unique identifier, follows JScience
97
+ mass.unit.label #=> "lb"
98
+ mass.unit.pluralized_name #=> "pounds"
99
+ mass.to_s #=> "100 lb"
100
+ mass.to_s(:name) #=> "100 pounds"
102
101
 
103
- # Describe the physical quantity represented by the quantity
104
- mass.represents => "mass"
102
+ # Describe the physical quantity represented by the quantity
103
+ mass.represents #=> "mass"
105
104
 
106
105
  Convert a quantity to a different unit
107
106
 
108
- energy = 100.kWh => "100 kilowatt hours"
109
-
110
- new_energy = energy.to_megajoules => "360.0 MJ"
111
-
112
- new_energy = energy.to_MJ => "360.0 MJ"
113
-
114
- new_energy = energy.to(:MJ) => "360.0 MJ"
115
-
116
- # Initialize a unit object and pass as conversion argument
117
- unit = Unit.MJ => <Quantify::Unit::SI:0xb75c9718 ... >
118
- new_energy = energy.to(unit) => "360.0 MJ"
107
+ energy = 100.kWh #=> "100 kilowatt hours"
108
+
109
+ new_energy = energy.to_megajoules #=> "360.0 MJ"
110
+
111
+ new_energy = energy.to_MJ #=> "360.0 MJ"
112
+
113
+ new_energy = energy.to(:MJ) #=> "360.0 MJ"
119
114
 
120
- Note: all of the above results are string representations of the actual objects which result from these operations.
115
+ # Initialize a unit object and pass as conversion argument
116
+ unit = Unit.MJ #=> <Quantify::Unit::SI:0xb75c9718 ... >
117
+ new_energy = energy.to(unit) #=> "360.0 MJ"
121
118
 
122
119
  Convert the units of a quantity with a compound unit
123
120
 
124
- speed = 70.mi/1.h => "70.0 mi/h"
125
-
126
- speed_in_kms = speed.to_km => "112.65408 km/h"
121
+ speed = 70.mi/1.h #=> "70.0 mi/h"
127
122
 
128
- speed_in_mins = speed_in_kms.to_min => "1.877568 km/min"
123
+ speed_in_kms = speed.to_km #=> "112.65408 km/h"
129
124
 
130
- Note: all of the above results are string representations of the actual objects which result from these operations.
125
+ speed_in_mins = speed_in_kms.to_min #=> "1.877568 km/min"
131
126
 
132
127
  Convert a quantity to the corresponding SI unit
133
128
 
134
- energy = 100.kWh => "100 kWh"
135
- si = quantity.to_si => "360000000.0 J"
136
-
137
- Note: all of the above results are string representations of the actual
138
- objects which result from these operations.
129
+ energy = 100.kWh #=> "100 kWh"
130
+ si = quantity.to_si #=> "360000000.0 J"
139
131
 
140
132
  Operate on a quantity
141
133
 
142
- mass = 10.kg * 3 => "30.0 kg"
134
+ mass = 10.kg * 3 #=> "30.0 kg"
143
135
 
144
- distance = 100.light_years / 20 => "5.0 ly"
136
+ distance = 100.light_years / 20 #=> "5.0 ly"
145
137
 
146
- area = 10.m * 10.m => "100.0 square metres"
138
+ area = 10.m * 10.m #=> "100.0 square metres"
147
139
 
148
- speed = 250.mi / 3.h => "83.3333333333333 miles per hour"
140
+ speed = 250.mi / 3.h #=> "83.3333333333333 miles per hour"
149
141
 
150
- speed = 70.mi/1.h => <Quantify::Quantity:0xb7332bbc ... >
151
- time = 0.5.h => <Quantify::Quantity:3xf3472hjc ... >
152
- distance = speed * time => <Quantify::Quantity:7d7f8g9d5g ... >
153
- distance.to_s => "35.0 mi"
142
+ speed = 70.mi/1.h #=> <Quantify::Quantity:0xb7332bbc ... >
143
+ time = 0.5.h #=> <Quantify::Quantity:3xf3472hjc ... >
144
+ distance = speed * time #=> <Quantify::Quantity:7d7f8g9d5g ... >
145
+ distance.to_s #=> "35.0 mi"
154
146
 
155
147
  Note: all of the above results are string representations of the actual
156
148
  objects which result from these operations.
157
149
 
158
150
  Additional operations
151
+ ---------------------
152
+
159
153
  The result of quantity operations is commonly a new quantity with a compound unit. Unless the result is equivalent to one of the base SI units (m, kg, s, K, etc.) or one of the following, square metre, cubic metre, joule, watt, newton or pascal, then the compound unit represents appropriate combination of the units involved, albeit with any like-units within the numerator and denominator grouped under a single
160
154
  power/index.
161
155
 
@@ -163,64 +157,24 @@ Units are not automatically cancelled or rationalized (made consistent). This is
163
157
  and the user may not necessarily prefer a conversion into consistent mass units (e.g. g/g or t/t). Therefore, the following methods are available...
164
158
 
165
159
  Where units representing the same physical quantity appear together, they can be made consistent by simply converting to the unit which is desired:
166
-
167
- area = 12.yd * 36.ft => <Quantify::Quantity:0xb7332bbc ... >
168
- area.to_s => "432.0 yd ft"
169
- area.to_yd
170
- area.to_s => "144.0 yd²"
160
+
161
+ area = 12.yd * 36.ft #=> <Quantify::Quantity:0xb7332bbc ... >
162
+ area.to_s #=> "432.0 yd ft"
163
+ area.to_yd
164
+ area.to_s #=> "144.0 yd²"
171
165
 
172
166
  Alternatively, all units within the numerator and denominator respectively can be standardized:
173
167
 
174
- quantity = (12.ft*8.mi)/(1.s*8.min)
175
- quantity.to_s => 12.0 ft mi/s min
176
- quantity.rationalize_units!
177
- quantity.to_s => 1056.0 ft²/s²
168
+ quantity = (12.ft*8.mi)/(1.s*8.min)
169
+ quantity.to_s #=> 12.0 ft mi/s min
170
+ quantity.rationalize_units!
171
+ quantity.to_s #=> 1056.0 ft²/s²
178
172
 
179
173
  A quantity with arbitrary cancelable units can be cancelled manually:
180
174
 
181
- quantity = (12.m**6) / 2.m**2
182
- quantity.to_s => "746496.0 m^6/m²"
183
- quantity.cancel_base_units! :m
184
- quantity.to_s => "746496.0 m^4"
185
-
186
- Note: there are more comprehensive and flexible methods for manupulating compound units available as part of of the class Unit::Compound. These can be used to convert a conpound unit into the precise form required. If such an approach is used, any quantity object can be converted to the new form by simply passing the new unit object into the Quantity#to method.
187
-
188
- Initializing unit objects
189
- -------------------------
190
-
191
- unit = Unit.for :km => <Quantify::Unit::SI:0xb75c9718 ... >
192
- unit = Unit.km => <Quantify::Unit::SI:0xb75c9718 ... >
193
- unit = Unit.kilometre => <Quantify::Unit::SI:0xb75c9718 ... >
194
-
195
- unit.name => 'kilometre'
196
- unit.symbol => 'kg'
197
- unit.label => 'kg'
198
- unit.dimensions => <Quantify::Dimensions:0xb75c4254 .. >
199
- unit.measures => 'length'
200
- unit.alternatives :name => ['metre','megametre','gigametre',
201
- 'terametre','angstrom','astronomical unit',
202
- 'baromil','chain','dram','ell','fathom',
203
- 'fermi','foot us survey','foot','furlong',
204
- 'hand','inch','nautical league',
205
- 'statute league','light year', 'line','link',
206
- 'yard']
207
-
208
- other_unit = Unit.hour
209
- other_unit.name => 'hour'
210
- other_unit.symbol => 'h'
211
- other_unit.measures => 'time'
212
- other_unit.alternatives :symbol => [ 's', 'ks', 'Ms', 'Gs', 'Ts', 'd', 'min' ]
213
-
214
- another_unit = unit / other_unit => <Quantify::Unit::Compound:0xb74af323 ... >
215
- another_unit.name => 'kilometer per hour'
216
- another_unit.symbol => 'km/h'
217
- another_unit.measures => 'velocity'
218
- another_unit.base_units.map(&:name) => ['kilogram','hour']
219
-
220
- last_unit = Unit.m
221
- last.unit.measures => 'length'
222
- square = last_unit ** 2 => <Quantify::Unit::Compound:0xb446f12f ... >
223
- square.symbol => 'm²'
224
- square.measures => 'area'
225
-
175
+ quantity = (12.m**6) / 2.m**2
176
+ quantity.to_s #=> "746496.0 m^6/m²"
177
+ quantity.cancel_base_units! :m
178
+ quantity.to_s #=> "746496.0 m^4"
226
179
 
180
+ Note: there are more comprehensive and flexible methods for manupulating compound units available as part of of the class Unit::Compound. These can be used to convert a conpound unit into the precise form required. If such an approach is used, any quantity object can be converted to the new form by simply passing the new unit object into the Quantity#to method.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.1.2
1
+ 3.1.3
@@ -1,4 +1,3 @@
1
-
2
1
  class Numeric
3
2
  # Syntactic sugar for defining instances of the Quantity class.
4
3
  #
@@ -10,7 +9,9 @@ class Numeric
10
9
  # 1000.t is equivalent to Quantity. new 1000, :t
11
10
  #
12
11
  def method_missing(method, *args, &block)
13
- if unit = Unit.for(method.to_s)
12
+ if (method == :to_str || method == :to_ary)
13
+ super
14
+ elsif unit = Unit.for(method.to_s)
14
15
  Quantify::Quantity.new self, unit
15
16
  else
16
17
  super
@@ -44,20 +44,47 @@ module Quantify
44
44
  # Parse a string and return a Quantity object based upon the value and
45
45
  # subseqent unit name, symbol or JScience label. Returns an array containing
46
46
  # quantity objects for each quantity recognised.
47
- def self.parse(string)
48
- words = string.words
49
- quantities = words.each_with_index.map do |word,index|
47
+ def self.parse(string,options={})
48
+
49
+ quantities = []
50
+ remainder = []
51
+ words = string.words
52
+
53
+ until words.empty? do
54
+ word = words.shift
50
55
  if word.starts_with_number?
51
- # Isolate number from subsequent string
52
- value, string = word, words[index+1..-1].join(" ")
53
- # Shift any trailing non-numeric characters to start of string.
54
- value, string = $1, "#{$2} #{string}" if (Unit::QUANTITY_REGEX).match(word)
55
- # Parse string for unit references
56
- unit = Unit.parse(string, :iterative => true) || Unit.dimensionless
57
- # Instantiate quantity using value and unit
58
- Quantity.new(value,unit)
56
+ if (Unit::QUANTITY_REGEX).match(word)
57
+ word, other = $1, $2
58
+ words.unshift(other)
59
+ end
60
+ quantities << [word]
61
+ else
62
+ if quantities.empty?
63
+ remainder << word
64
+ else
65
+ quantities.last << word
66
+ end
59
67
  end
68
+ end
69
+
70
+ remainders = []
71
+ remainders << remainder.join(" ")
72
+
73
+ quantities.map! do |words|
74
+
75
+ value = words.shift
76
+ string = words.join(" ")
77
+
78
+ # Parse string for unit references
79
+ unit, remainder = Unit.parse(string, :iterative => true, :remainder => true)
80
+ unit, remainder = Unit.dimensionless, string if unit.nil?
81
+ remainders << remainder
82
+
83
+ # Instantiate quantity using value and unit
84
+ Quantity.new(value,unit)
85
+
60
86
  end.compact
87
+ return [quantities, remainders] if options[:remainder] == true
61
88
  return quantities
62
89
  rescue Quantify::Exceptions::InvalidArgumentError
63
90
  raise Quantify::Exceptions::QuantityParseError, "Cannot parse string into value and unit"
@@ -66,7 +93,16 @@ module Quantify
66
93
  def self.configure(&block)
67
94
  self.class_eval(&block) if block
68
95
  end
96
+
97
+ def self.auto_consolidate_units=(true_or_false)
98
+ @auto_consolidate_units = true_or_false
99
+ end
69
100
 
101
+ def self.auto_consolidate_units?
102
+ @auto_consolidate_units.nil? ? false : @auto_consolidate_units
103
+ end
104
+ #alias :auto_consolidate_units? :auto_consolidate_units
105
+
70
106
  protected
71
107
 
72
108
  def self.is_basic_conversion_with_scalings?(quantity,new_unit)
@@ -281,6 +317,7 @@ module Quantify
281
317
  return self
282
318
  elsif other.kind_of? Quantity
283
319
  @unit = @unit.send(operator,other.unit).or_equivalent
320
+ @unit.consolidate_base_units! if @unit.is_compound_unit? && Quantity.auto_consolidate_units?
284
321
  @value = @value.send(operator,other.value)
285
322
  return self
286
323
  else
@@ -146,7 +146,7 @@ module Quantify
146
146
  end
147
147
 
148
148
  # Consilidates base quantities by finding multiple instances of the same unit
149
- # type and reducing them into a single unit represenation, by altering the
149
+ # type and reducing them into a single unit representation, by altering the
150
150
  # repsective index. It has the effect of raising units to powers and cancelling
151
151
  # those which appear in the numerator AND denominator
152
152
  #
@@ -247,10 +247,22 @@ module Quantify
247
247
  #
248
248
  def self.parse(string, options={})
249
249
  string = string.remove_underscores.without_superscript_characters
250
- units = options[:iterative] == true ? Unit.iterative_parse(string) : Unit.simple_parse(string)
251
- return nil if units.empty?
252
- return units.first.unit if units.size == 1 && units.first.index == 1
253
- return Unit::Compound.new(*units)
250
+ if options[:iterative] == true
251
+ units = Unit.iterative_parse(string, options)
252
+ units, remainder = units if options[:remainder] == true
253
+ else
254
+ units = Unit.simple_parse(string)
255
+ end
256
+
257
+ if units.empty?
258
+ units = nil
259
+ elsif units.size == 1 && units.first.index == 1
260
+ units = units.first.unit
261
+ else
262
+ units = Unit::Compound.new(*units)
263
+ end
264
+
265
+ options[:iterative] == true && options[:iterative] == true ? [units, remainder] : units
254
266
  end
255
267
 
256
268
  # This returns the suite of units which represents THE SI units for each of
@@ -320,7 +332,7 @@ module Quantify
320
332
  return units
321
333
  end
322
334
 
323
- def self.iterative_parse(string)
335
+ def self.iterative_parse(string, options={})
324
336
  units=[]
325
337
  is_denominator = false
326
338
  current_exponent = nil
@@ -341,6 +353,7 @@ module Quantify
341
353
  match_length = term.size
342
354
  string = string[match_length, string.length-match_length].strip
343
355
  end
356
+ return [units, string] if options[:remainder] == true
344
357
  return units
345
358
  end
346
359
 
data/quantify.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "quantify"
8
- s.version = "3.1.2"
8
+ s.version = "3.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andrew Berkeley"]
12
- s.date = "2012-01-18"
12
+ s.date = "2012-05-01"
13
13
  s.description = "A gem to support physical quantities and unit conversions"
14
14
  s.email = "andrew.berkeley.is@googlemail.com"
15
15
  s.extra_rdoc_files = [
@@ -54,7 +54,7 @@ Gem::Specification.new do |s|
54
54
  s.homepage = "https://github.com/spatchcock/quantify"
55
55
  s.licenses = ["MIT"]
56
56
  s.require_paths = ["lib"]
57
- s.rubygems_version = "1.8.15"
57
+ s.rubygems_version = "1.8.17"
58
58
  s.summary = "Support for handling physical quantities, unit conversions, etc"
59
59
 
60
60
  if s.respond_to? :specification_version then
@@ -249,6 +249,22 @@ describe Quantity do
249
249
  quantities.first.value.should == 10
250
250
  quantities.first.unit.symbol.should == 'mi/h'
251
251
  end
252
+
253
+ it "should create a valid instance with class parse method and return remainders" do
254
+ quantities = Quantity.parse "I sprayed 500L of fertilizer across 6000m^2 of farmland", :remainder => true
255
+ quantities.first.should be_a Array
256
+ quantities.first[0].value.should == 500
257
+ quantities.first[0].unit.name.should == 'litre'
258
+ quantities.first[0].unit.symbol.should == 'L'
259
+ quantities.first[1].value.should == 6000
260
+ quantities.first[1].unit.pluralized_name.should == 'square metres'
261
+ quantities.first[1].unit.symbol.should == 'm²'
262
+ quantities[1].should be_a Array
263
+ quantities[1].size.should eql 3
264
+ quantities[1][0].should eql "I sprayed"
265
+ quantities[1][1].should eql "of fertilizer across"
266
+ quantities[1][2].should eql "of farmland"
267
+ end
252
268
 
253
269
  it "should parse using string method" do
254
270
  "20 m".to_q.first.value.should == 20.0
@@ -525,13 +541,13 @@ describe Quantity do
525
541
  end
526
542
 
527
543
  it "should return between value from range" do
528
- (2.ft..20.ft).send(RUBY_VERSION < "1.9" ? :include? : :cover?, 3.ft).should be_true
544
+ (2.ft..20.ft).cover?(3.ft).should be_true
529
545
  end
530
546
 
531
547
  it "should return between value from range with different units" do
532
- (2.ft..4.m).send(RUBY_VERSION < "1.9" ? :include? : :cover?, 200.cm).should be_true
533
- (1.ly..1.parsec).send(RUBY_VERSION < "1.9" ? :include? : :cover?, 2.ly).should be_true
534
- (1.ly..1.parsec).send(RUBY_VERSION < "1.9" ? :include? : :cover?, 2.in).should be_false
548
+ (2.ft..4.m).cover?(200.cm).should be_true
549
+ (1.ly..1.parsec).cover?(2.ly).should be_true
550
+ (1.ly..1.parsec).cover?(2.in).should be_false
535
551
  end
536
552
 
537
553
  it "should return between value from range using === operator" do
@@ -551,5 +567,45 @@ describe Quantity do
551
567
  it "range comparison with non quantity should raise error" do
552
568
  lambda{20.ft === (1.ft..3)}.should raise_error
553
569
  end
570
+
571
+ it "should return unit consolidation setting" do
572
+ Quantity.auto_consolidate_units?.should be_false
573
+ end
574
+
575
+ it "should set unit consolidation setting" do
576
+ Quantity.auto_consolidate_units?.should be_false
577
+
578
+ Quantity.auto_consolidate_units=true
579
+ Quantity.auto_consolidate_units?.should be_true
580
+
581
+ Quantity.auto_consolidate_units=false
582
+ Quantity.auto_consolidate_units?.should be_false
583
+ end
584
+
585
+ it "should return non-consolidated units if consolidation disabled" do
586
+ quantity = 20.L * 1.km * (5.lb / 1.L)
587
+ quantity.to_s.should eql "100.0 L km lb/L"
588
+ end
589
+
590
+ it "should return equivalent units if consolidation disabled" do
591
+ quantity = 20.L * (5.lb / 1.L)
592
+ quantity.to_s.should eql "100.0 lb"
593
+ end
594
+
595
+ it "should return equivalent units if consolidation enabled" do
596
+ Quantity.auto_consolidate_units=true
597
+ quantity = 20.L * (5.lb / 1.L)
598
+ quantity.to_s.should eql "100.0 lb"
599
+ end
600
+
601
+ it "should return consolidated units if enabled" do
602
+ Quantity.auto_consolidate_units=true
603
+ quantity = 20.L * 1.km * (5.lb / 1.L)
604
+ quantity.to_s.should eql "100.0 km lb"
605
+ end
606
+
607
+ it "should return consolidated units if enabled" do
608
+
609
+ end
554
610
  end
555
611
 
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: 7
4
+ hash: 5
5
5
  prerelease:
6
6
  segments:
7
7
  - 3
8
8
  - 1
9
- - 2
10
- version: 3.1.2
9
+ - 3
10
+ version: 3.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andrew Berkeley
@@ -15,10 +15,12 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-01-18 00:00:00 Z
18
+ date: 2012-05-01 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: activesupport
22
+ prerelease: false
23
+ type: :runtime
22
24
  requirement: &id001 !ruby/object:Gem::Requirement
23
25
  none: false
24
26
  requirements:
@@ -29,11 +31,11 @@ dependencies:
29
31
  - 3
30
32
  - 0
31
33
  version: "3.0"
32
- type: :runtime
33
- prerelease: false
34
34
  version_requirements: *id001
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: i18n
37
+ prerelease: false
38
+ type: :runtime
37
39
  requirement: &id002 !ruby/object:Gem::Requirement
38
40
  none: false
39
41
  requirements:
@@ -43,11 +45,11 @@ dependencies:
43
45
  segments:
44
46
  - 0
45
47
  version: "0"
46
- type: :runtime
47
- prerelease: false
48
48
  version_requirements: *id002
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: bundler
51
+ prerelease: false
52
+ type: :development
51
53
  requirement: &id003 !ruby/object:Gem::Requirement
52
54
  none: false
53
55
  requirements:
@@ -59,11 +61,11 @@ dependencies:
59
61
  - 0
60
62
  - 0
61
63
  version: 1.0.0
62
- type: :development
63
- prerelease: false
64
64
  version_requirements: *id003
65
65
  - !ruby/object:Gem::Dependency
66
66
  name: jeweler
67
+ prerelease: false
68
+ type: :development
67
69
  requirement: &id004 !ruby/object:Gem::Requirement
68
70
  none: false
69
71
  requirements:
@@ -75,11 +77,11 @@ dependencies:
75
77
  - 6
76
78
  - 4
77
79
  version: 1.6.4
78
- type: :development
79
- prerelease: false
80
80
  version_requirements: *id004
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: rspec
83
+ prerelease: false
84
+ type: :development
83
85
  requirement: &id005 !ruby/object:Gem::Requirement
84
86
  none: false
85
87
  requirements:
@@ -91,11 +93,11 @@ dependencies:
91
93
  - 6
92
94
  - 0
93
95
  version: 2.6.0
94
- type: :development
95
- prerelease: false
96
96
  version_requirements: *id005
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rcov
99
+ prerelease: false
100
+ type: :development
99
101
  requirement: &id006 !ruby/object:Gem::Requirement
100
102
  none: false
101
103
  requirements:
@@ -105,11 +107,11 @@ dependencies:
105
107
  segments:
106
108
  - 0
107
109
  version: "0"
108
- type: :development
109
- prerelease: false
110
110
  version_requirements: *id006
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: rdoc
113
+ prerelease: false
114
+ type: :development
113
115
  requirement: &id007 !ruby/object:Gem::Requirement
114
116
  none: false
115
117
  requirements:
@@ -119,8 +121,6 @@ dependencies:
119
121
  segments:
120
122
  - 0
121
123
  version: "0"
122
- type: :development
123
- prerelease: false
124
124
  version_requirements: *id007
125
125
  description: A gem to support physical quantities and unit conversions
126
126
  email: andrew.berkeley.is@googlemail.com
@@ -194,7 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
194
194
  requirements: []
195
195
 
196
196
  rubyforge_project:
197
- rubygems_version: 1.8.15
197
+ rubygems_version: 1.8.17
198
198
  signing_key:
199
199
  specification_version: 3
200
200
  summary: Support for handling physical quantities, unit conversions, etc