quantitymanager 0.2.0

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.
Files changed (236) hide show
  1. data/doc/classes/Array.html +159 -0
  2. data/doc/classes/Array.src/M000001.html +21 -0
  3. data/doc/classes/Array.src/M000002.html +19 -0
  4. data/doc/classes/Array.src/M000040.html +21 -0
  5. data/doc/classes/Array.src/M000041.html +19 -0
  6. data/doc/classes/CalculatorTest.html +332 -0
  7. data/doc/classes/CalculatorTest.src/M000022.html +33 -0
  8. data/doc/classes/CalculatorTest.src/M000023.html +28 -0
  9. data/doc/classes/CalculatorTest.src/M000024.html +25 -0
  10. data/doc/classes/CalculatorTest.src/M000025.html +34 -0
  11. data/doc/classes/CalculatorTest.src/M000026.html +27 -0
  12. data/doc/classes/CalculatorTest.src/M000027.html +28 -0
  13. data/doc/classes/CalculatorTest.src/M000028.html +26 -0
  14. data/doc/classes/CalculatorTest.src/M000029.html +25 -0
  15. data/doc/classes/CalculatorTest.src/M000030.html +26 -0
  16. data/doc/classes/CalculatorTest.src/M000031.html +31 -0
  17. data/doc/classes/CalculatorTest.src/M000032.html +23 -0
  18. data/doc/classes/CalculatorTest.src/M000033.html +23 -0
  19. data/doc/classes/CalculatorTest.src/M000034.html +32 -0
  20. data/doc/classes/CalculatorTest.src/M000035.html +20 -0
  21. data/doc/classes/CompasedUnitTest.html +279 -0
  22. data/doc/classes/CompasedUnitTest.src/M000001.html +32 -0
  23. data/doc/classes/CompasedUnitTest.src/M000002.html +31 -0
  24. data/doc/classes/CompasedUnitTest.src/M000003.html +28 -0
  25. data/doc/classes/CompasedUnitTest.src/M000004.html +28 -0
  26. data/doc/classes/CompasedUnitTest.src/M000005.html +31 -0
  27. data/doc/classes/CompasedUnitTest.src/M000006.html +23 -0
  28. data/doc/classes/CompasedUnitTest.src/M000007.html +30 -0
  29. data/doc/classes/CompasedUnitTest.src/M000008.html +24 -0
  30. data/doc/classes/CompasedUnitTest.src/M000009.html +27 -0
  31. data/doc/classes/CompasedUnitTest.src/M000010.html +24 -0
  32. data/doc/classes/Configuration.html +184 -0
  33. data/doc/classes/Configuration.src/M000003.html +19 -0
  34. data/doc/classes/Configuration.src/M000004.html +18 -0
  35. data/doc/classes/Configuration.src/M000005.html +18 -0
  36. data/doc/classes/Configuration.src/M000006.html +24 -0
  37. data/doc/classes/Configuration.src/M000042.html +19 -0
  38. data/doc/classes/Configuration.src/M000043.html +18 -0
  39. data/doc/classes/Configuration.src/M000044.html +18 -0
  40. data/doc/classes/Configuration.src/M000045.html +24 -0
  41. data/doc/classes/ConfigurationTest.html +182 -0
  42. data/doc/classes/ConfigurationTest.src/M000065.html +31 -0
  43. data/doc/classes/ConfigurationTest.src/M000066.html +27 -0
  44. data/doc/classes/ConfigurationTest.src/M000067.html +35 -0
  45. data/doc/classes/ConfigurationTest.src/M000068.html +24 -0
  46. data/doc/classes/CustomUnitConversionTest.html +212 -0
  47. data/doc/classes/CustomUnitConversionTest.src/M000052.html +31 -0
  48. data/doc/classes/CustomUnitConversionTest.src/M000053.html +25 -0
  49. data/doc/classes/CustomUnitConversionTest.src/M000054.html +20 -0
  50. data/doc/classes/CustomUnitConversionTest.src/M000055.html +26 -0
  51. data/doc/classes/CustomUnitConversionTest.src/M000056.html +26 -0
  52. data/doc/classes/CustomUnitConversionTest.src/M000057.html +24 -0
  53. data/doc/classes/Numeric.html +125 -0
  54. data/doc/classes/QuantifiableTest.html +189 -0
  55. data/doc/classes/QuantifiableTest.src/M000036.html +24 -0
  56. data/doc/classes/QuantifiableTest.src/M000037.html +26 -0
  57. data/doc/classes/QuantifiableTest.src/M000038.html +41 -0
  58. data/doc/classes/QuantifiableTest.src/M000039.html +23 -0
  59. data/doc/classes/Quantity/Calculable.html +213 -0
  60. data/doc/classes/Quantity/Calculable.src/M000010.html +18 -0
  61. data/doc/classes/Quantity/Calculable.src/M000011.html +18 -0
  62. data/doc/classes/Quantity/Calculable.src/M000012.html +18 -0
  63. data/doc/classes/Quantity/Calculable.src/M000013.html +18 -0
  64. data/doc/classes/Quantity/Calculable.src/M000014.html +21 -0
  65. data/doc/classes/Quantity/Calculable.src/M000015.html +18 -0
  66. data/doc/classes/Quantity/Calculable.src/M000072.html +18 -0
  67. data/doc/classes/Quantity/Calculable.src/M000073.html +18 -0
  68. data/doc/classes/Quantity/Calculable.src/M000074.html +18 -0
  69. data/doc/classes/Quantity/Calculable.src/M000075.html +18 -0
  70. data/doc/classes/Quantity/Calculable.src/M000076.html +21 -0
  71. data/doc/classes/Quantity/Calculable.src/M000077.html +18 -0
  72. data/doc/classes/Quantity/Calculator.html +240 -0
  73. data/doc/classes/Quantity/Calculator.src/M000021.html +20 -0
  74. data/doc/classes/Quantity/Calculator.src/M000022.html +18 -0
  75. data/doc/classes/Quantity/Calculator.src/M000023.html +26 -0
  76. data/doc/classes/Quantity/Calculator.src/M000024.html +23 -0
  77. data/doc/classes/Quantity/Calculator.src/M000025.html +31 -0
  78. data/doc/classes/Quantity/Calculator.src/M000026.html +25 -0
  79. data/doc/classes/Quantity/Calculator.src/M000083.html +20 -0
  80. data/doc/classes/Quantity/Calculator.src/M000084.html +18 -0
  81. data/doc/classes/Quantity/Calculator.src/M000085.html +26 -0
  82. data/doc/classes/Quantity/Calculator.src/M000086.html +23 -0
  83. data/doc/classes/Quantity/Calculator.src/M000087.html +31 -0
  84. data/doc/classes/Quantity/Calculator.src/M000088.html +25 -0
  85. data/doc/classes/Quantity/Measurable.html +153 -0
  86. data/doc/classes/Quantity/Measurable.src/M000008.html +18 -0
  87. data/doc/classes/Quantity/Measurable.src/M000009.html +18 -0
  88. data/doc/classes/Quantity/Measurable.src/M000070.html +18 -0
  89. data/doc/classes/Quantity/Measurable.src/M000071.html +18 -0
  90. data/doc/classes/Quantity/OperationInfo.html +184 -0
  91. data/doc/classes/Quantity/OperationInfo.src/M000027.html +24 -0
  92. data/doc/classes/Quantity/OperationInfo.src/M000028.html +21 -0
  93. data/doc/classes/Quantity/OperationInfo.src/M000089.html +24 -0
  94. data/doc/classes/Quantity/OperationInfo.src/M000090.html +21 -0
  95. data/doc/classes/Quantity/Quantifiable.html +131 -0
  96. data/doc/classes/Quantity/Quantifiable.src/M000007.html +18 -0
  97. data/doc/classes/Quantity/Quantifiable.src/M000069.html +18 -0
  98. data/doc/classes/Quantity/Quantity.html +211 -0
  99. data/doc/classes/Quantity/Quantity.src/M000016.html +20 -0
  100. data/doc/classes/Quantity/Quantity.src/M000017.html +20 -0
  101. data/doc/classes/Quantity/Quantity.src/M000018.html +20 -0
  102. data/doc/classes/Quantity/Quantity.src/M000078.html +20 -0
  103. data/doc/classes/Quantity/Quantity.src/M000079.html +20 -0
  104. data/doc/classes/Quantity/Quantity.src/M000080.html +20 -0
  105. data/doc/classes/Quantity/QuantityInfo.html +179 -0
  106. data/doc/classes/Quantity/QuantityInfo.src/M000019.html +19 -0
  107. data/doc/classes/Quantity/QuantityInfo.src/M000020.html +18 -0
  108. data/doc/classes/Quantity/QuantityInfo.src/M000081.html +19 -0
  109. data/doc/classes/Quantity/QuantityInfo.src/M000082.html +18 -0
  110. data/doc/classes/Quantity.html +121 -0
  111. data/doc/classes/QuantityTest.html +220 -0
  112. data/doc/classes/QuantityTest.src/M000046.html +32 -0
  113. data/doc/classes/QuantityTest.src/M000047.html +33 -0
  114. data/doc/classes/QuantityTest.src/M000048.html +27 -0
  115. data/doc/classes/QuantityTest.src/M000049.html +20 -0
  116. data/doc/classes/QuantityTest.src/M000050.html +22 -0
  117. data/doc/classes/QuantityTest.src/M000051.html +20 -0
  118. data/doc/classes/SimpleUnitTest.html +234 -0
  119. data/doc/classes/SimpleUnitTest.src/M000058.html +24 -0
  120. data/doc/classes/SimpleUnitTest.src/M000059.html +25 -0
  121. data/doc/classes/SimpleUnitTest.src/M000060.html +21 -0
  122. data/doc/classes/SimpleUnitTest.src/M000061.html +20 -0
  123. data/doc/classes/SimpleUnitTest.src/M000062.html +26 -0
  124. data/doc/classes/SimpleUnitTest.src/M000063.html +21 -0
  125. data/doc/classes/SimpleUnitTest.src/M000064.html +18 -0
  126. data/doc/classes/SpecParserTest.html +174 -0
  127. data/doc/classes/SpecParserTest.src/M000019.html +25 -0
  128. data/doc/classes/SpecParserTest.src/M000020.html +32 -0
  129. data/doc/classes/SpecParserTest.src/M000021.html +31 -0
  130. data/doc/classes/Test.html +101 -0
  131. data/doc/classes/Unit/Comparable.html +156 -0
  132. data/doc/classes/Unit/Comparable.src/M000030.html +22 -0
  133. data/doc/classes/Unit/Comparable.src/M000031.html +22 -0
  134. data/doc/classes/Unit/Comparable.src/M000092.html +22 -0
  135. data/doc/classes/Unit/Comparable.src/M000093.html +22 -0
  136. data/doc/classes/Unit/ComposedUnit.html +301 -0
  137. data/doc/classes/Unit/ComposedUnit.src/M000044.html +22 -0
  138. data/doc/classes/Unit/ComposedUnit.src/M000045.html +25 -0
  139. data/doc/classes/Unit/ComposedUnit.src/M000046.html +25 -0
  140. data/doc/classes/Unit/ComposedUnit.src/M000047.html +22 -0
  141. data/doc/classes/Unit/ComposedUnit.src/M000048.html +29 -0
  142. data/doc/classes/Unit/ComposedUnit.src/M000049.html +24 -0
  143. data/doc/classes/Unit/ComposedUnit.src/M000050.html +25 -0
  144. data/doc/classes/Unit/ComposedUnit.src/M000051.html +18 -0
  145. data/doc/classes/Unit/ComposedUnit.src/M000107.html +22 -0
  146. data/doc/classes/Unit/ComposedUnit.src/M000108.html +25 -0
  147. data/doc/classes/Unit/ComposedUnit.src/M000109.html +25 -0
  148. data/doc/classes/Unit/ComposedUnit.src/M000110.html +22 -0
  149. data/doc/classes/Unit/ComposedUnit.src/M000111.html +29 -0
  150. data/doc/classes/Unit/ComposedUnit.src/M000112.html +24 -0
  151. data/doc/classes/Unit/ComposedUnit.src/M000113.html +25 -0
  152. data/doc/classes/Unit/ComposedUnit.src/M000114.html +18 -0
  153. data/doc/classes/Unit/Optimizer.html +152 -0
  154. data/doc/classes/Unit/Optimizer.src/M000042.html +44 -0
  155. data/doc/classes/Unit/Optimizer.src/M000043.html +18 -0
  156. data/doc/classes/Unit/Optimizer.src/M000105.html +44 -0
  157. data/doc/classes/Unit/Optimizer.src/M000106.html +18 -0
  158. data/doc/classes/Unit/Parser.html +176 -0
  159. data/doc/classes/Unit/Parser.src/M000039.html +18 -0
  160. data/doc/classes/Unit/Parser.src/M000040.html +25 -0
  161. data/doc/classes/Unit/Parser.src/M000041.html +19 -0
  162. data/doc/classes/Unit/Parser.src/M000102.html +18 -0
  163. data/doc/classes/Unit/Parser.src/M000103.html +25 -0
  164. data/doc/classes/Unit/Parser.src/M000104.html +19 -0
  165. data/doc/classes/Unit/SimpleUnit.html +287 -0
  166. data/doc/classes/Unit/SimpleUnit.src/M000032.html +20 -0
  167. data/doc/classes/Unit/SimpleUnit.src/M000033.html +20 -0
  168. data/doc/classes/Unit/SimpleUnit.src/M000034.html +20 -0
  169. data/doc/classes/Unit/SimpleUnit.src/M000035.html +20 -0
  170. data/doc/classes/Unit/SimpleUnit.src/M000036.html +18 -0
  171. data/doc/classes/Unit/SimpleUnit.src/M000037.html +21 -0
  172. data/doc/classes/Unit/SimpleUnit.src/M000038.html +18 -0
  173. data/doc/classes/Unit/SimpleUnit.src/M000094.html +20 -0
  174. data/doc/classes/Unit/SimpleUnit.src/M000095.html +20 -0
  175. data/doc/classes/Unit/SimpleUnit.src/M000096.html +20 -0
  176. data/doc/classes/Unit/SimpleUnit.src/M000097.html +20 -0
  177. data/doc/classes/Unit/SimpleUnit.src/M000098.html +18 -0
  178. data/doc/classes/Unit/SimpleUnit.src/M000099.html +21 -0
  179. data/doc/classes/Unit/SimpleUnit.src/M000100.html +18 -0
  180. data/doc/classes/Unit/SymbolConverter.html +131 -0
  181. data/doc/classes/Unit/SymbolConverter.src/M000029.html +24 -0
  182. data/doc/classes/Unit/SymbolConverter.src/M000091.html +24 -0
  183. data/doc/classes/Unit/TestCase.html +146 -0
  184. data/doc/classes/Unit/TestCase.src/M000101.html +24 -0
  185. data/doc/classes/Unit.html +120 -0
  186. data/doc/classes/UnitOptemizerTest.html +249 -0
  187. data/doc/classes/UnitOptemizerTest.src/M000011.html +31 -0
  188. data/doc/classes/UnitOptemizerTest.src/M000012.html +25 -0
  189. data/doc/classes/UnitOptemizerTest.src/M000013.html +27 -0
  190. data/doc/classes/UnitOptemizerTest.src/M000014.html +27 -0
  191. data/doc/classes/UnitOptemizerTest.src/M000015.html +27 -0
  192. data/doc/classes/UnitOptemizerTest.src/M000016.html +26 -0
  193. data/doc/classes/UnitOptemizerTest.src/M000017.html +23 -0
  194. data/doc/classes/UnitOptemizerTest.src/M000018.html +22 -0
  195. data/doc/created.rid +1 -0
  196. data/doc/files/config/default_configuration_rb.html +108 -0
  197. data/doc/files/quantity_rb.html +113 -0
  198. data/doc/files/test/all_tests_rb.html +108 -0
  199. data/doc/files/test/test_helper_rb.html +110 -0
  200. data/doc/files/test/unit/calculator_test_rb.html +109 -0
  201. data/doc/files/test/unit/composed_unit_test_rb.html +101 -0
  202. data/doc/files/test/unit/configuration_test_rb.html +109 -0
  203. data/doc/files/test/unit/custom_conversion_test_rb.html +109 -0
  204. data/doc/files/test/unit/quantifiable_test_rb.html +101 -0
  205. data/doc/files/test/unit/quantity_test_rb.html +101 -0
  206. data/doc/files/test/unit/simple_unit_test_rb.html +101 -0
  207. data/doc/files/test/unit/unit_optimizer_test_rb.html +101 -0
  208. data/doc/files/test/unit/unit_spec_parser_test_rb.html +101 -0
  209. data/doc/files/unitmanager/calculator_rb.html +101 -0
  210. data/doc/files/unitmanager/configuration_rb.html +101 -0
  211. data/doc/files/unitmanager/quantity_rb.html +101 -0
  212. data/doc/files/unitmanager/simple_unit_rb.html +108 -0
  213. data/doc/files/unitmanager/unit_composition_rb.html +101 -0
  214. data/doc/fr_class_index.html +44 -0
  215. data/doc/fr_file_index.html +33 -0
  216. data/doc/fr_method_index.html +77 -0
  217. data/doc/index.html +24 -0
  218. data/lib/config/default_configuration.rb +115 -0
  219. data/lib/quantity.rb +34 -0
  220. data/lib/unitmanager/calculator.rb +195 -0
  221. data/lib/unitmanager/configuration.rb +26 -0
  222. data/lib/unitmanager/quantity.rb +167 -0
  223. data/lib/unitmanager/simple_unit.rb +155 -0
  224. data/lib/unitmanager/unit_composition.rb +217 -0
  225. data/test/all_tests.rb +8 -0
  226. data/test/calculator_test.rb +194 -0
  227. data/test/composed_unit_test.rb +169 -0
  228. data/test/configuration_test.rb +70 -0
  229. data/test/custom_conversion_test.rb +90 -0
  230. data/test/quantifiable_test.rb +73 -0
  231. data/test/quantity_test.rb +79 -0
  232. data/test/simple_unit_test.rb +59 -0
  233. data/test/test_helper.rb +18 -0
  234. data/test/unit_optimizer_test.rb +104 -0
  235. data/test/unit_spec_parser_test.rb +50 -0
  236. metadata +307 -0
