Skip to content

Commit

Permalink
fixed bug related to required properties in Packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Yianilos committed Sep 18, 2012
1 parent 565f3d3 commit a1cd232
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
8 changes: 6 additions & 2 deletions lib/usps/request/package/base.rb
Expand Up @@ -6,7 +6,7 @@ class Base
attr_accessor :size
attr_accessor :width, :length, :height, :girth

@@required = [:id, :pounds, :ounces, :size]
@required = [:id, :pounds, :ounces, :size]

def initialize(fields)
fields.each { |name, value| send("#{name}=", value) }
Expand All @@ -19,13 +19,17 @@ def initialize(fields)
end
end

@@required.each do |field|
self.class.required_properties.each do |field|
error "#{field} is required" unless send(field)
end
end

protected

def self.required_properties
@required + (defined?(super) ? super : [])
end

def error(message)
raise ArgumentError.new message
end
Expand Down
2 changes: 1 addition & 1 deletion lib/usps/request/package/domestic_package.rb
Expand Up @@ -6,7 +6,7 @@ class DomesticPackage < Base
attr_accessor :value
attr_accessor :amount_to_collect

@@required += [:service, :origin_zip, :destination_zip]
@required = [:service, :origin_zip, :destination_zip]

def initialize(fields = {})
if fields[:service] == 'FIRST CLASS' and !fields[:first_class_mail_type]
Expand Down
2 changes: 1 addition & 1 deletion lib/usps/request/package/international_package.rb
Expand Up @@ -2,7 +2,7 @@ module USPS::Request::Package
class InternationalPackage < Base
attr_accessor :country, :mail_type

@@required += [:country, :mail_type]
@required = [:country, :mail_type]

def initialize(fields = {})
super
Expand Down
2 changes: 1 addition & 1 deletion spec/response/international_shipping_rates_lookup_spec.rb
Expand Up @@ -17,7 +17,7 @@

package.services[1].tap do |express|
express.id.should == '1'
express.rate.should == '103.00'
express.rate.should == '73.45'
express.description.should =~ /Express Mail/
end
end
Expand Down

0 comments on commit a1cd232

Please sign in to comment.