active_shipping 1.0.0.pre2 → 1.0.0.pre3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/.gitignore +12 -0
- data/.travis.yml +14 -0
- data/.yardopts +14 -0
- data/Gemfile +3 -0
- data/Gemfile.activesupport32 +4 -0
- data/Gemfile.activesupport40 +4 -0
- data/Gemfile.activesupport41 +4 -0
- data/Gemfile.activesupport42 +4 -0
- data/Rakefile +23 -0
- data/active_shipping.gemspec +32 -0
- data/lib/active_shipping.rb +1 -1
- data/lib/active_shipping/carrier.rb +1 -1
- data/lib/active_shipping/carriers/shipwire.rb +32 -35
- data/lib/active_shipping/carriers/ups.rb +2 -2
- data/lib/active_shipping/carriers/usps.rb +103 -101
- data/lib/active_shipping/label_response.rb +1 -1
- data/lib/active_shipping/rate_estimate.rb +76 -15
- data/lib/active_shipping/rate_response.rb +20 -0
- data/lib/active_shipping/response.rb +17 -0
- data/lib/active_shipping/shipment_packer.rb +2 -2
- data/lib/active_shipping/shipping_response.rb +24 -2
- data/lib/active_shipping/tracking_response.rb +65 -17
- data/lib/active_shipping/version.rb +1 -1
- data/shipit.rubygems.yml +1 -0
- data/test/credentials.yml +47 -0
- data/test/fixtures/files/label1.pdf +0 -0
- data/test/fixtures/files/ups-shipping-label.gif +0 -0
- data/test/fixtures/json/newzealandpost/domestic_book.json +1 -0
- data/test/fixtures/json/newzealandpost/domestic_default.json +1 -0
- data/test/fixtures/json/newzealandpost/domestic_error.json +1 -0
- data/test/fixtures/json/newzealandpost/domestic_poster.json +1 -0
- data/test/fixtures/json/newzealandpost/domestic_small_half_pound.json +1 -0
- data/test/fixtures/json/newzealandpost/international_book.json +1 -0
- data/test/fixtures/json/newzealandpost/international_new_zealand_wii.json +1 -0
- data/test/fixtures/json/newzealandpost/international_small_half_pound.json +1 -0
- data/test/fixtures/json/newzealandpost/international_wii.json +1 -0
- data/test/fixtures/xml/canadapost/example_request.xml +25 -0
- data/test/fixtures/xml/canadapost/example_response.xml +130 -0
- data/test/fixtures/xml/canadapost/example_response_error.xml +16 -0
- data/test/fixtures/xml/canadapost/example_response_french.xml +122 -0
- data/test/fixtures/xml/canadapost/example_response_with_nil_value.xml +164 -0
- data/test/fixtures/xml/canadapost/example_response_with_postal_outlet.xml +155 -0
- data/test/fixtures/xml/canadapost/example_response_with_postal_outlet_french.xml +274 -0
- data/test/fixtures/xml/canadapost/example_response_with_strange_delivery_date.xml +130 -0
- data/test/fixtures/xml/canadapost_pws/dnc_tracking_details_en.xml +112 -0
- data/test/fixtures/xml/canadapost_pws/merchant_details_error.xml +7 -0
- data/test/fixtures/xml/canadapost_pws/merchant_details_response.xml +7 -0
- data/test/fixtures/xml/canadapost_pws/option_response.xml +13 -0
- data/test/fixtures/xml/canadapost_pws/option_response_no_conflicts.xml +7 -0
- data/test/fixtures/xml/canadapost_pws/rates_info.xml +190 -0
- data/test/fixtures/xml/canadapost_pws/rates_info_error.xml +7 -0
- data/test/fixtures/xml/canadapost_pws/receipt_response.xml +42 -0
- data/test/fixtures/xml/canadapost_pws/receipt_response_no_priced_options.xml +36 -0
- data/test/fixtures/xml/canadapost_pws/register_token_error.xml +7 -0
- data/test/fixtures/xml/canadapost_pws/register_token_response.xml +3 -0
- data/test/fixtures/xml/canadapost_pws/service_options_response.xml +42 -0
- data/test/fixtures/xml/canadapost_pws/services_error.xml +6 -0
- data/test/fixtures/xml/canadapost_pws/services_response.xml +32 -0
- data/test/fixtures/xml/canadapost_pws/shipment_domestic.xml +69 -0
- data/test/fixtures/xml/canadapost_pws/shipment_response.xml +20 -0
- data/test/fixtures/xml/canadapost_pws/shipment_us.xml +69 -0
- data/test/fixtures/xml/canadapost_pws/tracking_details_en.xml +152 -0
- data/test/fixtures/xml/canadapost_pws/tracking_details_en_error.xml +7 -0
- data/test/fixtures/xml/canadapost_pws/tracking_details_en_undelivered.xml +116 -0
- data/test/fixtures/xml/canadapost_pws/tracking_details_fr.xml +156 -0
- data/test/fixtures/xml/canadapost_pws/tracking_details_no_expected_delivery_date.xml +40 -0
- data/test/fixtures/xml/fedex/freight_rate_request.xml +82 -0
- data/test/fixtures/xml/fedex/freight_rate_response.xml +506 -0
- data/test/fixtures/xml/fedex/invalid_fedex_reply.xml +27 -0
- data/test/fixtures/xml/fedex/ottawa_to_beverly_hills_commercial_rate_request.xml +79 -0
- data/test/fixtures/xml/fedex/ottawa_to_beverly_hills_rate_request.xml +80 -0
- data/test/fixtures/xml/fedex/ottawa_to_beverly_hills_rate_response.xml +214 -0
- data/test/fixtures/xml/fedex/raterequest_reply.xml +213 -0
- data/test/fixtures/xml/fedex/reply_without_notifications.xml +185 -0
- data/test/fixtures/xml/fedex/tracking_request.xml +27 -0
- data/test/fixtures/xml/fedex/tracking_response.xml +151 -0
- data/test/fixtures/xml/fedex/tracking_response_empty_destination.xml +76 -0
- data/test/fixtures/xml/fedex/tracking_response_no_destination.xml +139 -0
- data/test/fixtures/xml/fedex/tracking_response_no_ship_time.xml +150 -0
- data/test/fixtures/xml/fedex/tracking_response_with_estimated_delivery_date.xml +95 -0
- data/test/fixtures/xml/fedex/tracking_response_with_shipper_address.xml +71 -0
- data/test/fixtures/xml/fedex/unknown_fedex_document_reply.xml +3 -0
- data/test/fixtures/xml/kunaki/invalid_state_response.xml +3 -0
- data/test/fixtures/xml/kunaki/no_valid_items_response.xml +3 -0
- data/test/fixtures/xml/kunaki/successful_rates_response.xml +3 -0
- data/test/fixtures/xml/kunaki/unsuccessful_rates_response.xml +9 -0
- data/test/fixtures/xml/shipwire/international_rates_response.xml +17 -0
- data/test/fixtures/xml/shipwire/new_carrier_rate_response.xml +18 -0
- data/test/fixtures/xml/shipwire/no_rates_response.xml +7 -0
- data/test/fixtures/xml/shipwire/rates_response.xml +36 -0
- data/test/fixtures/xml/shipwire/rates_response_no_estimate.xml +14 -0
- data/test/fixtures/xml/stamps/authenticate_user_request.xml +15 -0
- data/test/fixtures/xml/stamps/authenticate_user_response.xml +10 -0
- data/test/fixtures/xml/stamps/cleanse_address_request.xml +19 -0
- data/test/fixtures/xml/stamps/cleanse_address_response.xml +27 -0
- data/test/fixtures/xml/stamps/create_indicium_request.xml +69 -0
- data/test/fixtures/xml/stamps/create_indicium_response.xml +40 -0
- data/test/fixtures/xml/stamps/expired_authenticator_response.xml +15 -0
- data/test/fixtures/xml/stamps/get_account_info_request.xml +11 -0
- data/test/fixtures/xml/stamps/get_account_info_response.xml +36 -0
- data/test/fixtures/xml/stamps/get_purchase_status_request.xml +12 -0
- data/test/fixtures/xml/stamps/get_purchase_status_response.xml +16 -0
- data/test/fixtures/xml/stamps/get_rates_request.xml +19 -0
- data/test/fixtures/xml/stamps/get_rates_response.xml +351 -0
- data/test/fixtures/xml/stamps/purchase_postage_request.xml +13 -0
- data/test/fixtures/xml/stamps/purchase_postage_response.xml +17 -0
- data/test/fixtures/xml/stamps/track_shipment_request.xml +12 -0
- data/test/fixtures/xml/stamps/track_shipment_response.xml +45 -0
- data/test/fixtures/xml/ups/delivered_shipment_with_refund.xml +290 -0
- data/test/fixtures/xml/ups/delivered_shipment_without_events_tracking_response.xml +62 -0
- data/test/fixtures/xml/ups/example_tracking_response.xml +53 -0
- data/test/fixtures/xml/ups/in_transit_shipment.xml +183 -0
- data/test/fixtures/xml/ups/out_for_delivery_shipment.xml +165 -0
- data/test/fixtures/xml/ups/shipment_accept_response.xml +42 -0
- data/test/fixtures/xml/ups/shipment_confirm_response.xml +33 -0
- data/test/fixtures/xml/ups/shipment_from_tiger_direct.xml +222 -0
- data/test/fixtures/xml/ups/test_real_home_as_residential_destination_response.xml +1 -0
- data/test/fixtures/xml/ups/test_real_home_as_residential_destination_response_with_insured.xml +289 -0
- data/test/fixtures/xml/ups/test_real_home_as_residential_destination_with_origin_account_response.xml +311 -0
- data/test/fixtures/xml/ups/triple_accept_response.xml +72 -0
- data/test/fixtures/xml/ups/triple_confirm_response.xml +32 -0
- data/test/fixtures/xml/usps/beverly_hills_to_new_york_book_commercial_base_rate_response.xml +2 -0
- data/test/fixtures/xml/usps/beverly_hills_to_new_york_book_commercial_plus_rate_response.xml +258 -0
- data/test/fixtures/xml/usps/beverly_hills_to_new_york_book_rate_response.xml +108 -0
- data/test/fixtures/xml/usps/beverly_hills_to_ottawa_american_wii_commercial_base_rate_response.xml +84 -0
- data/test/fixtures/xml/usps/beverly_hills_to_ottawa_american_wii_commercial_plus_rate_response.xml +212 -0
- data/test/fixtures/xml/usps/beverly_hills_to_ottawa_american_wii_rate_response.xml +230 -0
- data/test/fixtures/xml/usps/delivered_tracking_response.xml +11 -0
- data/test/fixtures/xml/usps/first_class_packages_with_invalid_mail_type_response.xml +12 -0
- data/test/fixtures/xml/usps/first_class_packages_with_mail_type_response.xml +16 -0
- data/test/fixtures/xml/usps/first_class_packages_without_mail_type_response.xml +12 -0
- data/test/fixtures/xml/usps/invalid_xml_tracking_response_error.xml +2 -0
- data/test/fixtures/xml/usps/tracking_request.xml +3 -0
- data/test/fixtures/xml/usps/tracking_response.xml +13 -0
- data/test/fixtures/xml/usps/tracking_response_failure.xml +3 -0
- data/test/fixtures/xml/usps/tracking_response_not_available.xml +12 -0
- data/test/fixtures/xml/usps/tracking_response_test_error.xml +8 -0
- data/test/fixtures/xml/usps/us_rate_request.xml +18 -0
- data/test/fixtures/xml/usps/world_rate_request_with_value.xml +20 -0
- data/test/fixtures/xml/usps/world_rate_request_without_value.xml +20 -0
- data/test/remote/canada_post_pws_platform_test.rb +246 -0
- data/test/remote/canada_post_pws_test.rb +171 -0
- data/test/remote/canada_post_test.rb +53 -0
- data/test/remote/fedex_test.rb +216 -0
- data/test/remote/kunaki_test.rb +36 -0
- data/test/remote/new_zealand_post_test.rb +147 -0
- data/test/remote/shipwire_test.rb +82 -0
- data/test/remote/stamps_test.rb +394 -0
- data/test/remote/ups_test.rb +257 -0
- data/test/remote/usps_test.rb +235 -0
- data/test/test_helper.rb +220 -0
- data/test/unit/carriers/benchmark_test.rb +18 -0
- data/test/unit/carriers/canada_post_pws_rating_test.rb +349 -0
- data/test/unit/carriers/canada_post_pws_register_test.rb +75 -0
- data/test/unit/carriers/canada_post_pws_shipping_test.rb +244 -0
- data/test/unit/carriers/canada_post_pws_tracking_test.rb +154 -0
- data/test/unit/carriers/canada_post_test.rb +145 -0
- data/test/unit/carriers/fedex_test.rb +408 -0
- data/test/unit/carriers/kunaki_test.rb +52 -0
- data/test/unit/carriers/new_zealand_post_test.rb +174 -0
- data/test/unit/carriers/shipwire_test.rb +187 -0
- data/test/unit/carriers/stamps_test.rb +241 -0
- data/test/unit/carriers/ups_test.rb +311 -0
- data/test/unit/carriers/usps_test.rb +484 -0
- data/test/unit/carriers_test.rb +17 -0
- data/test/unit/label_response_test.rb +15 -0
- data/test/unit/location_test.rb +138 -0
- data/test/unit/package_test.rb +68 -0
- data/test/unit/rate_estimate_test.rb +34 -0
- data/test/unit/response_test.rb +14 -0
- data/test/unit/shipment_packer_test.rb +174 -0
- metadata +331 -35
- metadata.gz.sig +0 -0
- data/lib/vendor/quantified/MIT-LICENSE +0 -22
- data/lib/vendor/quantified/README.markdown +0 -49
- data/lib/vendor/quantified/Rakefile +0 -13
- data/lib/vendor/quantified/init.rb +0 -0
- data/lib/vendor/quantified/lib/quantified.rb +0 -6
- data/lib/vendor/quantified/lib/quantified/attribute.rb +0 -211
- data/lib/vendor/quantified/lib/quantified/length.rb +0 -20
- data/lib/vendor/quantified/lib/quantified/mass.rb +0 -19
- data/lib/vendor/quantified/test/length_test.rb +0 -94
- data/lib/vendor/quantified/test/mass_test.rb +0 -96
- data/lib/vendor/quantified/test/test_helper.rb +0 -19
@@ -1,49 +0,0 @@
|
|
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
|
@@ -1,13 +0,0 @@
|
|
1
|
-
require 'rake'
|
2
|
-
require 'rake/testtask'
|
3
|
-
|
4
|
-
desc 'Default: run unit tests.'
|
5
|
-
task :default => :test
|
6
|
-
|
7
|
-
desc 'Test the plugin.'
|
8
|
-
Rake::TestTask.new(:test) do |t|
|
9
|
-
t.libs << 'lib'
|
10
|
-
t.libs << 'test'
|
11
|
-
t.pattern = 'test/**/*_test.rb'
|
12
|
-
t.verbose = true
|
13
|
-
end
|
File without changes
|
@@ -1,211 +0,0 @@
|
|
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(amount.to_s) == BigDecimal.new(other.amount.to_s) && unit == other.unit) || BigDecimal.new(self.class.convert(amount, unit, other.unit).to_s) == BigDecimal.new(other.amount.to_s)
|
22
|
-
rescue NoMethodError
|
23
|
-
amount == other
|
24
|
-
end
|
25
|
-
|
26
|
-
def eql?(other)
|
27
|
-
self.class == other.class && BigDecimal.new(amount.to_s) == BigDecimal.new(other.amount.to_s) && unit == other.unit
|
28
|
-
end
|
29
|
-
|
30
|
-
def <=>(other)
|
31
|
-
if self.class == other.class
|
32
|
-
self.class.convert(amount, unit, other.unit) <=> other.amount
|
33
|
-
else
|
34
|
-
amount <=> other
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def system
|
39
|
-
self.class.units_to_systems[unit]
|
40
|
-
end
|
41
|
-
|
42
|
-
def coerce(other)
|
43
|
-
[other, amount]
|
44
|
-
end
|
45
|
-
|
46
|
-
def method_missing(meth, *args)
|
47
|
-
if args.size == 1 && self.class == (other = args.first).class
|
48
|
-
other_amount_in_self_units = self.class.convert(other.amount, other.unit, unit)
|
49
|
-
self.class.new(amount.send(meth, other_amount_in_self_units), unit)
|
50
|
-
else
|
51
|
-
amount.send(meth, *args)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def self.conversion_rate(from, to)
|
56
|
-
return nil unless conversions[from] and conversions[to]
|
57
|
-
conversions[from][to] ||=
|
58
|
-
(1.0 / conversions[to][from] if conversions[to][from]) || begin
|
59
|
-
shared_conversions = conversions[from].keys & conversions[to].keys
|
60
|
-
if shared_conversions.any?
|
61
|
-
primitive = shared_conversions.first
|
62
|
-
conversions[from][primitive] * (1.0 / conversions[to][primitive])
|
63
|
-
else
|
64
|
-
conversions[from].each do |conversion_unit, multiple|
|
65
|
-
if conversions[to].include?(conversion_unit)
|
66
|
-
return multiple * conversion_rate(conversion) * (1.0 / conversions[to][conversion_unit])
|
67
|
-
end
|
68
|
-
end
|
69
|
-
from_primitive = (conversions[from].keys & primitives).first
|
70
|
-
to_primitive = (conversions[to].keys & primitives).first
|
71
|
-
if from_primitive_to_primitive_multiple = conversion_rate(from_primitive, to_primitive)
|
72
|
-
return conversions[from][from_primitive] * from_primitive_to_primitive_multiple * (1.0 / conversions[to][to_primitive])
|
73
|
-
end
|
74
|
-
raise StandardError, "No conversion path from #{from} to #{to}"
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
def self.units(system = nil)
|
80
|
-
if system
|
81
|
-
systems_to_units[system.to_sym].dup
|
82
|
-
else
|
83
|
-
primitives | conversions.keys
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def self.non_primitives
|
88
|
-
conversions.keys
|
89
|
-
end
|
90
|
-
|
91
|
-
def self.systems
|
92
|
-
systems_to_units.keys
|
93
|
-
end
|
94
|
-
|
95
|
-
def self.add_numeric_methods?
|
96
|
-
add_numeric_methods
|
97
|
-
end
|
98
|
-
|
99
|
-
def self.numeric_methods(*args)
|
100
|
-
args.each do |arg|
|
101
|
-
add_numeric_method_for(arg.to_sym)
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
protected
|
106
|
-
|
107
|
-
class << self
|
108
|
-
def primitives; @primitives ||= []; end
|
109
|
-
def add_numeric_methods; @add_numeric_methods ||= false; end
|
110
|
-
attr_writer :add_numeric_methods
|
111
|
-
def conversions; @conversions ||= {}; end
|
112
|
-
attr_reader :current_system
|
113
|
-
attr_writer :current_system
|
114
|
-
def systems_to_units; @systems_to_units ||= {}; end
|
115
|
-
def units_to_systems; @units_to_systems ||= {}; end
|
116
|
-
end
|
117
|
-
|
118
|
-
def self.system(system_name, &block)
|
119
|
-
old_system = current_system
|
120
|
-
self.current_system = system_name.to_sym
|
121
|
-
yield
|
122
|
-
self.current_system = old_system
|
123
|
-
end
|
124
|
-
|
125
|
-
def self.primitive(sym, options = {})
|
126
|
-
unit_sym = (options[:plural] || sym.to_s.pluralize).to_sym
|
127
|
-
primitives << unit_sym
|
128
|
-
add_to_system(unit_sym)
|
129
|
-
add_methods_for(unit_sym, options)
|
130
|
-
end
|
131
|
-
|
132
|
-
def self.add_to_system(unit_sym)
|
133
|
-
if current_system
|
134
|
-
units_to_systems[unit_sym] ||= begin
|
135
|
-
sys_ary = systems_to_units[current_system] ||= []
|
136
|
-
sys_ary << unit_sym
|
137
|
-
current_system
|
138
|
-
end
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
def self.one(sym, options = {})
|
143
|
-
unit_sym = (options[:plural] || sym.to_s.pluralize).to_sym
|
144
|
-
add_to_system(unit_sym)
|
145
|
-
register_unit(unit_sym, options[:is].unit, options[:is].amount)
|
146
|
-
add_methods_for(unit_sym, options)
|
147
|
-
end
|
148
|
-
|
149
|
-
def self.register_unit(multiple_unit, other_unit, multiple)
|
150
|
-
multiple_unit, other_unit = multiple_unit.to_sym, other_unit.to_sym
|
151
|
-
conversions[multiple_unit] ||= {}
|
152
|
-
conversions[other_unit] ||= {}
|
153
|
-
|
154
|
-
if primitives.include?(multiple_unit) || primitives.include?(other_unit)
|
155
|
-
add_conversion(multiple_unit, other_unit, multiple)
|
156
|
-
else
|
157
|
-
[multiple_unit, other_unit].each do |this_unit|
|
158
|
-
conversions[this_unit].each do |this_other_unit, this_multiple|
|
159
|
-
if primitives.include?(this_other_unit)
|
160
|
-
add_conversion(multiple_unit, this_other_unit, multiple * this_multiple)
|
161
|
-
end
|
162
|
-
end
|
163
|
-
end
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
def self.add_conversion(multiple_unit, other_unit, multiple)
|
168
|
-
conversions[multiple_unit] ||= {}
|
169
|
-
conversions[multiple_unit][other_unit] = multiple
|
170
|
-
conversions[other_unit] ||= {}
|
171
|
-
conversions[other_unit][multiple_unit] = (1.0 / multiple)
|
172
|
-
end
|
173
|
-
|
174
|
-
def self.convert(amount, from, to)
|
175
|
-
from, to = from.to_sym, to.to_sym
|
176
|
-
amount * conversion_rate(from, to)
|
177
|
-
end
|
178
|
-
|
179
|
-
def self.add_methods_for(sym, options = {})
|
180
|
-
add_conversion_method_for(sym, options)
|
181
|
-
add_numeric_method = if options.has_key?(:add_numeric_methods)
|
182
|
-
options[:add_numeric_methods]
|
183
|
-
else
|
184
|
-
add_numeric_methods
|
185
|
-
end
|
186
|
-
add_numeric_method_for(sym.to_s, options) if add_numeric_method
|
187
|
-
end
|
188
|
-
|
189
|
-
def self.add_conversion_method_for(sym, options = {})
|
190
|
-
unit_name = sym.to_s
|
191
|
-
class_eval do
|
192
|
-
define_method("to_#{unit_name}") do
|
193
|
-
return self if unit_name == unit.to_s
|
194
|
-
self.class.new(self.class.convert(amount, unit, unit_name), unit_name)
|
195
|
-
end
|
196
|
-
alias_method("in_#{unit_name}", "to_#{unit_name}")
|
197
|
-
end
|
198
|
-
end
|
199
|
-
|
200
|
-
def self.add_numeric_method_for(unit_name, options = {})
|
201
|
-
unit_name = unit_name.to_sym
|
202
|
-
raise ArgumentError, "#{unit_name.inspect} is not a unit in #{name}" unless units.include?(unit_name)
|
203
|
-
klass = self
|
204
|
-
Numeric.class_eval do
|
205
|
-
define_method(unit_name) do
|
206
|
-
klass.new(self, unit_name.to_sym)
|
207
|
-
end
|
208
|
-
end
|
209
|
-
end
|
210
|
-
end
|
211
|
-
end
|
@@ -1,20 +0,0 @@
|
|
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(0.0254, :metres)
|
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
|
@@ -1,19 +0,0 @@
|
|
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
|
@@ -1,94 +0,0 @@
|
|
1
|
-
require '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_mm_to_inches
|
35
|
-
assert_equal 12, Length.new(304.8, :millimetres).to_inches
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_convert_yards_to_feet
|
39
|
-
assert 6.feet.eql?(Length.new(2, :yards).to_feet)
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_convert_feet_to_yards
|
43
|
-
assert Length.new(2, :yards).eql?(6.feet.to_yards)
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_convert_yards_to_millimetres
|
47
|
-
assert_in_epsilon Length.new(914.4, :millimetres).to_f, Length.new(1, :yards).to_millimetres.to_f
|
48
|
-
end
|
49
|
-
|
50
|
-
def test_convert_millimetres_to_yards
|
51
|
-
assert_in_epsilon Length.new(1, :yards).to_f, Length.new(914.4, :millimetres).to_yards.to_f
|
52
|
-
end
|
53
|
-
|
54
|
-
def test_convert_metres_to_inches
|
55
|
-
assert_in_epsilon 1.inches.to_f, (0.0254).metres.to_inches.to_f
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_comparison_with_numeric
|
59
|
-
assert 2.feet > 1
|
60
|
-
assert 2.feet == 2
|
61
|
-
assert 2.feet <= 2
|
62
|
-
assert 2.feet < 3
|
63
|
-
end
|
64
|
-
|
65
|
-
def test_method_missing_to_i
|
66
|
-
assert_equal 2, (2.4).feet.to_i
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_method_missing_to_f
|
70
|
-
assert_equal 2.4, (2.4).feet.to_f
|
71
|
-
end
|
72
|
-
|
73
|
-
def test_method_missing_minus
|
74
|
-
assert_equal 2.feet, 5.feet - 3.feet
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_numeric_methods_not_added_for_some_units
|
78
|
-
assert_raises(NoMethodError) do
|
79
|
-
2.yards
|
80
|
-
end
|
81
|
-
assert_raises(NoMethodError) do
|
82
|
-
2.millimetres
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
def test_systems
|
87
|
-
assert_equal [:metric, :imperial], Length.systems
|
88
|
-
assert_equal [:metres, :centimetres, :millimetres, :kilometres], Length.units(:metric)
|
89
|
-
assert_equal [:inches, :feet, :yards, :miles], Length.units(:imperial)
|
90
|
-
|
91
|
-
assert_equal :metric, 2.centimetres.system
|
92
|
-
assert_equal :imperial, 2.feet.system
|
93
|
-
end
|
94
|
-
end
|
@@ -1,96 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'quantified/mass'
|
3
|
-
|
4
|
-
class MassTest < Test::Unit::TestCase
|
5
|
-
include Quantified
|
6
|
-
Mass.numeric_methods :grams, :kilograms, :ounces, :pounds
|
7
|
-
|
8
|
-
def setup
|
9
|
-
@mass = Mass.new(5, :pounds)
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_inspect
|
13
|
-
assert_equal "#<Quantified::Mass: 5 pounds>", @mass.inspect
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_to_s
|
17
|
-
assert_equal "5 pounds", @mass.to_s
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_initialize_from_numeric
|
21
|
-
assert_equal "5 pounds", 5.pounds.to_s
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_equalities
|
25
|
-
assert_equal 1.pounds, (1.0).pounds
|
26
|
-
# == based on value
|
27
|
-
assert_equal 4000.pounds, Mass.new(2, :short_tons)
|
28
|
-
# eql? based on value and unit
|
29
|
-
assert !4000.pounds.eql?(Mass.new(2, :short_tons))
|
30
|
-
# equal? based on object identity
|
31
|
-
assert !2.pounds.equal?(2.pounds)
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_convert_short_tons_to_pounds
|
35
|
-
assert 4000.pounds.eql?(Mass.new(2, :short_tons).to_pounds)
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_convert_pounds_to_short_tons
|
39
|
-
assert Mass.new(2, :short_tons).eql?(4000.pounds.to_short_tons)
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_convert_short_tons_to_milligrams
|
43
|
-
assert Mass.new(907_184_740, :milligrams).eql?(Mass.new(1, :short_tons).to_milligrams)
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_convert_milligrams_to_short_tons
|
47
|
-
assert Mass.new(1, :short_tons).eql?(Mass.new(907_184_740, :milligrams).to_short_tons)
|
48
|
-
end
|
49
|
-
|
50
|
-
def test_convert_grams_to_ounces
|
51
|
-
assert 1.ounces.eql?((28.349523125).grams.to_ounces)
|
52
|
-
assert 1.ounces.eql?((28.349523125).grams.in_ounces)
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_comparison_with_numeric
|
56
|
-
assert 2.pounds > 1
|
57
|
-
assert 2.pounds == 2
|
58
|
-
assert 2.pounds <= 2
|
59
|
-
assert 2.pounds < 3
|
60
|
-
end
|
61
|
-
|
62
|
-
def test_method_missing_to_i
|
63
|
-
assert_equal 2, (2.4).pounds.to_i
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_method_missing_to_f
|
67
|
-
assert_equal 2.4, (2.4).pounds.to_f
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_method_missing_minus
|
71
|
-
assert_equal 2.pounds, 5.pounds - 3.pounds
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_numeric_methods_not_added_for_some_units
|
75
|
-
assert_raises NoMethodError do
|
76
|
-
2.short_tons
|
77
|
-
end
|
78
|
-
assert_raises NoMethodError do
|
79
|
-
2.milligrams
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
def test_systems
|
84
|
-
assert_equal [:metric, :imperial], Mass.systems
|
85
|
-
assert_equal [:grams, :milligrams, :kilograms], Mass.units(:metric)
|
86
|
-
assert_equal [:ounces, :pounds, :stones, :short_tons], Mass.units(:imperial)
|
87
|
-
end
|
88
|
-
|
89
|
-
def test_right_side_comparison_with_fixnum
|
90
|
-
assert Mass.new(14, :grams) < 20
|
91
|
-
end
|
92
|
-
|
93
|
-
def test_left_side_comparison_with_fixnum
|
94
|
-
assert 20 > Mass.new(14, :grams)
|
95
|
-
end
|
96
|
-
end
|