Skip to content

Commit

Permalink
Add a few more fields to the Address object.
Browse files Browse the repository at this point in the history
  • Loading branch information
marnen committed Jan 7, 2009
1 parent 748478d commit d090313
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
16 changes: 15 additions & 1 deletion app/models/address.rb
@@ -1,3 +1,17 @@
# Value object for addresses.

class Address
attr_reader :street, :street2
FIELDS = [:street, :street2, :city, :state, :zip]
FIELDS.each do |f|
attr_reader f
end

# Initializes a new #Address object from a #Hash of fields.
def initialize(opts = {})
FIELDS.each do |f|
if opts.has_key?(f)
eval "@#{f.to_s} = opts[:#{f.to_s}]"
end
end
end
end
30 changes: 30 additions & 0 deletions spec/models/address_spec.rb
Expand Up @@ -11,5 +11,35 @@
@address.should respond_to(m)
end
end

it "should have a city field" do
@address.should respond_to(:city)
end

it "should have a state field" do
@address.should respond_to(:state)
end

it "should have a zip field" do
@address.should respond_to(:zip)
end
end

describe '(constructor)' do
it "should set all options passed in on legitimate fields" do
state = mock_model(State, :code => 'NY', :country => mock_model(Country, :code => 'US'))
a = Address.new(:street => '123 Main Street', :street2 => 'Apt. 1', :city => 'Anytown', :state => state, :zip => '12345')
a.street.should == '123 Main Street'
a.street2.should == 'Apt. 1'
a.city.should == 'Anytown'
a.state.should == state
a.zip.should == '12345'
end

it "should ignore bogus fields in the constructor" do
lambda { @a = Address.new(:street => 'real field', :bogus => 'bogus field') }.should_not raise_error
@a.street.should == 'real field'
@a.should_not respond_to(:bogus)
end
end
end

0 comments on commit d090313

Please sign in to comment.