omniship 0.1.0
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.
- data/CHANGELOG +0 -0
- data/MIT-LICENSE +20 -0
- data/README.markdown +91 -0
- data/lib/omniship/address.rb +135 -0
- data/lib/omniship/base.rb +11 -0
- data/lib/omniship/carrier.rb +69 -0
- data/lib/omniship/carriers/fedex.rb +330 -0
- data/lib/omniship/carriers/ups.rb +564 -0
- data/lib/omniship/carriers/usps.rb +438 -0
- data/lib/omniship/carriers.rb +13 -0
- data/lib/omniship/contact.rb +19 -0
- data/lib/omniship/package.rb +147 -0
- data/lib/omniship/rate_estimate.rb +59 -0
- data/lib/omniship/rate_response.rb +16 -0
- data/lib/omniship/response.rb +42 -0
- data/lib/omniship/shipment_event.rb +12 -0
- data/lib/omniship/tracking_response.rb +18 -0
- data/lib/omniship/version.rb +3 -0
- data/lib/omniship.rb +73 -0
- data/lib/vendor/quantified/MIT-LICENSE +22 -0
- data/lib/vendor/quantified/README.markdown +49 -0
- data/lib/vendor/quantified/Rakefile +21 -0
- data/lib/vendor/quantified/init.rb +0 -0
- data/lib/vendor/quantified/lib/quantified/attribute.rb +208 -0
- data/lib/vendor/quantified/lib/quantified/length.rb +20 -0
- data/lib/vendor/quantified/lib/quantified/mass.rb +19 -0
- data/lib/vendor/quantified/lib/quantified.rb +6 -0
- data/lib/vendor/quantified/test/length_test.rb +92 -0
- data/lib/vendor/quantified/test/mass_test.rb +88 -0
- data/lib/vendor/quantified/test/test_helper.rb +10 -0
- data/lib/vendor/xml_node/README +36 -0
- data/lib/vendor/xml_node/Rakefile +21 -0
- data/lib/vendor/xml_node/benchmark/bench_generation.rb +32 -0
- data/lib/vendor/xml_node/init.rb +1 -0
- data/lib/vendor/xml_node/lib/xml_node.rb +222 -0
- data/lib/vendor/xml_node/test/test_generating.rb +94 -0
- data/lib/vendor/xml_node/test/test_parsing.rb +43 -0
- metadata +205 -0
@@ -0,0 +1,16 @@
|
|
1
|
+
module Omniship #:nodoc:
|
2
|
+
class RateResponse < Response
|
3
|
+
|
4
|
+
attr_reader :rates
|
5
|
+
|
6
|
+
def initialize(success, message, params = {}, options = {})
|
7
|
+
@rates = Array(options[:estimates] || options[:rates] || options[:rate_estimates])
|
8
|
+
super
|
9
|
+
end
|
10
|
+
|
11
|
+
alias_method :estimates, :rates
|
12
|
+
alias_method :rate_estimates, :rates
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Omniship #:nodoc:
|
2
|
+
class Error < StandardError
|
3
|
+
end
|
4
|
+
|
5
|
+
class ResponseError < Error
|
6
|
+
attr_reader :response
|
7
|
+
|
8
|
+
def initialize(response = nil)
|
9
|
+
if response.is_a? Response
|
10
|
+
super(response.message)
|
11
|
+
@response = response
|
12
|
+
else
|
13
|
+
super(response)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class Response
|
19
|
+
|
20
|
+
attr_reader :params
|
21
|
+
attr_reader :message
|
22
|
+
attr_reader :test
|
23
|
+
attr_reader :xml
|
24
|
+
attr_reader :request
|
25
|
+
|
26
|
+
def initialize(success, message, params = {}, options = {})
|
27
|
+
@success, @message, @params = success, message, params.stringify_keys
|
28
|
+
@test = options[:test] || false
|
29
|
+
@xml = options[:xml]
|
30
|
+
@request = options[:request]
|
31
|
+
raise ResponseError.new(self) unless success
|
32
|
+
end
|
33
|
+
|
34
|
+
def success?
|
35
|
+
@success ? true : false
|
36
|
+
end
|
37
|
+
|
38
|
+
def test?
|
39
|
+
@test ? true : false
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Omniship #:nodoc:
|
2
|
+
class TrackingResponse < Response
|
3
|
+
attr_reader :tracking_number # string
|
4
|
+
attr_reader :shipment_events # array of ShipmentEvents in chronological order
|
5
|
+
attr_reader :origin, :destination
|
6
|
+
|
7
|
+
def initialize(success, message, params = {}, options = {})
|
8
|
+
@tracking_number = options[:tracking_number]
|
9
|
+
@shipment_events = Array(options[:shipment_events])
|
10
|
+
@origin, @destination = options[:origin], options[:destination]
|
11
|
+
super
|
12
|
+
end
|
13
|
+
|
14
|
+
def latest_event
|
15
|
+
@shipment_events.last
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/omniship.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2009 Jaded Pixel
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
#++
|
23
|
+
|
24
|
+
### TODO Working on creating code for using an initializer for configuration ###
|
25
|
+
|
26
|
+
def Omniship.setup
|
27
|
+
@root = Rails.root
|
28
|
+
@boot = File.join(@root, "config", "boot.rb").freeze
|
29
|
+
@config = File.join(@root, "config", "omniship.yml").freeze
|
30
|
+
@keys = %w{ username password key }.map { |v| v.freeze }.freeze
|
31
|
+
require boot unless defined? Rails.env
|
32
|
+
@@config = YAML.load_file(@config) rescue false
|
33
|
+
if @@config != false
|
34
|
+
raise "Invalid fedex configuration file: #{@config}" unless @@config.is_a?(Hash)
|
35
|
+
if (@@config.keys & @keys).sort == @keys.sort and !@@config.has_key?(Rails.env)
|
36
|
+
@@config[Rails.env] = {
|
37
|
+
"ups" => @@config["ups"],
|
38
|
+
"fedex" => @@config["fedex"],
|
39
|
+
"usps" => @@config["usps"]
|
40
|
+
}
|
41
|
+
end
|
42
|
+
@@config[Rails.env].freeze
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
$:.unshift File.dirname(__FILE__)
|
47
|
+
|
48
|
+
begin
|
49
|
+
require 'active_support/all'
|
50
|
+
rescue LoadError => e
|
51
|
+
require 'rubygems'
|
52
|
+
gem "activesupport", ">= 2.3.5"
|
53
|
+
require "active_support/all"
|
54
|
+
end
|
55
|
+
|
56
|
+
autoload :XmlNode, 'vendor/xml_node/lib/xml_node'
|
57
|
+
autoload :Quantified, 'vendor/quantified/lib/quantified'
|
58
|
+
|
59
|
+
require 'net/https'
|
60
|
+
require 'active_utils'
|
61
|
+
require 'nokogiri'
|
62
|
+
|
63
|
+
require 'omniship/base'
|
64
|
+
require 'omniship/contact'
|
65
|
+
require 'omniship/response'
|
66
|
+
require 'omniship/rate_response'
|
67
|
+
require 'omniship/tracking_response'
|
68
|
+
require 'omniship/package'
|
69
|
+
require 'omniship/address'
|
70
|
+
require 'omniship/rate_estimate'
|
71
|
+
require 'omniship/carrier'
|
72
|
+
require 'omniship/carriers'
|
73
|
+
require 'omniship/shipment_event'
|
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2009 James MacAulay
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,49 @@
|
|
1
|
+
Quantified
|
2
|
+
==========
|
3
|
+
|
4
|
+
Pretty quantifiable measurements which feel like ActiveSupport::Duration.
|
5
|
+
|
6
|
+
Access whichever included attributes you want like so:
|
7
|
+
|
8
|
+
require 'quantified/mass'
|
9
|
+
require 'quantified/length'
|
10
|
+
|
11
|
+
Add methods to Numeric (only plural, must correspond with plural unit names):
|
12
|
+
|
13
|
+
Mass.numeric_methods :grams, :kilograms, :ounces, :pounds
|
14
|
+
Length.numeric_methods :metres, :centimetres, :inches, :feet
|
15
|
+
|
16
|
+
Then you can do things like this:
|
17
|
+
|
18
|
+
1.feet == 12.inches
|
19
|
+
# => true
|
20
|
+
|
21
|
+
18.inches.to_feet
|
22
|
+
# => #<Quantified::Length: 1.5 feet>
|
23
|
+
|
24
|
+
(2.5).feet.in_millimetres.to_s
|
25
|
+
# => "762.0 millimetres"
|
26
|
+
|
27
|
+
|
28
|
+
You can easily define new attributes. Here's length.rb:
|
29
|
+
|
30
|
+
module Quantified
|
31
|
+
class Length < Attribute
|
32
|
+
system :metric do
|
33
|
+
primitive :metre
|
34
|
+
|
35
|
+
one :centimetre, :is => Length.new(0.01, :metres)
|
36
|
+
one :millimetre, :is => Length.new(0.1, :centimetres)
|
37
|
+
one :kilometre, :is => Length.new(1000, :metres)
|
38
|
+
end
|
39
|
+
|
40
|
+
system :imperial do
|
41
|
+
primitive :inch
|
42
|
+
one :inch, :is => Length.new(2.540, :centimetres)
|
43
|
+
|
44
|
+
one :foot, :plural => :feet, :is => Length.new(12, :inches)
|
45
|
+
one :yard, :is => Length.new(3, :feet)
|
46
|
+
one :mile, :is => Length.new(5280, :feet)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the calculations plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'Quantified'
|
19
|
+
rdoc.options << '--line-numbers --inline-source'
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
File without changes
|
@@ -0,0 +1,208 @@
|
|
1
|
+
module Quantified
|
2
|
+
class Attribute
|
3
|
+
include Comparable
|
4
|
+
|
5
|
+
attr_reader :amount, :unit
|
6
|
+
|
7
|
+
def initialize(amount, unit)
|
8
|
+
raise ArgumentError, "amount must be a Numeric" unless amount.is_a?(Numeric)
|
9
|
+
@amount, @unit = amount, unit.to_sym
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_s
|
13
|
+
"#{amount} #{unit}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def inspect
|
17
|
+
"#<#{self.class.name}: #{amount} #{unit}>"
|
18
|
+
end
|
19
|
+
|
20
|
+
def ==(other)
|
21
|
+
(BigDecimal.new(self.amount.to_s) == BigDecimal.new(other.amount.to_s) && self.unit == other.unit) || BigDecimal.new(self.class.convert(self.amount, self.unit, other.unit).to_s) == BigDecimal.new(other.amount.to_s)
|
22
|
+
rescue NoMethodError
|
23
|
+
self.amount == other
|
24
|
+
end
|
25
|
+
|
26
|
+
def eql?(other)
|
27
|
+
self.class == other.class && BigDecimal.new(self.amount.to_s) == BigDecimal.new(other.amount.to_s) && self.unit == other.unit
|
28
|
+
end
|
29
|
+
|
30
|
+
def <=>(other)
|
31
|
+
if self.class == other.class
|
32
|
+
self.class.convert(self.amount, self.unit, other.unit) <=> other.amount
|
33
|
+
else
|
34
|
+
self.amount <=> other
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def system
|
39
|
+
self.class.units_to_systems[unit]
|
40
|
+
end
|
41
|
+
|
42
|
+
def method_missing(meth, *args)
|
43
|
+
if args.size == 1 && self.class == (other = args.first).class
|
44
|
+
other_amount_in_self_units = self.class.convert(other.amount, other.unit, self.unit)
|
45
|
+
self.class.new(amount.send(meth, other_amount_in_self_units), self.unit)
|
46
|
+
else
|
47
|
+
amount.send(meth, *args)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.conversion_rate(from, to)
|
52
|
+
return nil unless self.conversions[from] and self.conversions[to]
|
53
|
+
return self.conversions[from][to] ||=
|
54
|
+
(1.0 / self.conversions[to][from] if self.conversions[to][from]) || begin
|
55
|
+
shared_conversions = self.conversions[from].keys & self.conversions[to].keys
|
56
|
+
if shared_conversions.any?
|
57
|
+
primitive = shared_conversions.first
|
58
|
+
self.conversions[from][primitive] * (1.0 / self.conversions[to][primitive])
|
59
|
+
else
|
60
|
+
self.conversions[from].each do |conversion_unit, multiple|
|
61
|
+
if self.conversions[to].include?(conversion_unit)
|
62
|
+
return multiple * conversion_rate(conversion) * (1.0 / self.conversions[to][conversion_unit])
|
63
|
+
end
|
64
|
+
end
|
65
|
+
from_primitive = (self.conversions[from].keys & self.primitives).first
|
66
|
+
to_primitive = (self.conversions[to].keys & self.primitives).first
|
67
|
+
if from_primitive_to_primitive_multiple = conversion_rate(from_primitive, to_primitive)
|
68
|
+
return self.conversions[from][from_primitive] * from_primitive_to_primitive_multiple * (1.0 / self.conversions[to][to_primitive])
|
69
|
+
end
|
70
|
+
raise StandardError, "No conversion path from #{from} to #{to}"
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.units(system=nil)
|
76
|
+
if system
|
77
|
+
self.systems_to_units[system.to_sym].dup
|
78
|
+
else
|
79
|
+
self.primitives | self.conversions.keys
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.non_primitives
|
84
|
+
self.conversions.keys
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.systems
|
88
|
+
self.systems_to_units.keys
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.add_numeric_methods?
|
92
|
+
self.add_numeric_methods
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.numeric_methods(*args)
|
96
|
+
args.each do |arg|
|
97
|
+
add_numeric_method_for(arg.to_sym)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
protected
|
102
|
+
|
103
|
+
class << self
|
104
|
+
def primitives; @primitives ||= []; end
|
105
|
+
def add_numeric_methods; @add_numeric_methods ||= false; end
|
106
|
+
def add_numeric_methods=(v); @add_numeric_methods = v; end
|
107
|
+
def conversions; @conversions ||= {}; end
|
108
|
+
def current_system; @current_system; end
|
109
|
+
def current_system=(v); @current_system = v; end
|
110
|
+
def systems_to_units; @systems_to_units ||= {}; end
|
111
|
+
def units_to_systems; @units_to_systems ||= {}; end
|
112
|
+
end
|
113
|
+
|
114
|
+
|
115
|
+
def self.system(system_name, &block)
|
116
|
+
old_system = self.current_system
|
117
|
+
self.current_system = system_name.to_sym
|
118
|
+
yield
|
119
|
+
self.current_system = old_system
|
120
|
+
end
|
121
|
+
|
122
|
+
def self.primitive(sym, options={})
|
123
|
+
unit_sym = (options[:plural] || sym.to_s.pluralize).to_sym
|
124
|
+
self.primitives << unit_sym
|
125
|
+
add_to_system(unit_sym)
|
126
|
+
add_methods_for(unit_sym, options)
|
127
|
+
end
|
128
|
+
|
129
|
+
def self.add_to_system(unit_sym)
|
130
|
+
if self.current_system
|
131
|
+
self.units_to_systems[unit_sym] ||= begin
|
132
|
+
sys_ary = self.systems_to_units[self.current_system] ||= []
|
133
|
+
sys_ary << unit_sym
|
134
|
+
self.current_system
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def self.one(sym, options={})
|
140
|
+
unit_sym = (options[:plural] || sym.to_s.pluralize).to_sym
|
141
|
+
add_to_system(unit_sym)
|
142
|
+
register_unit(unit_sym, options[:is].unit, options[:is].amount)
|
143
|
+
add_methods_for(unit_sym, options)
|
144
|
+
end
|
145
|
+
|
146
|
+
def self.register_unit(multiple_unit, other_unit, multiple)
|
147
|
+
multiple_unit, other_unit = multiple_unit.to_sym, other_unit.to_sym
|
148
|
+
self.conversions[multiple_unit] ||= {}
|
149
|
+
self.conversions[other_unit] ||= {}
|
150
|
+
|
151
|
+
if self.primitives.include?(multiple_unit) || self.primitives.include?(other_unit)
|
152
|
+
add_conversion(multiple_unit, other_unit, multiple)
|
153
|
+
else
|
154
|
+
[multiple_unit, other_unit].each do |this_unit|
|
155
|
+
self.conversions[this_unit].each do |this_other_unit, this_multiple|
|
156
|
+
if self.primitives.include?(this_other_unit)
|
157
|
+
add_conversion(multiple_unit, this_other_unit, multiple * this_multiple)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def self.add_conversion(multiple_unit, other_unit, multiple)
|
165
|
+
self.conversions[multiple_unit] ||={}
|
166
|
+
self.conversions[multiple_unit][other_unit] = multiple
|
167
|
+
self.conversions[other_unit] ||= {}
|
168
|
+
self.conversions[other_unit][multiple_unit] = (1.0 / multiple)
|
169
|
+
end
|
170
|
+
|
171
|
+
def self.convert(amount, from, to)
|
172
|
+
from, to = from.to_sym, to.to_sym
|
173
|
+
amount * conversion_rate(from, to)
|
174
|
+
end
|
175
|
+
|
176
|
+
def self.add_methods_for(sym, options={})
|
177
|
+
add_conversion_method_for(sym, options)
|
178
|
+
add_numeric_method = if options.has_key?(:add_numeric_methods)
|
179
|
+
options[:add_numeric_methods]
|
180
|
+
else
|
181
|
+
self.add_numeric_methods
|
182
|
+
end
|
183
|
+
add_numeric_method_for(sym.to_s, options) if add_numeric_method
|
184
|
+
end
|
185
|
+
|
186
|
+
def self.add_conversion_method_for(sym, options={})
|
187
|
+
unit_name = sym.to_s
|
188
|
+
class_eval do
|
189
|
+
define_method("to_#{unit_name}") do
|
190
|
+
return self if unit_name == self.unit.to_s
|
191
|
+
self.class.new(self.class.convert(self.amount, self.unit, unit_name), unit_name)
|
192
|
+
end
|
193
|
+
alias_method("in_#{unit_name}","to_#{unit_name}")
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
def self.add_numeric_method_for(unit_name, options={})
|
198
|
+
unit_name = unit_name.to_sym
|
199
|
+
raise ArgumentError, "#{unit_name.inspect} is not a unit in #{self.name}" unless units.include?(unit_name)
|
200
|
+
klass = self
|
201
|
+
Numeric.class_eval do
|
202
|
+
define_method(unit_name) do
|
203
|
+
klass.new(self, unit_name.to_sym)
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Quantified
|
2
|
+
class Length < Attribute
|
3
|
+
system :metric do
|
4
|
+
primitive :metre
|
5
|
+
|
6
|
+
one :centimetre, :is => Length.new(0.01, :metres)
|
7
|
+
one :millimetre, :is => Length.new(0.1, :centimetres)
|
8
|
+
one :kilometre, :is => Length.new(1000, :metres)
|
9
|
+
end
|
10
|
+
|
11
|
+
system :imperial do
|
12
|
+
primitive :inch
|
13
|
+
one :inch, :is => Length.new(2.540, :centimetres)
|
14
|
+
|
15
|
+
one :foot, :plural => :feet, :is => Length.new(12, :inches)
|
16
|
+
one :yard, :is => Length.new(3, :feet)
|
17
|
+
one :mile, :is => Length.new(5280, :feet)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Quantified
|
2
|
+
class Mass < Attribute
|
3
|
+
system :metric do
|
4
|
+
primitive :gram
|
5
|
+
|
6
|
+
one :milligram, :is => Mass.new(0.001, :grams)
|
7
|
+
one :kilogram, :is => Mass.new(1000, :grams)
|
8
|
+
end
|
9
|
+
|
10
|
+
system :imperial do
|
11
|
+
primitive :ounce
|
12
|
+
one :ounce, :is => Mass.new(28.349523125, :grams)
|
13
|
+
|
14
|
+
one :pound, :is => Mass.new(16, :ounces)
|
15
|
+
one :stone, :is => Mass.new(14, :pounds)
|
16
|
+
one :short_ton, :is => Mass.new(2000, :pounds)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
require 'quantified/length'
|
3
|
+
|
4
|
+
class LengthTest < Test::Unit::TestCase
|
5
|
+
include Quantified
|
6
|
+
Length.numeric_methods :metres, :centimetres, :inches, :feet
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@length = Length.new(5, :feet)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_inspect
|
13
|
+
assert_equal "#<Quantified::Length: 5 feet>", @length.inspect
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_to_s
|
17
|
+
assert_equal "5 feet", @length.to_s
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_initialize_from_numeric
|
21
|
+
assert_equal "5 feet", 5.feet.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_equalities
|
25
|
+
assert_equal 1.feet, (1.0).feet
|
26
|
+
# == based on value
|
27
|
+
assert_equal 6.feet, Length.new(2, :yards)
|
28
|
+
# eql? based on value and unit
|
29
|
+
assert !6.feet.eql?(Length.new(2, :yards))
|
30
|
+
# equal? based on object identity
|
31
|
+
assert !2.feet.equal?(2.feet)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_convert_yards_to_feet
|
35
|
+
assert 6.feet.eql?(Length.new(2, :yards).to_feet)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_convert_feet_to_yards
|
39
|
+
assert Length.new(2, :yards).eql?(6.feet.to_yards)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_convert_yards_to_millimetres
|
43
|
+
assert Length.new(914.4, :millimetres).eql?(Length.new(1, :yards).to_millimetres)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_convert_millimetres_to_yards
|
47
|
+
assert Length.new(1, :yards).eql?(Length.new(914.4, :millimetres).to_yards)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_convert_metres_to_inches
|
51
|
+
assert_equal 1.inches, (0.0254).metres.to_inches
|
52
|
+
assert 1.inches.eql?((0.0254).metres.to_inches)
|
53
|
+
assert 1.inches.eql?((0.0254).metres.in_inches)
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_comparison_with_numeric
|
57
|
+
assert 2.feet > 1
|
58
|
+
assert 2.feet == 2
|
59
|
+
assert 2.feet <= 2
|
60
|
+
assert 2.feet < 3
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_method_missing_to_i
|
64
|
+
assert_equal 2, (2.4).feet.to_i
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_method_missing_to_f
|
68
|
+
assert_equal 2.4, (2.4).feet.to_f
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_method_missing_minus
|
72
|
+
assert_equal 2.feet, 5.feet - 3.feet
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_numeric_methods_not_added_for_some_units
|
76
|
+
assert_raises NoMethodError do
|
77
|
+
2.yards
|
78
|
+
end
|
79
|
+
assert_raises NoMethodError do
|
80
|
+
2.millimetres
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_systems
|
85
|
+
assert_equal [:metric, :imperial], Length.systems
|
86
|
+
assert_equal [:metres, :centimetres, :millimetres, :kilometres], Length.units(:metric)
|
87
|
+
assert_equal [:inches, :feet, :yards, :miles], Length.units(:imperial)
|
88
|
+
|
89
|
+
assert_equal :metric, 2.centimetres.system
|
90
|
+
assert_equal :imperial, 2.feet.system
|
91
|
+
end
|
92
|
+
end
|