data/doc/index.html ADDED
@@ -0,0 +1,24 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
5
+
6
+ <!--
7
+
8
+ RDoc Documentation
9
+
10
+ -->
11
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
12
+ <head>
13
+ <title>RDoc Documentation</title>
14
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
15
+ </head>
16
+ <frameset rows="20%, 80%">
17
+ <frameset cols="25%,35%,45%">
18
+ <frame src="fr_file_index.html" title="Files" name="Files" />
19
+ <frame src="fr_class_index.html" name="Classes" />
20
+ <frame src="fr_method_index.html" name="Methods" />
21
+ </frameset>
22
+ <frame src="files/config/default_configuration_rb.html" name="docwin" />
23
+ </frameset>
24
+ </html>
@@ -0,0 +1,115 @@
1
+ # Default configuration file defines default set of units and conversions
2
+ # to be used by concrete Calculator instances.
3
+
4
+ $configuration = Configuration.new() {|config|
5
+
6
+ # Electrical Units
7
+ config.unit :unit => :A
8
+ config.unit :unit => :mA, :based_on => :A, :coefficient => 0.001
9
+ config.unit :unit => :mkA, :based_on => :A, :coefficient => 0.000001
10
+ config.unit :unit => :kA, :based_on => :A, :coefficient => 1000
11
+ config.unit :unit => :C
12
+ config.unit :unit => :mC, :based_on => :C, :coefficient => 0.001
13
+ config.unit :unit => :mkC, :based_on => :C, :coefficient => 0.000001
14
+ config.unit :unit => :kC, :based_on => :C, :coefficient => 1000
15
+ config.unit :unit => :V
16
+ config.unit :unit => :mV, :based_on => :V, :coefficient => 0.001
17
+ config.unit :unit => :mkV, :based_on => :V, :coefficient => 0.000001
18
+ config.unit :unit => :kV, :based_on => :V, :coefficient => 1000
19
+ config.unit :unit => :Ohm
20
+ config.unit :unit => :mOhm, :based_on => :Ohm, :coefficient => 0.001
21
+ config.unit :unit => :mkOhm, :based_on => :Ohm, :coefficient => 0.000001
22
+ config.unit :unit => :kOhm, :based_on => :Ohm, :coefficient => 1000
23
+ config.unit :unit => :S
24
+ config.unit :unit => :mS, :based_on => :S, :coefficient => 0.001
25
+ config.unit :unit => :mkS, :based_on => :S, :coefficient => 0.000001
26
+ config.unit :unit => :kS, :based_on => :S, :coefficient => 1000
27
+ config.unit :unit => :F
28
+ config.unit :unit => :mF, :based_on => :F, :coefficient => 0.001
29
+ config.unit :unit => :mkF, :based_on => :F, :coefficient => 0.000001
30
+ config.unit :unit => :H
31
+ config.unit :unit => :mH, :based_on => :H, :coefficient => 0.001
32
+ config.unit :unit => :mkH, :based_on => :H, :coefficient => 0.000001
33
+ config.unit :unit => :kH, :based_on => :H, :coefficient => 1000
34
+ config.unit :unit => :J
35
+ config.unit :unit => :mJ, :based_on => :J, :coefficient => 0.001
36
+ config.unit :unit => :mkJ, :based_on => :J, :coefficient => 0.000001
37
+ config.unit :unit => :kJ, :based_on => :J, :coefficient => 1000
38
+ config.unit :unit => :W
39
+ config.unit :unit => :kW, :based_on => :W, :coefficient => 1000
40
+ config.unit :unit => :kWh
41
+ config.unit :unit => :Hz
42
+ config.unit :unit => :mHz, :based_on => :Hz, :coefficient => 0.001
43
+ config.unit :unit => :mkHz, :based_on => :Hz, :coefficient => 0.000001
44
+ config.unit :unit => :kHz, :based_on => :Hz, :coefficient => 1000
45
+ config.unit :unit => :rad
46
+
47
+ # Units of Length
48
+ config.unit :unit => :mm
49
+ config.unit :unit => :cm, :based_on => :mm, :coefficient => 10
50
+ config.unit :unit => :dm, :based_on => :cm, :coefficient => 10
51
+ config.unit :unit => :m, :based_on => :cm, :coefficient => 100
52
+ config.unit :unit => :km, :based_on => :m, :coefficient => 1000
53
+ config.unit :unit => :in, :based_on => :cm, :coefficient => 2.54
54
+ config.unit :unit => :ft, :based_on => :in, :coefficient => 12
55
+ config.unit :unit => :yd, :based_on => :ft, :coefficient => 3
56
+ config.unit :unit => :mi, :based_on => :yd, :coefficient => 1760
57
+ config.unit :unit => :rd, :based_on => :in, :coefficient => 198
58
+
59
+ # Units of Mass -->
60
+ config.unit :unit => :g
61
+ config.unit :unit => :kg, :based_on => :g, :coefficient => 1000
62
+ config.unit :unit => :oz, :based_on => :g, :coefficient => 28.349523125
63
+ config.unit :unit => :lb, :based_on => :oz, :coefficient => 16
64
+ config.unit :unit => :mg, :based_on => :g, :coefficient => 0.001
65
+
66
+ # Units of Capacity or Volume -->
67
+ config.unit :unit => :L
68
+ config.unit :unit => :mL, :based_on => :L, :coefficient => 0.001
69
+ config.unit :unit => :cu_dm, :based_on => :L, :coefficient => 1
70
+ config.unit :unit => :cu_cm, :based_on => :L, :coefficient => 0.001
71
+ config.unit :unit => :cu_in, :based_on => :L, :coefficient => 0.016387064
72
+ config.unit :unit => :cu_ft, :based_on => :cu_in, :coefficient => 1728
73
+ config.unit :unit => :cu_yd, :based_on => :cu_ft, :coefficient => 27
74
+ config.unit :unit => :cu_mm, :based_on => :cu_cm, :coefficient => 0.001
75
+ config.unit :unit => :cu_m, :based_on => :L, :coefficient => 1000
76
+ config.unit :unit => :gal, :based_on => :L, :coefficient => 3.785332
77
+ config.unit :unit => :qt, :based_on => :gal, :coefficient => 0.25
78
+
79
+ # Units of time -->
80
+ config.unit :unit => :sec
81
+ config.unit :unit => :min, :based_on => :sec, :coefficient => 60
82
+ config.unit :unit => :hour, :based_on => :min, :coefficient => 60
83
+ config.unit :unit => :day, :based_on => :hour, :coefficient => 24
84
+
85
+ # Units of Area -->
86
+ config.unit :unit => :sq_mm
87
+ config.unit :unit => :sq_cm, :based_on => :sq_mm, :coefficient => 100
88
+ config.unit :unit => :sq_m, :based_on => :sq_cm, :coefficient => 10000
89
+ config.unit :unit => :ha, :based_on => :sq_m, :coefficient => 10000
90
+ config.unit :unit => :sq_in, :based_on => :sq_cm, :coefficient => 6.4516
91
+ config.unit :unit => :sq_ft, :based_on => :sq_in, :coefficient => 144
92
+ config.unit :unit => :acre, :based_on => :sq_ft, :coefficient => 43560
93
+ # Units of Preassure -->
94
+ config.unit :unit => :Pa
95
+ config.unit :unit => :bar, :based_on => :Pa, :coefficient => 100000
96
+ config.unit :unit => :kPa, :based_on => :Pa, :coefficient => 1000
97
+ config.unit :unit => :hPa, :based_on => :Pa, :coefficient => 100
98
+ config.unit :unit => :mbar, :based_on => :hPa, :coefficient => 1
99
+ config.unit :unit => :atm, :based_on => :Pa, :coefficient => 101325
100
+ config.unit :unit => :mm_Hg
101
+
102
+ #Define conversions
103
+
104
+ config.conversion 1.sq_mm => 1.mm * 1.mm
105
+ config.conversion 1.cu_mm => 1.mm * 1.mm * 1.mm
106
+ config.conversion 1.Hz => 1 / 1.sec
107
+
108
+ config.conversion 1.C => 1.F * 1.V
109
+ config.conversion 1.W => 1.A * 1.V
110
+ config.conversion 1.V => 1.A * 1.Ohm
111
+ config.conversion 1.W => 1.J / 1.sec
112
+ config.conversion 1.Hz => 6.28318530718.rad / 1.sec
113
+ config.conversion 1.Pa => 0.000145038.lb / 1.sq_in
114
+
115
+ }
data/lib/quantity.rb ADDED
@@ -0,0 +1,34 @@
1
+ #--
2
+ # Copyright (c) 2006 Alexander Alon Portnov
3
+ #
4
+ # alex.portnov@gmail.com
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining
7
+ # a copy of this software and associated documentation files (the
8
+ # "Software"), to deal in the Software without restriction, including
9
+ # without limitation the rights to use, copy, modify, merge, publish,
10
+ # distribute, sublicense, and/or sell copies of the Software, and to
11
+ # permit persons to whom the Software is furnished to do so, subject to
12
+ # the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be
15
+ # included in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ #++
25
+
26
+ $:.unshift(File.dirname(__FILE__))
27
+
28
+ require "unitmanager/simple_unit"
29
+ require "unitmanager/unit_composition"
30
+ require "unitmanager/quantity"
31
+ require "unitmanager/calculator"
32
+ require "unitmanager/configuration"
33
+
34
+ require "config/default_configuration"
@@ -0,0 +1,195 @@
1
+ module Quantity
2
+
3
+ class Calculator
4
+ include Quantifiable
5
+ include Unit::SymbolConverter
6
+
7
+ attr_reader :units, :conversions
8
+
9
+ def initialize(units = {}, conversions = [])
10
+ @units = units
11
+ @parser ||= Unit::Parser.new(@units)
12
+ @conversions ||= conversions
13
+ end
14
+
15
+ def quantity(value, unit_sym)
16
+ return Quantity.new(self, {:value => 1.0 * value, :unit => result_unit(unit_sym)})
17
+ end
18
+
19
+ def perform_operation(first_operand, second_operand, operation)
20
+ result = case operation
21
+ when :+ then add(first_operand, second_operand)
22
+ when :- then subtract(first_operand, second_operand)
23
+ when :* then multiply(first_operand, second_operand)
24
+ when :/ then divide(first_operand, second_operand)
25
+ else
26
+ raise "Operation #{operation.to_s} is not supported"
27
+ end
28
+ return result
29
+ end
30
+
31
+ def exp (return_as = nil)
32
+
33
+ operand = yield self
34
+ raise "Operand is not Quantifiable" unless quantifiable?(operand)
35
+
36
+
37
+ convert_to(operand.to_quantity(self), result_unit(return_as))
38
+ end
39
+
40
+ def add_unit(params)
41
+ raise "Unit symbol is required" unless (unit_sym = params[:unit])
42
+
43
+ raise "Unit with symbol: #{unit_sym.to_s} already defined" if @units[unit_sym]
44
+
45
+ if (based_on_sym = params[:based_on])
46
+ raise "Invalid configuration. " +
47
+ "Based_on unit must be defined before used." unless (based_on = @units[based_on_sym])
48
+ end
49
+
50
+ @units[unit_sym] = Unit::SimpleUnit.new({
51
+ :unit => unit_sym,
52
+ :based_on => based_on,
53
+ :coefficient => params[:coefficient] || 1.0
54
+ })
55
+ end
56
+
57
+ def add_conversion(params)
58
+ params.each {|source, target|
59
+ begin
60
+ @conversions << exp { target / source }
61
+ @conversions << exp { source / target }
62
+ rescue
63
+ raise "Unit must be defined before used in conversion"
64
+ end
65
+ }
66
+ end
67
+
68
+ private
69
+
70
+ def add (first_operand, second_operand)
71
+ converted_second_operand = convert_to(second_operand, first_operand.unit)
72
+
73
+ value = first_operand.value + converted_second_operand.value
74
+
75
+ Quantity.new(self,{:value => value, :unit => first_operand.unit})
76
+ end
77
+
78
+ def subtract (first_operand, second_operand)
79
+ converted_second_operand = convert_to(second_operand, first_operand.unit)
80
+
81
+ value = first_operand.value - converted_second_operand.value
82
+
83
+ Quantity.new(self,{:value => value, :unit => first_operand.unit})
84
+ end
85
+
86
+ def multiply (first_operand, second_operand)
87
+ value = first_operand.value * second_operand.value
88
+
89
+ unit = Unit::ComposedUnit.new({
90
+ :dividends =>
91
+ convert(first_operand[:dividends] + second_operand[:dividends]),
92
+ :divisors =>
93
+ convert(first_operand[:divisors] + second_operand[:divisors])
94
+ })
95
+
96
+ Quantity.new(self, {:value => value, :unit => unit})
97
+ end
98
+
99
+ def divide (first_operand, second_operand)
100
+ value = second_operand.value == 0 ? 0 : first_operand.value / second_operand.value
101
+
102
+ unit = Unit::ComposedUnit.new({
103
+ :dividends =>
104
+ convert(first_operand[:dividends] + second_operand[:divisors]),
105
+ :divisors =>
106
+ convert(first_operand[:divisors] + second_operand[:dividends])
107
+ })
108
+
109
+ Quantity.new(self, {:value => value, :unit => unit})
110
+ end
111
+
112
+ def convert_to(qty, unit)
113
+ unit = qty.unit.h_base if unit.nil?
114
+
115
+ return qty if qty.unit == unit
116
+
117
+ if qty.unit.compatible?(unit)
118
+ value = unit.from_base(qty.unit.to_base(qty.value))
119
+ return Quantity.new(self, {:value => value, :unit => unit})
120
+ end
121
+
122
+ if (reverse = exp {1 / qty} ).unit.compatible?(unit)
123
+ return convert_to(reverse, unit)
124
+ end
125
+
126
+ if conversion = find_conversion( :from => qty.unit, :to => unit)
127
+ return convert_to(exp {qty * conversion}, unit)
128
+ end
129
+
130
+ if conversion = find_conversion( :from => reverse.unit, :to => unit)
131
+ return convert_to(exp {reverse * conversion}, unit)
132
+ end
133
+
134
+ raise "Incompatible Units: #{qty[:string]} and #{unit[:string]} " unless conversion
135
+ end
136
+
137
+ def result_unit(return_spec)
138
+ return nil if return_spec.nil?
139
+
140
+ if quantifiable?(return_spec)
141
+ qty = return_spec.to_quantity(self)
142
+ return qty.unit
143
+ end
144
+
145
+ if return_spec.respond_to?(:split)
146
+ return Unit::ComposedUnit.new(@parser.to_units(return_spec))
147
+ end
148
+
149
+ unit = @units[return_spec]
150
+ return unit if unit
151
+
152
+ raise "Return Unit Expression Error: Invalid Unit Specification "
153
+ end
154
+
155
+ def find_conversion (params)
156
+
157
+ from, to = params[:from], params[:to]
158
+
159
+ target = Unit::ComposedUnit.new({
160
+ :dividends => convert(to[:dividends] + from[:divisors]),
161
+ :divisors => convert(to[:divisors] + from[:dividends])
162
+ })
163
+
164
+ if (conversion = direct_conversion(target))
165
+ return conversion
166
+ end
167
+
168
+ return composed_conversion(target)
169
+ end
170
+
171
+ def direct_conversion(target)
172
+ @conversions.each {|c|
173
+ return exp(target[:string]) {c} if c.unit == target.h_base
174
+ }
175
+ nil
176
+ end
177
+
178
+ def composed_conversion(target)
179
+ convs, conv = [], Quantity.new(self, :unit => target, :value => 1)
180
+ @conversions.each {|c|
181
+ if conv.unit.contains?(c.unit.dividend)
182
+ convs << c; conv /= c
183
+ end
184
+
185
+ if conv.unit[:dividends] == [] && conv.unit[:divisors] == []
186
+ conv = 1
187
+ convs.each {|c| conv *= c}
188
+ return conv
189
+ end
190
+ }
191
+ nil
192
+ end
193
+ end
194
+
195
+ end
@@ -0,0 +1,26 @@
1
+ class Configuration
2
+
3
+ def initialize
4
+ @default = Quantity::Calculator.new
5
+ yield self if block_given?
6
+ end
7
+
8
+ def unit (params)
9
+ @default.add_unit params
10
+ end
11
+
12
+ def conversion (params)
13
+ @default.add_conversion params
14
+ end
15
+
16
+ def calc
17
+ calc = Quantity::Calculator.new({}.replace(@default.units))
18
+
19
+ @default.conversions.each {|c|
20
+ calc.conversions << Quantity::Quantity.new(calc, :unit => c.unit, :value => c.value)
21
+ }
22
+
23
+ return calc
24
+ end
25
+
26
+ end
@@ -0,0 +1,167 @@
1
+ module Quantity
2
+
3
+ module Quantifiable
4
+ def quantifiable? (value)
5
+ value && value.respond_to?(:to_quantity)
6
+ end
7
+ end
8
+
9
+ module Calculable
10
+ include Quantifiable
11
+
12
+ def + (value)
13
+ perform_operation(value, :+)
14
+ end
15
+
16
+ def - (value)
17
+ perform_operation(value, :-)
18
+ end
19
+
20
+ def * (value)
21
+ perform_operation(value, :*)
22
+ end
23
+
24
+ def / (value)
25
+ perform_operation(value, :/)
26
+ end
27
+
28
+ def == (other)
29
+ return false if other == nil
30
+ return false unless other.kind_of?(self.class)
31
+
32
+ value == other.value && unit == other.unit
33
+ end
34
+
35
+ def coerce(other)
36
+ [QuantityInfo.new(other, nil), self]
37
+ end
38
+ private
39
+
40
+ def perform_operation(value, operation)
41
+ raise "Operand is not Quantifiable" unless quantifiable?(value)
42
+
43
+ if value.respond_to?(:calc)
44
+ return apply_operator(to_quantity(value.calc), value, operation)
45
+ end
46
+
47
+ OperationInfo.new(self, value, operation)
48
+ end
49
+
50
+ def apply_operator(first_operand, second_operand, operation)
51
+ result = case operation
52
+ when :+ then first_operand + second_operand
53
+ when :- then first_operand - second_operand
54
+ when :* then first_operand * second_operand
55
+ when :/ then first_operand / second_operand
56
+ end
57
+ return result
58
+ end
59
+
60
+ end
61
+
62
+
63
+ # DSL (domain specific language) processing module. Mixin for Numeric class.
64
+ module Measurable
65
+
66
+ def method_missing (method_name)
67
+ QuantityInfo.new(self, method_name)
68
+ end
69
+
70
+ def to_quantity (calc)
71
+ calc.quantity(self, nil)
72
+ end
73
+ end
74
+
75
+ class QuantityInfo
76
+
77
+ include Calculable
78
+
79
+ attr_reader :value, :unit_sym
80
+
81
+ def initialize(value, unit_sym)
82
+ @value = value
83
+ @unit_sym = unit_sym
84
+ end
85
+
86
+ def to_quantity(calc)
87
+ calc.quantity(@value, @unit_sym)
88
+ end
89
+
90
+ end
91
+
92
+ class OperationInfo
93
+ include Calculable
94
+
95
+ attr_reader :first_operand, :second_operand, :operation
96
+
97
+ def initialize(first_operand, second_operand, operation)
98
+
99
+ raise "Operand is not Quantifiable" unless quantifiable?(first_operand) &&
100
+ quantifiable?(second_operand)
101
+
102
+ @first_operand = first_operand
103
+ @second_operand = second_operand
104
+ @operation = operation
105
+ end
106
+
107
+ def to_quantity(calc)
108
+ first_operand = @first_operand.to_quantity(calc)
109
+ second_operand = @second_operand.to_quantity(calc)
110
+
111
+ return apply_operator(first_operand, second_operand, @operation)
112
+ end
113
+
114
+ end
115
+
116
+
117
+ class Quantity
118
+
119
+ include Calculable
120
+
121
+ attr_reader :calc, :value, :unit
122
+
123
+ def initialize(calc, params)
124
+ @calc = calc
125
+ @unit = params[:unit] || Unit::ComposedUnit.new({})
126
+ @value = params[:value] || 0.0
127
+ end
128
+
129
+ def to_quantity(calc)
130
+ raise "Incompatible Argument." +
131
+ "Both operands must be created by the same calculator" unless @calc == calc
132
+ self
133
+ end
134
+
135
+ # This method allows to retrieve symbolic portions of the quantity unit definition
136
+ # and quantity value.
137
+ # Supported values are:
138
+ # :dividends - returns an array of symbols representing dividend part of
139
+ # unit definition.
140
+ # :divisors - returns an array of symbols representing divisor part of
141
+ # unit definition.
142
+ # :string - string representation of the unit of measure.
143
+ # :value - returns quantity value
144
+ def [] (symbol)
145
+ return value if symbol == :value
146
+
147
+ @unit[symbol]
148
+ end
149
+
150
+ private
151
+
152
+ def perform_operation(value, operation)
153
+ raise "Operand is not Quantifiable" unless quantifiable?(value)
154
+
155
+ @calc.perform_operation(self, value.to_quantity(@calc), operation)
156
+ end
157
+
158
+ end
159
+
160
+ end
161
+
162
+ # Extention to standard Numeric hierarchy to support quantity DSL
163
+ class Numeric
164
+ include Quantity::Measurable
165
+ end
166
+
167
+