ruby-avm-library/lib/avm/creator.rb

60 lines
1.2 KiB
Ruby
Raw Normal View History

2011-03-07 21:38:20 +00:00
require 'avm/contact'
2011-03-09 15:14:31 +00:00
require 'nokogiri'
2011-03-07 21:38:20 +00:00
2011-03-07 17:39:47 +00:00
module AVM
class Creator
2011-03-07 21:38:20 +00:00
attr_reader :contacts
2011-03-09 15:14:31 +00:00
PRIMARY_CONTACT_FIELDS = [ :address, :city, :state, :province, :postal_code, :zip, :country ]
2011-03-07 17:39:47 +00:00
def initialize
@options = {}
2011-03-07 21:38:20 +00:00
@contacts = []
2011-03-07 17:39:47 +00:00
end
def merge!(hash)
@options.merge!(hash)
end
def method_missing(key, *opts)
if key.to_s[-1..-1] == '='
@options[key.to_s[0..-2].to_sym] = opts.first
else
2011-03-09 15:14:31 +00:00
if PRIMARY_CONTACT_FIELDS.include?(key)
primary_contact_field key
else
@options[key]
end
2011-03-07 17:39:47 +00:00
end
end
2011-03-07 21:38:20 +00:00
2011-03-09 15:14:31 +00:00
def add_to_rdf(rdf)
creator = rdf.add_child('<dc:creator><rdf:Seq></rdf:Seq></dc:creator>')
list = creator.at_xpath('.//rdf:Seq')
contacts.sort.each { |contact| list.add_child(contact.to_creator_list_element) }
end
def primary_contact
@contacts.find(&:primary) || @contacts.sort.first
end
def create_contact(info)
contact = Contact.new(info)
contacts << contact
contact
end
2011-03-07 21:38:20 +00:00
private
def primary_contact_field(field)
if contact = primary_contact
contact.send(field)
else
nil
end
end
2011-03-07 17:39:47 +00:00
end
end