eymiha_units 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/gem_package.rb +34 -0
- data/lib/units/definitions/area.rb +5 -0
- data/lib/units/definitions/length.rb +34 -0
- data/lib/units/definitions/mass.rb +16 -0
- data/lib/units/definitions/measures.rb +6 -0
- data/lib/units/definitions/time.rb +61 -0
- data/lib/units/definitions/velocity.rb +9 -0
- data/lib/units/definitions/volume.rb +27 -0
- data/lib/units/numeric.rb +88 -0
- data/lib/units/numeric_with_units.rb +635 -0
- data/lib/units/object.rb +79 -0
- data/lib/units/units.rb +229 -0
- data/lib/units/units_exception.rb +14 -0
- data/lib/units/units_hash.rb +112 -0
- data/lib/units/units_measure.rb +91 -0
- data/lib/units/units_system.rb +85 -0
- data/lib/units/units_unit.rb +60 -0
- data/lib/units.rb +4 -0
- data/rakefile.rb +2 -0
- data/test/framework.rb +7 -0
- data/test/tc_definitions.rb +49 -0
- data/test/tc_formatting.rb +41 -0
- data/test/tc_formatting_derived.rb +73 -0
- data/test/tc_measure_create.rb +59 -0
- data/test/tc_measure_derive.rb +46 -0
- data/test/tc_system_create.rb +31 -0
- data/test/tc_unit_ambiguity.rb +46 -0
- data/test/tc_unit_arithmetic.rb +41 -0
- data/test/tc_unit_create.rb +35 -0
- data/test/tc_unit_derive.rb +87 -0
- data/test/tc_unit_equality.rb +54 -0
- data/test/tc_unit_forward_reference.rb +44 -0
- data/test/tc_unit_greek.rb +163 -0
- data/test/tc_unit_hash.rb +66 -0
- data/test/tc_unit_identifiers.rb +48 -0
- data/test/tc_unit_rank.rb +53 -0
- data/test/tc_uses_1.rb +212 -0
- data/test/tc_uses_2.rb +149 -0
- metadata +118 -0
data/gem_package.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
class GemPackage
|
2
|
+
|
3
|
+
attr_reader :name, :version, :files
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@name = 'eymiha_units'
|
7
|
+
@version = '0.1.0'
|
8
|
+
@files = FileList[
|
9
|
+
'*.rb',
|
10
|
+
'lib/**/*',
|
11
|
+
'test/*',
|
12
|
+
'html/**/*'
|
13
|
+
]
|
14
|
+
end
|
15
|
+
|
16
|
+
def fill_spec(s)
|
17
|
+
s.name = name
|
18
|
+
s.version = version
|
19
|
+
s.summary = "Emiyha - units and conversions"
|
20
|
+
s.files = files.to_a
|
21
|
+
s.require_path = 'lib'
|
22
|
+
# s.autorequire = name
|
23
|
+
s.has_rdoc = true
|
24
|
+
s.rdoc_options << "--all"
|
25
|
+
s.author = "Dave Anderson"
|
26
|
+
s.email = "dave@eymiha.com"
|
27
|
+
s.homepage = "http://www.eymiha.com"
|
28
|
+
s.rubyforge_project = "cori"
|
29
|
+
s.add_dependency("eymiha",">= 0.1.1")
|
30
|
+
s.add_dependency("eymiha_math",">= 0.1.1")
|
31
|
+
s.add_dependency("eymiha_util",">= 0.1.5")
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
Units.create :length do |m|
|
2
|
+
m.system :english do |s|
|
3
|
+
s.unit :name => :inch, :plural => :inches, :abbrev => :in
|
4
|
+
s.unit :name => :foot, :plural => :feet, :equals => 12.inches,
|
5
|
+
:abbrev => :ft
|
6
|
+
s.unit :name => :yard, :equals => 3.feet, :abbrev => :yd
|
7
|
+
s.unit :name => :mile, :equals => 5280.feet, :abbrev => :mi
|
8
|
+
s.unit :name => :nautical_mile, :equals => 1852.meters, :abbrev => :nmi
|
9
|
+
s.format :name => :feet_inches_and_32s,
|
10
|
+
:format => lambda { |u|
|
11
|
+
ru = u.round_to_nearest(1.inch,32)
|
12
|
+
feet = ru.feet.floor
|
13
|
+
inches = (ru-feet).inches
|
14
|
+
"#{feet} #{inches.with_fraction 32}" }
|
15
|
+
end
|
16
|
+
m.system :old_english do |s|
|
17
|
+
s.unit :name => :fathom, :equals => 2.yards
|
18
|
+
s.unit :name => :chain, :equals => 22.yards
|
19
|
+
s.unit :name => :furlong, :equals => 660.feet
|
20
|
+
s.unit :name => :league, :equals => 3.nmi
|
21
|
+
end
|
22
|
+
m.system :metric do |s|
|
23
|
+
s.unit :name => :meter, :equals =>39.37.inches, :abbrev => :m,
|
24
|
+
:greek => :ten
|
25
|
+
s.unit :name => :anstrom, :equals => 0.1.nanometers, :abbrev => :A
|
26
|
+
end
|
27
|
+
m.system :astronomical do |s|
|
28
|
+
s.unit :name => :astronomical_unit, :equals => 149598000.kilometers,
|
29
|
+
:abbrev => :AU
|
30
|
+
s.unit :name => :light_year, :equals => speed_of_light*(year.seconds),
|
31
|
+
:abbrev => :ly
|
32
|
+
s.unit :name => :parsec, :equals => 3.262.light_years, :abbrev => :pc
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
Units.create :mass do |m|
|
2
|
+
m.system :metric do |s|
|
3
|
+
s.unit :name => :gram, :abbrev => :g, :greek => :ten
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
Units.create :weight do |m|
|
8
|
+
m.system :avoirdupois do |s|
|
9
|
+
s.unit :name => :grain, :equals => (1/7000.0).lb, :abbrev => :gr
|
10
|
+
s.unit :name => :dram, :equals => (1/16.0).oz, :abbrev => :dr
|
11
|
+
s.unit :name => :ounce, :equals => (1/16.0).lb, :abbrev => :oz
|
12
|
+
s.unit :name => :pound, :equals => 454.g, :abbrev => :lb
|
13
|
+
s.unit :name => :hundredweight, :equals => 100.lb, :abbrev => :cwt
|
14
|
+
s.unit :name => :ton, :equals => 20.cwt
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
Units.create :time do |m|
|
2
|
+
m.system :base do |s|
|
3
|
+
s.unit :name => :nanosecond, :equals => 0.000000001.seconds,
|
4
|
+
:abbrev => [:ns, :nsec]
|
5
|
+
s.unit :name => :microsecond, :equals => 0.000001.seconds,
|
6
|
+
:abbrev => [:us, :usec]
|
7
|
+
s.unit :name => :millisecond, :equals => 0.001.seconds,
|
8
|
+
:abbrev => [:ms, :msec]
|
9
|
+
s.unit :name => :second, :abbrev => [:s, :sec]
|
10
|
+
s.unit :name => :minute, :equals => 60.seconds, :abbrev => [:m, :min]
|
11
|
+
s.unit :name => :hour, :equals => 60.minutes, :abbrev => [:h, :hr]
|
12
|
+
s.unit :name => :day, :equals => 24.hours, :abbrev => [:d, :dy]
|
13
|
+
s.unit :name => :week, :equals => 7.days, :abbrev => [:w, :wk]
|
14
|
+
s.format :name => :weeks_and_days,
|
15
|
+
:format => lambda { |u|
|
16
|
+
weeks = u.weeks.floor
|
17
|
+
days = (u-weeks).days.round
|
18
|
+
weeks == 0 ?
|
19
|
+
(days == 0 ? "#{weeks}" : "#{days}") :
|
20
|
+
"#{weeks} #{days}" }
|
21
|
+
end
|
22
|
+
m.system :common do |s|
|
23
|
+
s.unit :name => :month, :equals => [30.days, 4.weeks], :abbrev => [:m, :mo]
|
24
|
+
s.unit :name => :year, :equals => [365.days, 12.months, 52.weeks],
|
25
|
+
:abbrev => [:y, :yr]
|
26
|
+
end
|
27
|
+
m.system :long do |s|
|
28
|
+
s.unit :name => :decade, :equals => 10.years
|
29
|
+
s.unit :name => :century, :plural => :centuries, :equals => 100.years
|
30
|
+
s.unit :name => :millenium, :plural => :millenia, :equals => 1000.years
|
31
|
+
end
|
32
|
+
m.system :old_english do |s|
|
33
|
+
s.unit :name => :fortnight, :equals => 2.weeks
|
34
|
+
end
|
35
|
+
m.system :months do |s|
|
36
|
+
s.unit :name => :january, :equals => 31.days, :singular => true,
|
37
|
+
:sequence => 1
|
38
|
+
s.unit :name => :february, :equals => [28.days, 29.days], :singular => true,
|
39
|
+
:sequence => 2
|
40
|
+
s.unit :name => :march, :equals => 31.days, :singular => true,
|
41
|
+
:sequence => 3
|
42
|
+
s.unit :name => :april, :equals => 30.days, :singular => true,
|
43
|
+
:sequence => 4
|
44
|
+
s.unit :name => :may, :equals => 31.days, :singular => true,
|
45
|
+
:sequence => 5
|
46
|
+
s.unit :name => :june, :equals => 30.days, :singular => true,
|
47
|
+
:sequence => 6
|
48
|
+
s.unit :name => :july, :equals => 31.days, :singular => true,
|
49
|
+
:sequence => 7
|
50
|
+
s.unit :name => :august, :equals => 31.days, :singular => true,
|
51
|
+
:sequence => 8
|
52
|
+
s.unit :name => :september, :equals => 30.days, :singular => true,
|
53
|
+
:sequence => 9
|
54
|
+
s.unit :name => :october, :equals => 31.days, :singular => true,
|
55
|
+
:sequence => 10
|
56
|
+
s.unit :name => :november, :equals => 30.days, :singular => true,
|
57
|
+
:sequence => 11
|
58
|
+
s.unit :name => :december, :equals => 31.days, :singular => true,
|
59
|
+
:sequence => 12
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
Units.derive :volume, Units[:length]**3 do |m|
|
2
|
+
m.system :metric do |s|
|
3
|
+
s.unit :name => :liter, :equals => (10.cm)**3, :abbrev => :L, :greek => :ten
|
4
|
+
end
|
5
|
+
m.system :us do |s|
|
6
|
+
s.unit :name => :minim, :equals => (1/480.0).floz, :abbrev => :min
|
7
|
+
s.unit :name => :drop, :equals => (1/360.0).floz, :abbrev => :gtt
|
8
|
+
s.unit :name => :fluid_dram, :equals => 0.125.floz, :abbrev => :fldr
|
9
|
+
s.unit :name => :teaspoon, :equals => (1/3.0).tbsp, :abbrev => :tsp
|
10
|
+
s.unit :name => :tablespoon, :equals => 0.5.floz, :abbrev => :tbsp
|
11
|
+
s.unit :name => :fluid_ounce, :equals => 29.5735295625.mL, :abbrev => :floz
|
12
|
+
s.unit :name => :cup, :equals => 8.floz, :abbrev => :c
|
13
|
+
s.unit :name => :pint, :equals => 2.cups, :abbrev => :pt
|
14
|
+
s.unit :name => :quart, :equals => 2.pints, :abbrev => :qt
|
15
|
+
s.unit :name => :gallon, :equals => 4.quarts, :abbrev => :gal
|
16
|
+
s.unit :name => :firkin, :equals => 9.gallons
|
17
|
+
s.unit :name => :barrel, :equals => 31.5.gallons, :abbrev => :flbl
|
18
|
+
s.unit :name => :hogshead, :equals => 2.barrels, :abbrev => :hhd
|
19
|
+
end
|
20
|
+
m.system :mixology do |s|
|
21
|
+
s.unit :name => :pony, :equals => 0.75.shots
|
22
|
+
s.unit :name => :shot, :equals => 1.floz
|
23
|
+
s.unit :name => :jigger, :equals => 1.5.shots
|
24
|
+
s.unit :name => :fifth, :equals => 0.2.gallons
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'units/units'
|
2
|
+
require 'eymiha_math'
|
3
|
+
require 'approximately_equals'
|
4
|
+
|
5
|
+
# Numerics are extended to allow them to be united with units.
|
6
|
+
#
|
7
|
+
# Part of this extension is in the added dynamism of the class. During
|
8
|
+
# unit definition, if an unencountered unit is referenced, the use is held
|
9
|
+
# until more information is loaded into the system and the unknown unit has
|
10
|
+
# been resolved. During use, unencountered units are raised.
|
11
|
+
class Numeric
|
12
|
+
|
13
|
+
alias :method_missing_before_units :method_missing
|
14
|
+
|
15
|
+
def method_missing(method,*args) # :nodoc:
|
16
|
+
if Units.defining?
|
17
|
+
reference = Units.make_forward_reference(method,Units.defining?)
|
18
|
+
begin
|
19
|
+
value = Units.convert self, method
|
20
|
+
Units.release_forward_reference reference
|
21
|
+
value
|
22
|
+
rescue MissingUnitsException
|
23
|
+
Units.hold_forward_reference
|
24
|
+
rescue UnitsException => exception
|
25
|
+
units_problem("definition",exception,method,args)
|
26
|
+
rescue Exception => exception
|
27
|
+
method_missing_before_units(method,args)
|
28
|
+
end
|
29
|
+
else
|
30
|
+
begin
|
31
|
+
ps = method.to_s.split '_per_'
|
32
|
+
case ps.size
|
33
|
+
when 1 then unite method
|
34
|
+
when 2 then unite(ps[0])/(1.unite(ps[1]))
|
35
|
+
else raise UnitsException('invalid per method')
|
36
|
+
end
|
37
|
+
rescue UnitsException => exception
|
38
|
+
units_problem("usage",exception,method,args)
|
39
|
+
rescue Exception
|
40
|
+
method_missing_before_units(method,*args)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns a NumericWithUnits whose numeric value is the value of the
|
46
|
+
# instance, and whose unit is determined from the provided unit and power in
|
47
|
+
# the context of a given measure:
|
48
|
+
# * as a String or Symbol equal to the singular, plural or abbreviated name of a unit,
|
49
|
+
# * as a UnitsHash,
|
50
|
+
# * as the unit part of a NumericWithUnits,
|
51
|
+
# * as an Array of any of the above.
|
52
|
+
def unite(unit=nil,power=1,measure=nil)
|
53
|
+
if !unit
|
54
|
+
self
|
55
|
+
else
|
56
|
+
if (unit.kind_of? Array)
|
57
|
+
units = UnitsHash.new
|
58
|
+
unit.each {|u| units.merge! 1.unite(u)}
|
59
|
+
unit = units
|
60
|
+
elsif (unit.kind_of? String)||(unit.kind_of? Symbol)
|
61
|
+
s = unit.to_s
|
62
|
+
as = s.split('_and_')
|
63
|
+
if as.size == 1
|
64
|
+
units = Units.lookup(as[0])
|
65
|
+
case units.size
|
66
|
+
when 1 then unit = units[0]
|
67
|
+
when 0 then
|
68
|
+
as = as[0].split('_')
|
69
|
+
units_problem("usage",
|
70
|
+
AmbiguousUnitsException.new(unit),
|
71
|
+
:unit=,unit) if as.size == 0
|
72
|
+
unit = unite(as,power,measure).unit
|
73
|
+
else
|
74
|
+
units_problem("usage",
|
75
|
+
AmbiguousUnitsException.new(unit),
|
76
|
+
:unit=,unit)
|
77
|
+
end
|
78
|
+
else
|
79
|
+
unit = unite(as,power,measure).unit
|
80
|
+
end
|
81
|
+
elsif unit.kind_of? NumericWithUnits
|
82
|
+
unit = unit.unit
|
83
|
+
end
|
84
|
+
NumericWithUnits.new(self,unit,power)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|