alchemist 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/Rakefile +1 -1
  2. data/lib/alchemist.rb +81 -5
  3. metadata +2 -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.1.1'
9
+ gem_spec.version = '0.1.2'
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'
data/lib/alchemist.rb CHANGED
@@ -244,19 +244,20 @@ module Alchemist
244
244
  },
245
245
  :temperature => {
246
246
  :kelvin => 1.0, :K => 1.0,
247
+
247
248
  :celsius => [Proc.new{ |t| t + 273.15 }, Proc.new{ |t| t - 273.15 }], :centrigrade => [Proc.new{ |t| t + 273.15 }, Proc.new{ |t| t - 273.15 }],
248
249
  :degree_celsius => [Proc.new{ |t| t + 273.15 }, Proc.new{ |t| t - 273.15 }], :degree_centrigrade => [Proc.new{ |t| t + 273.15 }, Proc.new{ |t| t - 273.15 }],
249
250
  :degrees_celsius => [Proc.new{ |t| t + 273.15 }, Proc.new{ |t| t - 273.15 }], :degrees_centrigrade => [Proc.new{ |t| t + 273.15 }, Proc.new{ |t| t - 273.15 }],
250
- :fahrenheit => [Proc.new{ |t| t * (5.0/9.0) + 459.67 }, Proc.new{ |t| t * (9.0/5.0) - 459.67 }],
251
- :degree_fahrenheit => [Proc.new{ |t| t * (5.0/9.0) + 459.67 }, Proc.new{ |t| t * (9.0/5.0) - 459.67 }],
252
- :degrees_fahrenheit => [Proc.new{ |t| t * (5.0/9.0) + 459.67 }, Proc.new{ |t| t * (9.0/5.0) - 459.67 }],
251
+ :fahrenheit => [Proc.new{ |t| (t + 459.67) * (5.0/9.0) }, Proc.new{ |t| t * (9.0/5.0) - 459.67 }],
252
+ :degree_fahrenheit => [Proc.new{ |t| (t + 459.67) * (5.0/9.0) }, Proc.new{ |t| t * (9.0/5.0) - 459.67 }],
253
+ :degrees_fahrenheit => [Proc.new{ |t| (t + 459.67) * (5.0/9.0) }, Proc.new{ |t| t * (9.0/5.0) - 459.67 }],
253
254
  :rankine => 1.8, :rankines => 1.8
254
255
  },
255
256
  :time => {
256
257
  :second => 1.0, :seconds => 1.0, :s => 1.0,
257
258
  :minute => 60.0, :minutes => 60.0, :min => 60.0,
258
259
  :sidereal_minute => 5.983617, :sidereal_minutes => 5.983617,
259
- :hour => 3600.0, :hours => 3600.0, :hr => 3600.0,
260
+ :hour => 3600.0, :hours => 3600.0, :hr => 3600.0, :h => 3600.0,
260
261
  :sidereal_hour => 3.590170e+3, :sidereal_hours => 3.590170e+3,
261
262
  :day => 86400.0, :days => 86400.0,
262
263
  :sidereal_day => 8.616409e+4, :sidereal_days => 8.616409e+4,
@@ -343,9 +344,69 @@ module Alchemist
343
344
  @@operator_actions
344
345
  end
345
346
 
347
+ class CompoundNumericConversion
348
+ attr_accessor :numerators, :denominators
349
+ def initialize(numerator)
350
+ @coefficient = 1 #* numerator.to_f
351
+ @numerators = [numerator]
352
+ @denominators = []
353
+ end
354
+ def *(value)
355
+ case value
356
+ when Numeric
357
+ @coefficient *= value
358
+ self
359
+ when Alchemist::NumericConversion
360
+ @numerators << value
361
+ return consolidate
362
+ end
363
+ end
364
+
365
+ def consolidate
366
+ @numerators.each_with_index do |numerator, n|
367
+ @denominators.each_with_index do |denominator, d|
368
+ next if numerator.is_a?(Numeric)
369
+ next if denominator.is_a?(Numeric)
370
+ if (Conversions[numerator.unit_name] & Conversions[denominator.unit_name]).length > 0
371
+ value = numerator / denominator
372
+ @numerators.delete_at(n)
373
+ @denominators.delete_at(d)
374
+ @coefficient *= value
375
+ end
376
+ end
377
+ end
378
+ if @denominators.length == 0 && @numerators.length == 1
379
+ @numerators[0] * @coefficient
380
+ elsif @denominators.length == 0 && @numerators.length == 0
381
+ @coefficient
382
+ else
383
+ self
384
+ end
385
+ end
386
+
387
+ def to_s
388
+
389
+ end
390
+
391
+ def method_missing(method, *attrs, &block)
392
+ if Conversions[method]
393
+ @denominators << 1.send(method)
394
+ consolidate
395
+ end
396
+ end
397
+ end
398
+
346
399
  class NumericConversion
347
400
  include Comparable
348
401
 
402
+ def per
403
+ Alchemist::CompoundNumericConversion.new(self)
404
+ end
405
+
406
+ def p
407
+ per
408
+ end
409
+
349
410
  def to(type = nil)
350
411
  unless type
351
412
  self
@@ -371,10 +432,18 @@ module Alchemist
371
432
  @value.to_s
372
433
  end
373
434
 
435
+ def value
436
+ @value
437
+ end
438
+
374
439
  def to_f
375
440
  @value
376
441
  end
377
442
 
443
+ def ==(other)
444
+ self <=> other
445
+ end
446
+
378
447
  def <=>(other)
379
448
  (self.to_f * @exponent).to_f <=> other.to(@unit_name).to_f
380
449
  end
@@ -409,7 +478,14 @@ module Alchemist
409
478
  end
410
479
  end
411
480
  end
412
- raise Exception, "Incompatible Types" if unit_name == :*
481
+ if unit_name == :*
482
+ if args[0].is_a?(Numeric)
483
+ @value *= args[0]
484
+ return self
485
+ else
486
+ raise Exception, "Incompatible Types"
487
+ end
488
+ end
413
489
  if unit_name == :/ && args[0].is_a?(NumericConversion)
414
490
  raise Exception, "Incompatible Types" unless (Conversions[@unit_name] & Conversions[args[0].unit_name]).length > 0
415
491
  end
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.1.1
4
+ version: 0.1.2
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-18 00:00:00 -05:00
12
+ date: 2010-01-01 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15