Skip to content

Commit

Permalink
Add composed_of :address to Event, and implement a reasonable == meth…
Browse files Browse the repository at this point in the history
…od for Address.
  • Loading branch information
marnen committed Jan 7, 2009
1 parent d090313 commit 2efa0c7
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
34 changes: 32 additions & 2 deletions app/models/address.rb
Expand Up @@ -6,12 +6,42 @@ class Address
attr_reader f
end

# Initializes a new #Address object from a #Hash of fields.
def initialize(opts = {})
# Initializes a new #Address object from a #Hash of fields or separate arguments in the order given in FIELDS.
def initialize(*args)
opts = {}
case args.length
when 0
# do nothing
when 1
if args[0].kind_of?(Hash)
opts = args[0]
else
raise ArgumentError, "expected Hash, got #{args[0].class.name}"
end
when FIELDS.length
FIELDS.each_index do |i|
opts[FIELDS[i]] = args[i]
end
else
raise ArgumentError, "expected 0, 1, or #{FIELDS.length} arguments, got #{args.length}"
end
FIELDS.each do |f|
if opts.has_key?(f)
eval "@#{f.to_s} = opts[:#{f.to_s}]"
end
end
end

def ==(other)
if other.kind_of?(Address)
FIELDS.each do |f|
if self.send(f) != other.send(f)
return false
end
end
return true
else
return false
end
end
end
1 change: 1 addition & 0 deletions app/models/event.rb
Expand Up @@ -6,6 +6,7 @@ class Event < ActiveRecord::Base
belongs_to :calendar
has_many :commitments
has_many :users, :through => :commitments
composed_of :address, :mapping => %w(street street2 city state zip).collect{|i| [i, i]}
# validates_presence_of :city
validates_presence_of :calendar_id
validates_presence_of :name
Expand Down
43 changes: 39 additions & 4 deletions spec/models/address_spec.rb
Expand Up @@ -26,13 +26,16 @@
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')
before(:each) do
@state = mock_model(State, :code => 'NY', :country => mock_model(Country, :code => 'US'))
end

it "should set all options passed in on legitimate field keys in a Hash" do
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.state.should == @state
a.zip.should == '12345'
end

Expand All @@ -41,5 +44,37 @@
@a.street.should == 'real field'
@a.should_not respond_to(:bogus)
end

it "should accept 5 separate arguments for street, street2, city, state, and zip" do
street = '123 Main Street'
street2 = 'Apt. 1'
city = 'Anytown'
zip = '12345'

a = Address.new(street, street2, city, @state, zip)
a.street.should == street
a.street2.should == street2
a.city.should == city
a.state.should == @state
a.zip.should == zip
end
end

describe '(methods)' do
describe '==' do
it "should only compare value, not object identity" do
opts = {:street => 'street', :zip => 'zip', :state => mock_model(State)}
a = Address.new(opts)
b = Address.new(opts)
a.should_not equal b # object identity
a.should == b # value identity

b = opts # Hash, not Address
a.should_not == b

a = Address.new(opts.update(:street2 => 'street2'))
a.should_not == b
end
end
end
end
8 changes: 8 additions & 0 deletions spec/models/event_spec.rb
Expand Up @@ -42,6 +42,14 @@
event.country.should == event.state.country
end

it "should be composed_of an Address" do
aggr = Event.reflect_on_aggregation(:address)
aggr.should_not be_nil
aggr.options[:mapping].should == %w(street street2 city state zip).collect{|i| [i, i]}
e = Event.new(:street => '123 Main Street', :street2 => '1st floor', :city => 'Anytown', :zip => 12345, :state => mock_model(State, :code => 'NY', :country => mock_model(Country, :code => 'US')))
e.address.should be_a_kind_of(Address)
end

it "should have a deleted property" do
event = Event.new
event.should respond_to(:deleted)
Expand Down

0 comments on commit 2efa0c7

Please sign in to comment.