farski-systeme 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 2
3
+ :patch: 0
4
+ :major: 0
@@ -0,0 +1,15 @@
1
+ require 'systeme/registration'
2
+
3
+ module Systeme
4
+ module Conversions
5
+ def method_missing(method_name, *arguments)
6
+ if match_data = method_name.to_s.match(/in_([a-zA-Z]+)/)
7
+ if Systeme::Registration::names.include?(match_data[1])
8
+ return self / 1.0.send(match_data[1])
9
+ end
10
+ end
11
+
12
+ return super
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,59 @@
1
+ require 'systeme/registration'
2
+
3
+ module Systeme
4
+ module Imperial
5
+ def self.included(caller)
6
+ caller.send :include, Systeme::Imperial::Declarations
7
+ end
8
+
9
+ module Units
10
+ IMPERIAL_MEASURES = Hash.new
11
+
12
+ IMPERIAL_MEASURES['length'] = Hash.new
13
+ IMPERIAL_MEASURES['length'][:si] = 0.3048
14
+ IMPERIAL_MEASURES['length'][:units] = Array.new
15
+ IMPERIAL_MEASURES['length'][:units] << { :unit => "thou", :factor => (1.0/12000), :aliases => ["mil"] }
16
+ IMPERIAL_MEASURES['length'][:units] << { :unit => "inch", :factor => (1.0/12), :aliases => ["in", "inches"] }
17
+ IMPERIAL_MEASURES['length'][:units] << { :unit => "foot", :factor => 1, :aliases => ["feet", "ft"] }
18
+ IMPERIAL_MEASURES['length'][:units] << { :unit => "yard", :factor => 3, :aliases => ["yd"] }
19
+ IMPERIAL_MEASURES['length'][:units] << { :unit => "furlong", :factor => 660 }
20
+ IMPERIAL_MEASURES['length'][:units] << { :unit => "mile", :factor => 5280, :aliases => ["mi"] }
21
+ IMPERIAL_MEASURES['length'][:units] << { :unit => "league", :factor => 15840 }
22
+ # maritime units
23
+ IMPERIAL_MEASURES['length'][:units] << { :unit => "fathom", :factor => 6 }
24
+ IMPERIAL_MEASURES['length'][:units] << { :unit => "cable", :factor => 608 }
25
+ IMPERIAL_MEASURES['length'][:units] << { :unit => "nautical_mile", :factor => 6080, :aliases => ["NM", "nmi", "M"] }
26
+ IMPERIAL_MEASURES['length'][:units] << { :unit => "link", :factor => (66/100), :aliases => ["lnk"] }
27
+ IMPERIAL_MEASURES['length'][:units] << { :unit => "rod", :factor => (66/4), :aliases => ["perch", "perches", "pole", "lug"] }
28
+ IMPERIAL_MEASURES['length'][:units] << { :unit => "chain", :factor => 66 }
29
+
30
+ IMPERIAL_MEASURES['weight'] = Hash.new
31
+ IMPERIAL_MEASURES['weight'][:si] = 0.45359237
32
+ IMPERIAL_MEASURES['weight'][:units] = Array.new
33
+ IMPERIAL_MEASURES['weight'][:units] << { :unit => "grain", :factor => (1/7000) }
34
+ IMPERIAL_MEASURES['weight'][:units] << { :unit => "drachm", :factor => (1/256) }
35
+ IMPERIAL_MEASURES['weight'][:units] << { :unit => "ounce", :factor => (1/16), :aliases => ["oz"] }
36
+ IMPERIAL_MEASURES['weight'][:units] << { :unit => "pound", :factor => 1, :aliases => ["lb", "lbm", "lbs"] }
37
+ IMPERIAL_MEASURES['weight'][:units] << { :unit => "stone", :factor => 14, :aliases => ["st"] }
38
+ IMPERIAL_MEASURES['weight'][:units] << { :unit => "quarter", :factor => 28 }
39
+ IMPERIAL_MEASURES['weight'][:units] << { :unit => "hundredweight", :factor => 112, :aliases => ["cwt"] }
40
+ IMPERIAL_MEASURES['weight'][:units] << { :unit => "ton", :factor => 2240 }
41
+ end # Units
42
+
43
+ module Declarations
44
+ Systeme::Imperial::Units::IMPERIAL_MEASURES.each do |measure, data|
45
+ data[:units].each do |unit|
46
+ define_method(unit[:unit].to_sym) { self * unit[:factor].to_f * data[:si] }
47
+ class_eval("alias :" + unit[:unit] + "s :" + unit[:unit])
48
+ Systeme::Registration::names << unit[:unit] << unit[:unit]+"s"
49
+ if unit[:aliases]
50
+ unit[:aliases].each do |aka|
51
+ class_eval("alias :" + aka + " :" + unit[:unit])
52
+ Systeme::Registration::names << aka
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end # Declarations
58
+ end
59
+ end
@@ -0,0 +1,62 @@
1
+ require 'systeme/registration'
2
+
3
+ module Systeme
4
+ module Metric
5
+ def self.included(caller)
6
+ caller.send :include, Systeme::Metric::Declarations
7
+ end
8
+
9
+ module Units
10
+ SI_BASE_UNITS = Hash.new
11
+ SI_BASE_UNITS[:meter] = { :symbol => :m, :measure => "length" }
12
+ SI_BASE_UNITS[:gram] = { :symbol => :g, :measure => "mass", :factor_offset => -3 }
13
+ SI_BASE_UNITS[:second] = { :symbol => :s, :measure => "time" }
14
+ SI_BASE_UNITS[:ampere] = { :symbol => :A, :measure => "electric current" }
15
+ SI_BASE_UNITS[:kelvin] = { :symbol => :K, :measure => "thermodynamic temperature" }
16
+
17
+ SI_UNIT_PREFIXES = Hash.new
18
+ SI_UNIT_PREFIXES[:yocto] = { :factor => -24, :symbol_prefix => "y" }
19
+ SI_UNIT_PREFIXES[:zepto] = { :factor => -21, :symbol_prefix => "z" }
20
+ SI_UNIT_PREFIXES[:atto] = { :factor => -18, :symbol_prefix => "a" }
21
+ SI_UNIT_PREFIXES[:femto] = { :factor => -15, :symbol_prefix => "f" }
22
+ SI_UNIT_PREFIXES[:pico] = { :factor => -12, :symbol_prefix => "p" }
23
+ SI_UNIT_PREFIXES[:nano] = { :factor => -9, :symbol_prefix => "n" }
24
+ SI_UNIT_PREFIXES[:micro] = { :factor => -6, :symbol_prefix => "u" } # use of 'µ' should be supported with unicode?
25
+ SI_UNIT_PREFIXES[:milli] = { :factor => -3, :symbol_prefix => "m" }
26
+ SI_UNIT_PREFIXES[:centi] = { :factor => -2, :symbol_prefix => "c" }
27
+ SI_UNIT_PREFIXES[:deci] = { :factor => -1, :symbol_prefix => "d" }
28
+ SI_UNIT_PREFIXES[:deca] = { :factor => 1, :symbol_prefix => "da" }
29
+ SI_UNIT_PREFIXES[:hecto] = { :factor => 2, :symbol_prefix => "h" }
30
+ SI_UNIT_PREFIXES[:kilo] = { :factor => 3, :symbol_prefix => "k" }
31
+ SI_UNIT_PREFIXES[:mega] = { :factor => 6, :symbol_prefix => "M" }
32
+ SI_UNIT_PREFIXES[:giga] = { :factor => 9, :symbol_prefix => "G" }
33
+ SI_UNIT_PREFIXES[:tera] = { :factor => 12, :symbol_prefix => "T" }
34
+ SI_UNIT_PREFIXES[:peta] = { :factor => 15, :symbol_prefix => "P" }
35
+ SI_UNIT_PREFIXES[:exa] = { :factor => 18, :symbol_prefix => "E" }
36
+ SI_UNIT_PREFIXES[:zetta] = { :factor => 21, :symbol_prefix => "Z" }
37
+ SI_UNIT_PREFIXES[:yotta] = { :factor => 24, :symbol_prefix => "Y" }
38
+ end # Units
39
+
40
+ module Declarations
41
+ Systeme::Metric::Units::SI_BASE_UNITS.each do |unit, unit_data|
42
+ unit_factor_offset = (unit_data[:factor_offset] ? unit_data[:factor_offset] : 0 )
43
+ plural_unit_name = unit.to_s + "s"
44
+
45
+ define_method(plural_unit_name.to_sym) { self * (10**(unit_factor_offset)) }
46
+ class_eval("alias :" + unit_data[:symbol].to_s + " :" + plural_unit_name)
47
+ class_eval("alias :" + plural_unit_name.chop + " :" + plural_unit_name)
48
+ Systeme::Registration::names << plural_unit_name << unit_data[:symbol].to_s << plural_unit_name.chop
49
+
50
+ Systeme::Metric::Units::SI_UNIT_PREFIXES.each do |prefix, prefix_data|
51
+ plural_prefixed_unit_name = prefix.to_s + plural_unit_name
52
+ prefixed_symbol = prefix_data[:symbol_prefix] + unit_data[:symbol].to_s
53
+
54
+ define_method(plural_prefixed_unit_name.to_sym) { self * (10**(prefix_data[:factor] + unit_factor_offset)) }
55
+ class_eval("alias :" + prefixed_symbol + " :" + plural_prefixed_unit_name)
56
+ class_eval("alias :" + plural_prefixed_unit_name.chop + " :" + plural_prefixed_unit_name)
57
+ Systeme::Registration::names << plural_prefixed_unit_name << prefixed_symbol << plural_prefixed_unit_name.chop
58
+ end
59
+ end
60
+ end # Declarations
61
+ end
62
+ end
@@ -0,0 +1,8 @@
1
+ module Systeme
2
+ module Registration
3
+ class << self
4
+ attr_accessor :names
5
+ end
6
+ @names = Array.new
7
+ end
8
+ end
data/lib/systeme.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'systeme/metric'
2
+ require 'systeme/imperial'
3
+ require 'systeme/conversions'
4
+ require 'systeme/registration'
5
+
6
+ module Systeme
7
+ include Systeme::Metric
8
+ include Systeme::Imperial
9
+ include Systeme::Conversions
10
+ end
11
+
12
+ Numeric.send :include, Systeme
@@ -0,0 +1,24 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class ImperialTest < Test::Unit::TestCase
4
+ should "maintain base unit value through declaration" do
5
+ assert_equal 1, 1
6
+ # assert_equal 1, 1.ft
7
+ # assert_equal 1, 1.lb
8
+ end
9
+ #
10
+ # should "express non-base unit value in the base unit" do
11
+ # assert_equal 3, 1.yd
12
+ # assert_equal 14, 1.stone
13
+ # end
14
+
15
+ # should "convert value from base unit to any other unit" do
16
+ # assert_equal 1000, 1.kg.to_g
17
+ # assert_equal 100, 1.m.to_cm
18
+ # end
19
+ #
20
+ # should "convert value between arbitrary units" do
21
+ # assert_equal 1, 1000.mg.to_g
22
+ # assert_equal 10, 1.cm.to_mm
23
+ # end
24
+ end
@@ -0,0 +1,23 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class MetricTest < Test::Unit::TestCase
4
+ should "maintain base unit value through declaration" do
5
+ assert_equal 1, 1.kg
6
+ assert_equal 1, 1.m
7
+ end
8
+
9
+ should "express non-base unit value in the base unit" do
10
+ assert_equal 0.001, 1.g
11
+ assert_equal 1000, 1.km
12
+ end
13
+
14
+ should "convert value from base unit to any other unit" do
15
+ assert_equal 1000, 1.kg.in_g
16
+ assert_equal 100, 1.m.in_cm
17
+ end
18
+
19
+ should "convert value between arbitrary units" do
20
+ assert_equal 1, 1000.mg.in_g
21
+ assert_equal 10, 1.cm.in_mm
22
+ end
23
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+
6
+ require 'systeme'
7
+
8
+ class Test::Unit::TestCase
9
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: farski-systeme
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Kalafarski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-17 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: TODO
17
+ email: chris@farski.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - VERSION.yml
26
+ - lib/systeme
27
+ - lib/systeme/conversions.rb
28
+ - lib/systeme/imperial.rb
29
+ - lib/systeme/metric.rb
30
+ - lib/systeme/registration.rb
31
+ - lib/systeme.rb
32
+ - test/imperial_test.rb
33
+ - test/metric_test.rb
34
+ - test/test_helper.rb
35
+ has_rdoc: false
36
+ homepage: http://github.com/farski/systeme
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.2.0
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: TODO
61
+ test_files: []
62
+