quantitymanager 0.9.0 → 0.9.1

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/quantity.rb CHANGED
@@ -25,6 +25,7 @@
25
25
 
26
26
  $:.unshift(File.dirname(__FILE__))
27
27
 
28
+ require "unitmanager/utils"
28
29
  require "unitmanager/simple_unit"
29
30
  require "unitmanager/unit_composition"
30
31
  require "unitmanager/quantity"
@@ -2,7 +2,6 @@ module Quantity
2
2
 
3
3
  class Calculator
4
4
  include Quantifiable
5
- include Unit::SymbolConverter
6
5
 
7
6
  attr_reader :units, :conversions
8
7
 
@@ -87,9 +86,9 @@ module Quantity
87
86
 
88
87
  unit = Unit::ComposedUnit.new({
89
88
  :dividends =>
90
- convert(first_operand[:dividends] + second_operand[:dividends]),
89
+ @units.collect_for(first_operand[:dividends] + second_operand[:dividends]),
91
90
  :divisors =>
92
- convert(first_operand[:divisors] + second_operand[:divisors])
91
+ @units.collect_for(first_operand[:divisors] + second_operand[:divisors])
93
92
  })
94
93
 
95
94
  Quantity.new(self, {:value => value, :unit => unit})
@@ -100,9 +99,9 @@ module Quantity
100
99
 
101
100
  unit = Unit::ComposedUnit.new({
102
101
  :dividends =>
103
- convert(first_operand[:dividends] + second_operand[:divisors]),
102
+ @units.collect_for(first_operand[:dividends] + second_operand[:divisors]),
104
103
  :divisors =>
105
- convert(first_operand[:divisors] + second_operand[:dividends])
104
+ @units.collect_for(first_operand[:divisors] + second_operand[:dividends])
106
105
  })
107
106
 
108
107
  Quantity.new(self, {:value => value, :unit => unit})
@@ -156,8 +155,8 @@ module Quantity
156
155
  from, to = params[:from], params[:to]
157
156
 
158
157
  target = Unit::ComposedUnit.new({
159
- :dividends => convert(to[:dividends] + from[:divisors]),
160
- :divisors => convert(to[:divisors] + from[:dividends])
158
+ :dividends => @units.collect_for(to[:dividends] + from[:divisors]),
159
+ :divisors => @units.collect_for(to[:divisors] + from[:dividends])
161
160
  })
162
161
 
163
162
  if (conversion = direct_conversion(target))
@@ -1,64 +1,5 @@
1
- #Extension to standard Array class.
2
- #Added functionality to compare array content regardless element order.
3
- class Array
4
- def contains?(other_array)
5
- copy = dup
6
- other_array.each {|item|
7
- return false unless i = copy.index(item)
8
- copy[i] = nil
9
- }
10
- true
11
- end
12
-
13
- def same?(other_array)
14
- length == other_array.length &&
15
- contains?(other_array)
16
- end
17
-
18
- end
19
-
20
1
  module Unit
21
2
 
22
- class BaseUnit
23
-
24
- class << self
25
-
26
- def once (*methods) #:nodoc:
27
- methods.each {|method|
28
-
29
- module_eval <<-"end;"
30
- alias_method :__#{method.to_i}__ , :#{method.to_s}
31
-
32
- def #{method.to_s}(*args, &block)
33
-
34
- def self.#{method.to_s}(*args, &block)
35
- @__#{method.to_i}__
36
- end
37
-
38
- @__#{method.to_i}__ = __#{method.to_i}__(*args, &block)
39
- end
40
- private :__#{method.to_i}__
41
-
42
- end;
43
-
44
- }
45
- end
46
-
47
- private :once
48
- end
49
-
50
- # This method allows to retrieve symbolic portions of the unit definition.
51
- # Supported values are:
52
- # :dividends - returns an array of symbols representing dividend part of
53
- # unit definition.
54
- # :divisors - returns an array of symbols representing divisor part of
55
- # unit definition.
56
- # :string - string representation of the unit of measure.
57
- def [] (symbol)
58
- unit_sym[symbol]
59
- end
60
- end
61
-
62
3
  # Adds comparing capabilities to unit implementations
63
4
  module Comparable
64
5
 
@@ -87,7 +28,7 @@ module Unit
87
28
  # simple unit with linear coefficient.
88
29
  # Example: :cm is based on :mm with coefficient equal 10.
89
30
  # In other words X cm = 10 * Y mm.
90
- class SimpleUnit < BaseUnit
31
+ class SimpleUnit
91
32
 
92
33
  include Unit::Comparable
93
34
 
@@ -99,17 +40,16 @@ module Unit
99
40
  @unit = args[:unit]
100
41
  @based_on = args[:based_on] || nil
101
42
  @coefficient = args[:coefficient] || 1
43
+ @unit_sym = {:dividends => [@unit], :divisors => [], :string => @unit.to_s}
102
44
  end
103
45
 
104
46
  def to_base(value)
105
47
  value = @based_on.to_base(value) if derived?
106
-
107
48
  value *= @coefficient
108
49
  end
109
50
 
110
51
  def from_base(value)
111
52
  value = @based_on.from_base(value) if derived?
112
-
113
53
  value /= @coefficient
114
54
  end
115
55
 
@@ -140,16 +80,19 @@ module Unit
140
80
  self == other
141
81
  end
142
82
 
