Skip to content

Commit

Permalink
Add address_type= so address type can be changed
Browse files Browse the repository at this point in the history
  • Loading branch information
John Duff committed Sep 19, 2011
1 parent 933147c commit c04370b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/active_shipping/shipping/location.rb
Expand Up @@ -37,14 +37,9 @@ def initialize(options = {})
@address3 = options[:address3]
@phone = options[:phone]
@fax = options[:fax]
if options[:address_type].present?
@address_type = options[:address_type].to_s
unless ADDRESS_TYPES.include?(@address_type)
raise ArgumentError.new("address_type must be one of #{ADDRESS_TYPES.map(&:inspect).join(', ')}")
end
end

@company_name = options[:company_name] || options[:company]

self.address_type = options[:address_type]
end

def self.from(object, options={})
Expand Down Expand Up @@ -90,6 +85,11 @@ def residential?; @address_type == 'residential' end
def commercial?; @address_type == 'commercial' end
def po_box?; @address_type == 'po_box' end

def address_type=(value)
return unless value.present?
raise ArgumentError.new("address_type must be one of #{ADDRESS_TYPES.join(', ')}") unless ADDRESS_TYPES.include?(value.to_s)
@address_type = value.to_s
end

def to_hash
{
Expand Down
18 changes: 18 additions & 0 deletions test/unit/location_test.rb
Expand Up @@ -61,4 +61,22 @@ def test_location_with_company_name
location = Location.from(:company_name => "Mine")
assert_equal "Mine", location.company_name
end

def test_set_address_type
location = @locations[:ottawa]
assert !location.commercial?

location.address_type = :commercial
assert location.commercial?
end

def test_set_address_type_invalid
location = @locations[:ottawa]

assert_raises ArgumentError do
location.address_type = :new_address_type
end

assert_not_equal "new_address_type", location.address_type
end
end

0 comments on commit c04370b

Please sign in to comment.