diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..b71bde1 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +-c diff --git a/lib/avm/contact.rb b/lib/avm/contact.rb new file mode 100644 index 0000000..00dff9b --- /dev/null +++ b/lib/avm/contact.rb @@ -0,0 +1,24 @@ +module AVM + class Contact + FIELD_MAP = { + :zip => :postal_code, + :state => :state_province, + :province => :state_province + } + + attr_accessor :primary + + def initialize(info) + @info = Hash[info.collect { |key, value| [ FIELD_MAP[key] || key, value ] }] + @primary = false + end + + def method_missing(key) + @info[FIELD_MAP[key] || key] + end + + def <=>(other) + self.name <=> other.name + end + end +end diff --git a/lib/avm/creator.rb b/lib/avm/creator.rb index 2239744..7e9fb85 100644 --- a/lib/avm/creator.rb +++ b/lib/avm/creator.rb @@ -1,13 +1,22 @@ +require 'avm/contact' + module AVM class Creator + attr_reader :contacts + def initialize @options = {} + @contacts = [] end def merge!(hash) @options.merge!(hash) end + def address + primary_contact_field :address + end + def method_missing(key, *opts) if key.to_s[-1..-1] == '=' @options[key.to_s[0..-2].to_sym] = opts.first @@ -15,6 +24,19 @@ module AVM @options[key] end end + + private + def primary_contact_field(field) + if contact = primary_contact + contact.send(field) + else + nil + end + end + + def primary_contact + @contacts.find(&:primary) || @contacts.sort.first + end end end diff --git a/spec/avm/contact_spec.rb b/spec/avm/contact_spec.rb new file mode 100644 index 0000000..c28371f --- /dev/null +++ b/spec/avm/contact_spec.rb @@ -0,0 +1,50 @@ +require 'spec_helper' +require 'avm/contact' + +describe AVM::Contact do + let(:contact) { AVM::Contact.new(contact_info) } + + subject { contact } + + let(:contact_info) { { + :name => name, + :email => email, + :telephone => telephone, + :address => address, + :city => city, + :state => state, + :postal_code => postal_code, + :country => country + } } + + let(:name) { 'John Bintz' } + let(:email) { 'bintz@stsci.edu' } + let(:telephone) { '800-555-1234' } + let(:address) { '3700 San Martin Drive' } + let(:city) { 'Baltimore' } + let(:state) { 'Maryland' } + let(:postal_code) { '21218' } + let(:country) { 'USA' } + + its(:name) { should == name } + its(:email) { should == email } + its(:telephone) { should == telephone } + its(:address) { should == address } + its(:city) { should == city } + its(:state) { should == state } + its(:province) { should == state } + its(:postal_code) { should == postal_code } + its(:zip) { should == postal_code } + its(:country) { should == country } + + describe 'mappings' do + AVM::Contact::FIELD_MAP.each do |key, value| + context "#{key} => #{value}" do + let(:contact_info) { { key => "test" } } + + its(value) { should == "test" } + end + end + end +end + diff --git a/spec/avm/creator_spec.rb b/spec/avm/creator_spec.rb index 0ba4433..87ceb2a 100644 --- a/spec/avm/creator_spec.rb +++ b/spec/avm/creator_spec.rb @@ -33,4 +33,36 @@ describe AVM::Creator do can_read_properties end + + describe 'primary contact passthrough' do + context 'no contacts' do + its(:address) { should be_nil } + end + + context 'one contact, must be primary' do + let(:first_contact) { AVM::Contact.new(:name => 'zz bill', :address => first_contact_address) } + let(:first_contact_address) { 'first contact' } + + before { creator.contacts << first_contact } + + its(:address) { should == first_contact_address } + + context 'two contacts' do + let(:second_contact) { AVM::Contact.new(:name => 'aa bill', :address => second_contact_address) } + let(:second_contact_address) { 'second contact' } + + before { creator.contacts << second_contact } + + context 'no primary, first alphabetical is primary' do + its(:address) { should == second_contact_address } + end + + context 'one is primary, use it' do + before { first_contact.primary = true } + + its(:address) { should == first_contact_address } + end + end + end + end end