start work on contacts
This commit is contained in:
parent
b41516c13b
commit
8f21381475
24
lib/avm/contact.rb
Normal file
24
lib/avm/contact.rb
Normal file
@ -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
|
@ -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
|
||||
|
||||
|
50
spec/avm/contact_spec.rb
Normal file
50
spec/avm/contact_spec.rb
Normal file
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user