83
+ # This method allows to retrieve symbolic portions of the unit definition.
84
+ # Supported values are:
85
+ # :dividends - returns an array of symbols representing dividend part of
86
+ # unit definition.
87
+ # :string - string representation of the unit of measure.
88
+ def [] (symbol)
89
+ @unit_sym[symbol]
90
+ end
91
+
143
92
  private
144
93
  def derived?
145
94
  @based_on != nil
146
95
  end
147
-
148
- def unit_sym
149
- {:dividends => [@unit], :divisors => [], :string => @unit.to_s}
150
- end
151
-
152
- once :unit_sym
153
96
  end
154
97
 
155
98
  end
@@ -1,27 +1,14 @@
1
1
  module Unit
2
2
 
3
- module SymbolConverter
4
-
5
- def convert(symbols)
6
- units = []
7
- symbols.each {|symbol|
8
- unit = @units[symbol]
9
- raise "Invalid Unit Name: #{symbol.to_s}" if unit.nil?
10
- units << unit
11
- }
12
- return units
13
- end
14
-
15
- end
16
-
17
-
18
3
  # Implementation of what is called ‘composed unit of measure’.
19
4
  # This is a result of performing multiplication or division of
20
5
  # quantities with simple units.
21
6
  # Example: 1 lb per in or 70 mi per hour
22
- class ComposedUnit < BaseUnit
7
+ class ComposedUnit
23
8
 
24
9
  include Unit::Comparable
10
+
11
+ self.extend ExecControl
25
12
 
26
13
  attr_reader :dividends, :divisors, :coefficient
27
14
 
@@ -82,9 +69,20 @@ module Unit
82
69
  self == other
83
70
  end
84
71
 
72
+ # This method allows to retrieve symbolic portions of the unit definition.
73
+ # Supported values are:
74
+ # :dividends - returns an array of symbols representing dividend part of
75
+ # unit definition.
76
+ # :divisors - returns an array of symbols representing divisor part of
77
+ # unit definition.
78
+ # :string - string representation of the unit of measure.
79
+ def [] (symbol)
80
+ unit_sym[symbol]
81
+ end
82
+
85
83
  private
86
84
 
87
- def collect(array)
85
+ def collect(array, &block)
88
86
  result = []
89
87
 
90
88
  array.each {|item| result += (yield(item) || []) }
@@ -151,7 +149,6 @@ module Unit
151
149
 
152
150
 
153
151
  class Parser
154
- include SymbolConverter
155
152
 
156
153
  def initialize(units)
157
154
  @units = units
@@ -163,8 +160,8 @@ module Unit
163
160
  raise "Invalid Number of Operands in : #{unit_spec}" if operands.size > 2
164
161
 
165
162
  {
166
- :dividends => convert(parse_operand_symbols(operands[0])),
167
- :divisors => convert(parse_operand_symbols(operands[1]))
163
+ :dividends => @units.collect_for(parse_operand_symbols(operands[0])),
164
+ :divisors => @units.collect_for(parse_operand_symbols(operands[1]))
168
165
  }
169
166
  end
170
167
 
@@ -0,0 +1,63 @@
1
+ module ExecControl
2
+
3
+ def once (*methods) #:nodoc:
4
+ methods.each {|method|
5
+
6
+ module_eval <<-"end;"
7
+ alias_method :__#{method.to_i}__ , :#{method.to_s}
8
+
9
+ def #{method.to_s}(*args, &block)
10
+
11
+ def self.#{method.to_s}(*args, &block)
12
+ @__#{method.to_i}__
13
+ end
14
+
15
+ @__#{method.to_i}__ = __#{method.to_i}__(*args, &block)
16
+ end
17
+ private :__#{method.to_i}__
18
+
19
+ end;
20
+
21
+ }
22
+ end
23
+
24
+ private :once
25
+
26
+ end
27
+
28
+
29
+ class Hash
30
+
31
+ def collect_for(keys)
32
+ results = []
33
+
34
+ keys.each {|key|
35
+ value = self[key]
36
+ raise "Invalid Key : #{key.to_s}" if value.nil?
37
+ results << value
38
+ }
39
+ results
40
+ end
41
+
42
+ end
43
+
44
+
45
+ #Extension to standard Array class.
46
+ #Added functionality to compare array content regardless element order.
47
+ class Array
48
+ def contains?(other_array)
49
+ copy = dup
50
+ other_array.each {|item|
51
+ return false unless i = copy.index(item)
52
+ copy[i] = nil
53
+ }
54
+ true
55
+ end
56
+
57
+ def same?(other_array)
58
+ length == other_array.length &&
59
+ contains?(other_array)
60
+ end
61
+
62
+ end
63
+
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: quantitymanager
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.9.0
7
- date: 2006-06-06 00:00:00 -05:00
6
+ version: 0.9.1
7
+ date: 2006-07-19 00:00:00 -05:00
8
8
  summary: "Unit of measure, quantity framework"
9
9
  require_paths:
10
10
  - lib
@@ -34,6 +34,7 @@ files:
34
34
  - lib/quantity.rb
35
35
  - lib/config/default_configuration.rb
36
36
  - lib/unitmanager/configuration.rb
37
+ - lib/unitmanager/utils.rb
37
38
  - lib/unitmanager/quantity.rb
38
39
  - lib/unitmanager/simple_unit.rb
39
40
  - lib/unitmanager/unit_composition.rb