alchemist 0.0.9 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +15 -2
- data/lib/alchemist.rb +36 -3
- data/lib/alchemist/compound.rb +6 -0
- metadata +3 -2
data/Rakefile
CHANGED
@@ -6,7 +6,7 @@ PKG_FILES = %w(Rakefile) +
|
|
6
6
|
|
7
7
|
gem_spec = Gem::Specification.new do |gem_spec|
|
8
8
|
gem_spec.name = 'alchemist'
|
9
|
-
gem_spec.version = '0.0
|
9
|
+
gem_spec.version = '0.1.0'
|
10
10
|
gem_spec.summary = 'Conversions... like you\'ve never seen them before!'
|
11
11
|
gem_spec.description = 'Conversions... like you\'ve never seen them before!!'
|
12
12
|
gem_spec.email = 'matt@toastyapps.com'
|
@@ -20,4 +20,17 @@ task :gemspec do
|
|
20
20
|
File.open("#{gem_spec.name}.gemspec", "w") do |f|
|
21
21
|
f.write gem_spec.to_yaml
|
22
22
|
end
|
23
|
-
end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
task :default => [:test]
|
27
|
+
task :test => ['test:units']
|
28
|
+
|
29
|
+
namespace :test do
|
30
|
+
Rake::TestTask.new(:units) do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.ruby_opts << '-rubygems'
|
33
|
+
test.pattern = 'test/*.rb'
|
34
|
+
test.verbose = true
|
35
|
+
end
|
36
|
+
end
|
data/lib/alchemist.rb
CHANGED
@@ -8,6 +8,7 @@ module Alchemist
|
|
8
8
|
@@si_units += %w[joule joules J newton newtons N lux lx henry henrys H b B bits bytes bit byte lumen lumens lm candela candelas cd]
|
9
9
|
@@si_units += %w[tesla teslas T gauss Gs G gram gramme grams grammes g watt watts W pascal pascals Pa]
|
10
10
|
@@si_units += %w[becquerel becquerels Bq curie curies Ci]
|
11
|
+
@@operator_actions = {}
|
11
12
|
@@conversion_table = {
|
12
13
|
:absorbed_radiation_dose => {
|
13
14
|
:gray => 1.0, :grays => 1.0, :Gy => 1.0,
|
@@ -338,6 +339,10 @@ module Alchemist
|
|
338
339
|
@@conversion_table
|
339
340
|
end
|
340
341
|
|
342
|
+
def self.operator_actions
|
343
|
+
@@operator_actions
|
344
|
+
end
|
345
|
+
|
341
346
|
class NumericConversion
|
342
347
|
include Comparable
|
343
348
|
|
@@ -358,6 +363,10 @@ module Alchemist
|
|
358
363
|
end
|
359
364
|
end
|
360
365
|
|
366
|
+
def unit_name
|
367
|
+
@unit_name
|
368
|
+
end
|
369
|
+
|
361
370
|
def to_s
|
362
371
|
@value.to_s
|
363
372
|
end
|
@@ -367,7 +376,7 @@ module Alchemist
|
|
367
376
|
end
|
368
377
|
|
369
378
|
def <=>(other)
|
370
|
-
(self.to_f * @exponent) <=> other.to(@unit_name).to_f
|
379
|
+
(self.to_f * @exponent).to_f <=> other.to(@unit_name).to_f
|
371
380
|
end
|
372
381
|
|
373
382
|
private
|
@@ -391,9 +400,24 @@ module Alchemist
|
|
391
400
|
raise Exception, "Incompatible Types"
|
392
401
|
end
|
393
402
|
else
|
403
|
+
if args[0] && args[0].is_a?(NumericConversion) && Alchemist.operator_actions[unit_name]
|
404
|
+
t1 = Conversions[ @unit_name ][0]
|
405
|
+
t2 = Conversions[ args[0].unit_name ][0]
|
406
|
+
Alchemist.operator_actions[unit_name].each do |s1, s2, new_type|
|
407
|
+
if t1 == s1 && t2 == s2
|
408
|
+
return (@value * args[0].to_f).send(new_type)
|
409
|
+
end
|
410
|
+
end
|
411
|
+
end
|
412
|
+
raise Exception, "Incompatible Types" if unit_name == :*
|
413
|
+
if unit_name == :/ && args[0].is_a?(NumericConversion)
|
414
|
+
raise Exception, "Incompatible Types" unless (Conversions[@unit_name] & Conversions[args[0].unit_name]).length > 0
|
415
|
+
end
|
394
416
|
args.map!{|a| a.is_a?(NumericConversion) ? a.send(@unit_name).to_f / @exponent : a }
|
395
417
|
@value = @value.send( unit_name, *args, &block )
|
396
|
-
|
418
|
+
|
419
|
+
|
420
|
+
unit_name == :/ ? @value : self
|
397
421
|
end
|
398
422
|
end
|
399
423
|
end
|
@@ -414,6 +438,11 @@ module Alchemist
|
|
414
438
|
Alchemist.conversion_table[type][name] = value
|
415
439
|
end
|
416
440
|
end
|
441
|
+
|
442
|
+
def self.register_operation_conversions type, other_type, operation, converted_type
|
443
|
+
@@operator_actions[operation] ||= []
|
444
|
+
@@operator_actions[operation] << [type, other_type, converted_type]
|
445
|
+
end
|
417
446
|
|
418
447
|
def self.parse_prefix(unit)
|
419
448
|
@@unit_prefixes.each do |prefix, value|
|
@@ -437,4 +466,8 @@ end
|
|
437
466
|
|
438
467
|
class Numeric
|
439
468
|
include Alchemist
|
440
|
-
end
|
469
|
+
end
|
470
|
+
|
471
|
+
require 'alchemist/compound'
|
472
|
+
|
473
|
+
3.seconds * 3.seconds
|
@@ -0,0 +1,6 @@
|
|
1
|
+
Alchemist.register_operation_conversions(:distance, :distance, :*, :square_meters)
|
2
|
+
Alchemist.register_operation_conversions(:area, :distance, :*, :cubic_meters)
|
3
|
+
Alchemist.register_operation_conversions(:distance, :area, :*, :cubic_meters)
|
4
|
+
Alchemist.register_operation_conversions(:area, :distance, :/, :meters)
|
5
|
+
Alchemist.register_operation_conversions(:volume, :distance, :/, :square_meters)
|
6
|
+
Alchemist.register_operation_conversions(:volume, :area, :/, :meters)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: alchemist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Mongeau
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-11-
|
12
|
+
date: 2009-11-16 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -23,6 +23,7 @@ extra_rdoc_files: []
|
|
23
23
|
|
24
24
|
files:
|
25
25
|
- Rakefile
|
26
|
+
- lib/alchemist/compound.rb
|
26
27
|
- lib/alchemist.rb
|
27
28
|
has_rdoc: true
|
28
29
|
homepage: http://github.com/toastyapps/alchemist
|