alchemist 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +0,0 @@
1
- module Alchemist
2
- module Conversion
3
- def method_missing unit_name, *args, &block
4
- exponent, unit_name = Alchemist.parse_prefix(unit_name)
5
- Alchemist.measurement_for(unit_name) || super( unit_name, *args, &block )
6
- Alchemist.measurement self, unit_name, exponent
7
- end
8
- end
9
- end
10
-
11
- class Numeric
12
- include Alchemist::Conversion
13
- end
@@ -1,13 +0,0 @@
1
- module Alchemist
2
- def self.si_units
3
- %w[
4
- m meter metre meters metres liter litre litres liters l L
5
- farad farads F coulombs C gray grays Gy siemen siemens S
6
- mhos mho ohm ohms volt volts V joule joules J newton
7
- newtons N lux lx henry henrys H b B bits bytes bit byte
8
- lumen lumens lm candela candelas cd tesla teslas T gauss
9
- Gs G gram gramme grams grammes g watt watts W pascal
10
- pascals Pa becquerel becquerels Bq curie curies Ci
11
- ]
12
- end
13
- end
@@ -1,38 +0,0 @@
1
- module Alchemist
2
- def self.unit_prefixes
3
- {
4
- :googol => 1e+100,
5
- :yotta => 1e+24, :Y => 1e+24,
6
- :zetta => 1e+21, :Z => 1e+21,
7
- :exa => 1e+18, :E => 1e+18,
8
- :peta => 1e+15, :P => 1e+15,
9
- :tera => 1e+12, :T => 1e+12,
10
- :giga => 1e+9, :G => 1e+9,
11
- :mega => 1e+6, :M => 1e+6,
12
- :kilo => 1e+3, :k => 1e+3,
13
- :hecto => 1e+2, :h => 1e+2,
14
- :deca => 10, :da => 10,
15
- :deci => 1e-1, :d => 1e-1,
16
- :centi => 1e-2, :c => 1e-2,
17
- :milli => 1e-3, :m => 1e-3,
18
- :micro => 1e-6, :u => 1e-6,
19
- :nano => 1e-9, :n => 1e-9,
20
- :pico => 1e-12, :p => 1e-12,
21
- :femto => 1e-15, :f => 1e-15,
22
- :atto => 1e-18, :a => 1e-18,
23
- :zepto => 1e-21, :z => 1e-21,
24
- :yocto => 1e-24, :y => 1e-24,
25
-
26
- # binary prefixes
27
-
28
- :kibi => 2.0**10.0, :Ki => 2.0**10.0,
29
- :mebi => 2.0**20.0, :Mi => 2.0**20.0,
30
- :gibi => 2.0**30.0, :Gi => 2.0**30.0,
31
- :tebi => 2.0**40.0, :Ti => 2.0**40.0,
32
- :pebi => 2.0**50.0, :Pi => 2.0**50.0,
33
- :exbi => 2.0**60.0, :Ei => 2.0**60.0,
34
- :zebi => 2.0**70.0, :Zi => 2.0**70.0,
35
- :yobi => 2.0**80.0, :Yi => 2.0**80.0
36
- }
37
- end
38
- end
@@ -1,76 +0,0 @@
1
- require 'yaml'
2
- require 'alchemist/si_units'
3
- require 'alchemist/unit_prefixes'
4
- require 'alchemist/binary_prefixes'
5
-
6
- module Alchemist
7
- class ConversionTable
8
-
9
- def load_all
10
- @conversions ||= load_yaml.merge(proc_based)
11
- end
12
-
13
- private
14
-
15
- def load_yaml
16
- YAML.load_file(yaml_file)
17
- end
18
-
19
- def yaml_file
20
- File.join(File.dirname(__FILE__), "units.yml")
21
- end
22
-
23
- def proc_based
24
- {
25
- :density => {
26
- :specific_gravity => 1, :sg => 1,
27
- :brix => [lambda{ |d| -261.3 / (d - 261.3) }, lambda{ |d| 261.3 - (261.3 / d) }],
28
- :plato => [lambda{ |d| -260.0 / (d - 260.0) }, lambda{ |d| 260.0 - (260.0 / d) }],
29
- :baume => [lambda{ |d| -145.0 / (d - 145.0) }, lambda{ |d| 145.0 - (145.0 / d) }]
30
- },
31
- :temperature => temperature
32
- }
33
- end
34
-
35
- def temperature
36
- {
37
- :kelvin => 1.0, :K => 1.0,
38
-
39
- :celsius => celsius_conversion,
40
- :centrigrade => celsius_conversion,
41
- :degree_celsius => celsius_conversion,
42
- :degree_centrigrade => celsius_conversion,
43
- :degrees_celsius => celsius_conversion,
44
- :degrees_centrigrade => celsius_conversion,
45
- :fahrenheit => fahrenheit_conversion,
46
- :degree_fahrenheit => fahrenheit_conversion,
47
- :degrees_fahrenheit => fahrenheit_conversion,
48
- :rankine => 1.8, :rankines => 1.8
49
- }
50
- end
51
-
52
- def to_celsius
53
- lambda{ |t| t + 273.15 }
54
- end
55
-
56
- def from_celsius
57
- lambda{ |t| t - 273.15 }
58
- end
59
-
60
- def celsius_conversion
61
- [to_celsius, from_celsius]
62
- end
63
-
64
- def to_fahrenheit
65
- lambda{ |t| (t + 459.67) * (5.0/9.0) }
66
- end
67
-
68
- def from_fahrenheit
69
- lambda{ |t| t * (9.0/5.0) - 459.67 }
70
- end
71
-
72
- def fahrenheit_conversion
73
- [to_fahrenheit, from_fahrenheit]
74
- end
75
- end
76
- end
@@ -1,12 +0,0 @@
1
- $:.unshift(File.dirname(__FILE__) + '../lib')
2
-
3
- require 'test/unit'
4
- require 'alchemist'
5
-
6
- class AlchemistCompoundTest < Test::Unit::TestCase
7
- def test_compound
8
- assert_equal 30.miles, (10.miles.per.second * 3.seconds)
9
- assert_equal 30.km, (10.km.p.h * 3.hours)
10
- assert_equal 4, 4.m.per.m
11
- end
12
- end
@@ -1,13 +0,0 @@
1
- $:.unshift(File.dirname(__FILE__) + '../lib')
2
-
3
- require 'test/unit'
4
- require 'alchemist'
5
-
6
- class AlchemistMeasurementTest < Test::Unit::TestCase
7
-
8
- def test_measurement
9
- unit = Alchemist.measurement(1, :meter)
10
- assert_equal(unit, 1.meter)
11
- end
12
-
13
- end
@@ -1,98 +0,0 @@
1
- $:.unshift(File.dirname(__FILE__) + '../lib')
2
-
3
- require 'test/unit'
4
- require 'alchemist'
5
-
6
- # TODO: Remove this
7
- class Float
8
- # truncates float to specified decimal places
9
- def truncate(dec = 0)
10
- return to_i if dec == 0
11
- sprintf("%.#{dec}f", self).to_f
12
- end
13
- end
14
-
15
- class AlchemistTest < Test::Unit::TestCase
16
-
17
- def test_equivalence
18
- assert_equal(1.m, 1.meter)
19
- end
20
-
21
- def test_bit_and_bytes
22
- assert_equal( 65.bit.to_f, (1.bit + 8.bytes).to_f )
23
- assert_equal( 0.125.bytes.to_f, 1.bit.to.bytes.to_f )
24
- assert_in_delta(1.MB.to.kB.to_f, 1024.0, 1e-5)
25
- assert_in_delta(1.MB.to.b.to_f, 8388608.0, 1e-5)
26
- assert_in_delta(1.GB.to.B.to_f, 1073741824.0, 1e-5)
27
- assert_in_delta(1.MiB.to(:KiB).to_f, 1024.0, 1e-5)
28
- assert_in_delta(1.MiB.to.b.to_f, 8388608.0, 1e-5)
29
- assert_in_delta(1.GiB.to.B.to_f, 1073741824.0, 1e-5)
30
- Alchemist::use_si = true
31
- assert_in_delta(1.GB.to.B.to_f, 1000000000.0, 1e-5)
32
- assert_in_delta(1.MB.to.b.to_f, 8000000.0, 1e-5)
33
- assert_in_delta(1.MB.to.kB.to_f, 1000.0, 1e-5)
34
- end
35
-
36
- def test_feet_to_miles
37
- assert_equal( 5280.feet, 1.mile.to.feet )
38
- end
39
-
40
- def test_acre_to_yards_squared
41
- assert_in_delta( 4840.square_yards.to_f, 1.acre.to.square_yards.to_f, 1e-5)
42
- end
43
-
44
- def test_gallon_to_liter
45
- assert_in_delta( 3.785411784.L.to_f, 1.gallon.to.L.to_f, 1e-5 )
46
- end
47
-
48
- def test_lb_to_kg
49
- assert_equal( 0.45359237.kg.to_f, 1.lb.to.kg.to_f )
50
- end
51
-
52
- def test_comparison
53
- assert_equal( 5.grams, 0.005.kilograms )
54
- end
55
-
56
- def test_register
57
- Alchemist.register(:distance, [:beard_second, :beard_seconds], 5.angstroms)
58
- assert_equal( 1.beard_second, 5.angstroms)
59
- Alchemist.register(:temperature, :yeti, [Proc.new{|t| t + 1}, Proc.new{|t| t - 1}])
60
- assert_equal( 0.yeti, 1.kelvin)
61
- end
62
-
63
- def test_meters_times_meters
64
- assert_equal(1.meter * 1.meter, 1.square_meter)
65
- end
66
-
67
- def test_meters_times_meters_times_meters
68
- assert_equal(1.meter * 2.meter * 3.meter, 6.cubic_meters)
69
- assert_equal(2.square_meters * 3.meters, 6.cubic_meters)
70
- end
71
-
72
- def test_division
73
- assert_equal(2.meters / 1.meters, 2.0)
74
- end
75
-
76
- # Don't think these worked before
77
- # def test_temperature
78
- # assert_equal(1.fahrenheit, 1.fahrenheit)
79
- # assert_in_delta(1.fahrenheit, 1.fahrenheit.to.fahrenheit, 1e-5)
80
- # end
81
-
82
- def test_density
83
- assert_equal(25.brix.to_f, 1.1058.sg.to.brix.truncate(1))
84
- assert_equal(25.plato, 25.125.brix)
85
- end
86
-
87
- def test_frequency
88
- assert_equal(1.hertz, 60.rpm)
89
- assert_equal(60.hz, 3600.rpm)
90
- assert_equal(2400.rpm, 40.hz)
91
- end
92
-
93
- def test_conversion_to_datatypes
94
- assert_equal(10.meters.to_i, 10)
95
- assert_equal(10.meters.to_f, 10.0)
96
- assert_equal(10.meters.to_s, "10.0")
97
- end
98
- end