alchemist 0.1.2.1 → 0.1.3

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 (3) hide show
  1. data/Rakefile +6 -6
  2. data/lib/alchemist.rb +47 -42
  3. metadata +21 -44
data/Rakefile CHANGED
@@ -6,12 +6,12 @@ task :default => [:test]
6
6
  task :test => ['test:units']
7
7
 
8
8
  namespace :test do
9
- Rake::TestTask.new(:units) do |test|
10
- test.libs << 'test'
11
- test.ruby_opts << '-rubygems'
12
- test.pattern = 'test/*.rb'
13
- test.verbose = true
14
- end
9
+ Rake::TestTask.new(:units) do |test|
10
+ test.libs << 'test'
11
+ test.ruby_opts << '-rubygems'
12
+ test.pattern = 'test/*.rb'
13
+ test.verbose = true
14
+ end
15
15
  end
16
16
 
17
17
  eval("$specification = begin; #{IO.read('alchemist.gemspec')}; end")
data/lib/alchemist.rb CHANGED
@@ -3,7 +3,7 @@ module Alchemist
3
3
  class << self
4
4
  attr_accessor :use_si
5
5
  end
6
-
6
+
7
7
  @@si_units = %w[m meter metre meters metres liter litre litres liters l L farad farads F coulombs C gray grays Gy siemen siemens S mhos mho ohm ohms volt volts V ]
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]
@@ -29,7 +29,7 @@ module Alchemist
29
29
  },
30
30
  :area => {
31
31
  :square_meter => 1.0, :square_meters => 1.0, :square_metre => 1.0, :square_metres => 1.0,
32
- :acre => 4046.85642, :acres => 4046.85642,
32
+ :acre => 4046.85642, :acres => 4046.85642,
33
33
  :are => 1.0e+2, :ares => 1.0e+2, :a => 1.0e+2,
34
34
  :barn => 1.0e-28, :barns => 1.0e-28, :b => 1.0e-28,
35
35
  :circular_mil => 5.067075e-10, :circular_mils => 5.067075e-10,
@@ -143,6 +143,11 @@ module Alchemist
143
143
  #unusual measurements
144
144
  :foe => 1e+44, :foes => 1e+44
145
145
  },
146
+ :frequency => {
147
+ :hertz => 1.0, :hz => 1.0, :'Hz' => 1.0,
148
+ :revolutions_per_minute => 1/60.0, :rpm => 1/60.0,
149
+ :beats_per_minute => 1/60.0, :bpm => 1/60.0
150
+ },
146
151
  :force => {
147
152
  :newton => 1.0, :newtons => 1.0, :N => 1.0,
148
153
  :dyne => 1.0e-5, :dynes => 1.0e-5, :dyn => 1.0e-5,
@@ -250,7 +255,7 @@ module Alchemist
250
255
  },
251
256
  :temperature => {
252
257
  :kelvin => 1.0, :K => 1.0,
253
-
258
+
254
259
  :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 }],
255
260
  :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 }],
256
261
  :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 }],
@@ -258,7 +263,7 @@ module Alchemist
258
263
  :degree_fahrenheit => [Proc.new{ |t| (t + 459.67) * (5.0/9.0) }, Proc.new{ |t| t * (9.0/5.0) - 459.67 }],
259
264
  :degrees_fahrenheit => [Proc.new{ |t| (t + 459.67) * (5.0/9.0) }, Proc.new{ |t| t * (9.0/5.0) - 459.67 }],
260
265
  :rankine => 1.8, :rankines => 1.8
261
- },
266
+ },
262
267
  :time => {
263
268
  :second => 1.0, :seconds => 1.0, :s => 1.0,
264
269
  :minute => 60.0, :minutes => 60.0, :min => 60.0,
@@ -321,9 +326,9 @@ module Alchemist
321
326
  :atto => 1e-18, :a => 1e-18,
322
327
  :zepto => 1e-21, :z => 1e-21,
323
328
  :yocto => 1e-24, :y => 1e-24,
324
-
329
+
325
330
  # binary prefixes
326
-
331
+
327
332
  :kibi => 2.0**10.0, :Ki => 2.0**10.0,
328
333
  :mebi => 2.0**20.0, :Mi => 2.0**20.0,
329
334
  :gibi => 2.0**30.0, :Gi => 2.0**30.0,
@@ -333,23 +338,23 @@ module Alchemist
333
338
  :zebi => 2.0**70.0, :Zi => 2.0**70.0,
334
339
  :yobi => 2.0**80.0, :Yi => 2.0**80.0
335
340
  }
336
-
341
+
337
342
  def from(unit_name)
338
343
  send(unit_name)
339
344
  end
340
-
345
+
341
346
  def self.unit_prefixes
342
347
  @@unit_prefixes
343
348
  end
344
-
349
+
345
350
  def self.conversion_table
346
351
  @@conversion_table
347
352
  end
348
-
353
+
349
354
  def self.operator_actions
350
355
  @@operator_actions
351
356
  end
352
-
357
+
353
358
  class CompoundNumericConversion
354
359
  attr_accessor :numerators, :denominators
355
360
  def initialize(numerator)
@@ -359,7 +364,7 @@ module Alchemist
359
364
  end
360
365
  def *(value)
361
366
  case value
362
- when Numeric
367
+ when Numeric
363
368
  @coefficient *= value
364
369
  self
365
370
  when Alchemist::NumericConversion
@@ -367,7 +372,7 @@ module Alchemist
367
372
  return consolidate
368
373
  end
369
374
  end
370
-
375
+
371
376
  def consolidate
372
377
  @numerators.each_with_index do |numerator, n|
373
378
  @denominators.each_with_index do |denominator, d|
@@ -389,11 +394,11 @@ module Alchemist
389
394
  self
390
395
  end
391
396
  end
392
-
397
+
393
398
  def to_s
394
-
399
+
395
400
  end
396
-
401
+
397
402
  def method_missing(method, *attrs, &block)
398
403
  if Conversions[method]
399
404
  @denominators << 1.send(method)
@@ -401,27 +406,27 @@ module Alchemist
401
406
  end
402
407
  end
403
408
  end
404
-
409
+
405
410
  class NumericConversion
406
411
  include Comparable
407
-
412
+
408
413
  def per
409
414
  Alchemist::CompoundNumericConversion.new(self)
410
415
  end
411
-
416
+
412
417
  def p
413
418
  per
414
419
  end
415
-
420
+
416
421
  def to(type = nil)
417
422
  unless type
418
- self
423
+ self
419
424
  else
420
425
  send(type)
421
426
  end
422
427
  end
423
428
  alias_method :as, :to
424
-
429
+
425
430
  def base(unit_type)
426
431
  if(Alchemist.conversion_table[unit_type][@unit_name].is_a?(Array))
427
432
  @exponent * Alchemist.conversion_table[unit_type][@unit_name][0].call(@value)
@@ -429,41 +434,41 @@ module Alchemist
429
434
  @exponent * @value * Alchemist.conversion_table[unit_type][@unit_name]
430
435
  end
431
436
  end
432
-
437
+
433
438
  def unit_name
434
439
  @unit_name
435
440
  end
436
-
441
+
437
442
  def to_s
438
443
  @value.to_s
439
444
  end
440
-
445
+
441
446
  def value
442
447
  @value
443
448
  end
444
-
449
+
445
450
  def to_f
446
451
  @value
447
452
  end
448
-
453
+
449
454
  def ==(other)
450
- self <=> other
455
+ (self <=> other) == 0
451
456
  end
452
-
457
+
453
458
  def <=>(other)
454
459
  (self.to_f * @exponent).to_f <=> other.to(@unit_name).to_f
455
460
  end
456
-
457
- private
461
+
462
+ private
458
463
  def initialize value, unit_name, exponent = 1.0
459
464
  @value = value.to_f
460
465
  @unit_name = unit_name
461
466
  @exponent = exponent
462
467
  end
463
-
468
+
464
469
  def method_missing unit_name, *args, &block
465
470
  exponent, unit_name = Alchemist.parse_prefix(unit_name)
466
- if Conversions[ unit_name ]
471
+ if Conversions[ unit_name ]
467
472
  types = Conversions[ @unit_name] & Conversions[ unit_name]
468
473
  if types[0] # assume first type
469
474
  if(Alchemist.conversion_table[types[0]][unit_name].is_a?(Array))
@@ -493,24 +498,24 @@ module Alchemist
493
498
  end
494
499
  end
495
500
  if unit_name == :/ && args[0].is_a?(NumericConversion)
496
- raise Exception, "Incompatible Types" unless (Conversions[@unit_name] & Conversions[args[0].unit_name]).length > 0
501
+ raise Exception, "Incompatible Types" unless (Conversions[@unit_name] & Conversions[args[0].unit_name]).length > 0
497
502
  end
498
503
  args.map!{|a| a.is_a?(NumericConversion) ? a.send(@unit_name).to_f / @exponent : a }
499
504
  @value = @value.send( unit_name, *args, &block )
500
-
501
-
505
+
506
+
502
507
  unit_name == :/ ? @value : self
503
508
  end
504
509
  end
505
510
  end
506
-
511
+
507
512
  Conversions = {}
508
513
  def method_missing unit_name, *args, &block
509
514
  exponent, unit_name = Alchemist.parse_prefix(unit_name)
510
515
  Conversions[ unit_name ] || super( unit_name, *args, &block )
511
516
  NumericConversion.new self, unit_name, exponent
512
517
  end
513
-
518
+
514
519
  def self.register(type, names, value)
515
520
  names = [names] unless names.is_a?(Array)
516
521
  value = value.is_a?(NumericConversion) ? value.base(type) : value
@@ -525,10 +530,10 @@ module Alchemist
525
530
  @@operator_actions[operation] ||= []
526
531
  @@operator_actions[operation] << [type, other_type, converted_type]
527
532
  end
528
-
533
+
529
534
  def self.parse_prefix(unit)
530
535
  @@unit_prefixes.each do |prefix, value|
531
- if unit.to_s =~ /^#{prefix}.+/ && @@si_units.include?(unit.to_s.gsub(/^#{prefix}/,''))
536
+ if unit.to_s =~ /^#{prefix}.+/ && @@si_units.include?(unit.to_s.gsub(/^#{prefix}/,''))
532
537
  if !(Conversions[ unit.to_s.gsub(/^#{prefix}/,'').to_sym ] & [ :information_storage ]).empty? && !@use_si && value >= 1000.0 && value.to_i & -value.to_i != value
533
538
  value = 2 ** (10 * (Math.log(value) / Math.log(10)) / 3)
534
539
  end
@@ -537,7 +542,7 @@ module Alchemist
537
542
  end
538
543
  [1.0, unit]
539
544
  end
540
-
545
+
541
546
  @@conversion_table.each do |type, conversions|
542
547
  conversions.each do |name, value|
543
548
  Conversions[name] ||= []
@@ -550,4 +555,4 @@ class Numeric
550
555
  include Alchemist
551
556
  end
552
557
 
553
- require 'alchemist/compound'
558
+ require 'alchemist/compound'
metadata CHANGED
@@ -1,70 +1,47 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: alchemist
3
- version: !ruby/object:Gem::Version
4
- hash: 77
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 1
9
- - 2
10
- - 1
11
- version: 0.1.2.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ prerelease:
12
6
  platform: ruby
13
- authors:
7
+ authors:
14
8
  - Matthew Mongeau
15
9
  autorequire:
16
10
  bindir: bin
17
11
  cert_chain: []
18
-
19
- date: 2011-01-05 00:00:00 -05:00
20
- default_executable:
12
+ date: 2012-05-25 00:00:00.000000000Z
21
13
  dependencies: []
22
-
23
14
  description: Conversions... like you've never seen them before!!
24
- email: matt@toastyapps.com
15
+ email: halogenandtoast@gmail.com
25
16
  executables: []
26
-
27
17
  extensions: []
28
-
29
18
  extra_rdoc_files: []
30
-
31
- files:
19
+ files:
32
20
  - Rakefile
33
21
  - lib/alchemist/compound.rb
34
22
  - lib/alchemist.rb
35
- has_rdoc: true
36
- homepage: http://github.com/toastyapps/alchemist
23
+ homepage: http://github.com/halogenandtoast/alchemist
37
24
  licenses: []
38
-
39
25
  post_install_message:
40
26
  rdoc_options: []
41
-
42
- require_paths:
27
+ require_paths:
43
28
  - lib
44
- required_ruby_version: !ruby/object:Gem::Requirement
29
+ required_ruby_version: !ruby/object:Gem::Requirement
45
30
  none: false
46
- requirements:
47
- - - ">="
48
- - !ruby/object:Gem::Version
49
- hash: 3
50
- segments:
51
- - 0
52
- version: "0"
53
- required_rubygems_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
36
  none: false
55
- requirements:
56
- - - ">="
57
- - !ruby/object:Gem::Version
58
- hash: 3
59
- segments:
60
- - 0
61
- version: "0"
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
62
41
  requirements: []
63
-
64
42
  rubyforge_project:
65
- rubygems_version: 1.3.7
43
+ rubygems_version: 1.8.16
66
44
  signing_key:
67
45
  specification_version: 3
68
46
  summary: Conversions... like you've never seen them before!
69
47
  test_files: []
70
-