Class: AVM::Creator

Inherits:
Object
  • Object
show all
Defined in:
lib/avm/creator.rb

Overview

A container for Contacts (contributors to an image)

Constant Summary collapse

IPTC_CORE_FIELDS =
[ :address, :city, :state, :zip, :country ]
PRIMARY_CONTACT_FIELDS =
IPTC_CORE_FIELDS + [ :province, :postal_code ]
IPTC_MULTI_FIELD_MAP =
[ [ :telephone, 'CiTelWork' ], [ :email, 'CiEmailWork' ] ]
IPTC_CORE_FIELD_ELEMENT_NAMES =
%w{CiAdrExtadr CiAdrCity CiAdrRegion CiAdrPcode CiAdrCtry}
IPTC_CORE_FIELDS_AND_NAMES =
IPTC_CORE_FIELDS.zip(IPTC_CORE_FIELD_ELEMENT_NAMES)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image, given_contacts = []) ⇒ Creator

Returns a new instance of Creator.



15
16
17
18
19
# File 'lib/avm/creator.rb', line 15

def initialize(image, given_contacts = [])
  @options = {}
  @contacts = given_contacts
  @image = image
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *opts) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/avm/creator.rb', line 37

def method_missing(key, *opts)
  if (key_to_s = key.to_s)[-1..-1] == '='
    @options[key_to_s[0..-2].to_sym] = opts.first
  else
    if PRIMARY_CONTACT_FIELDS.include?(key)
      primary_contact_field key
    else
      @options[key]
    end
  end
end

Instance Attribute Details

#contactsObject (readonly)

Returns the value of attribute contacts.



7
8
9
# File 'lib/avm/creator.rb', line 7

def contacts
  @contacts
end

#imageObject (readonly)

Returns the value of attribute image.



7
8
9
# File 'lib/avm/creator.rb', line 7

def image
  @image
end

Instance Method Details

#[](which) ⇒ Object



29
30
31
# File 'lib/avm/creator.rb', line 29

def [](which)
  contacts[which]
end

#add_to_document(document) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/avm/creator.rb', line 49

def add_to_document(document)
  document.get_refs do |refs|
    creator = refs[:dublin_core].add_child('<dc:creator><rdf:Seq></rdf:Seq></dc:creator>')

    list = creator.at_xpath('.//rdf:Seq')
    contact_info = refs[:iptc].add_child('<Iptc4xmpCore:CreatorContactInfo rdf:parseType="Resource" />').first

    contacts.sort.each do |contact| 
      list.add_child(contact.to_creator_list_element) 
    end

    if primary_contact
      IPTC_MULTI_FIELD_MAP.each do |key, element_name|
        contact_info.add_child "<Iptc4xmpCore:#{element_name}>#{contacts.sort.collect(&key).join(',')}</Iptc4xmpCore:#{element_name}>"
      end

      iptc_namespace = document.doc.root.namespace_scopes.find { |ns| ns.prefix == 'Iptc4xmpCore' }

      IPTC_CORE_FIELDS_AND_NAMES.each do |key, element_name|
        node = contact_info.document.create_element(element_name, primary_contact.send(key))
        node.namespace = iptc_namespace
        contact_info.add_child node
      end
    end
  end
end

#create_contact(info) ⇒ Object



107
108
109
110
111
# File 'lib/avm/creator.rb', line 107

def create_contact(info)
  contact = Contact.new(info)
  contacts << contact
  contact
end

#from_xml(image, document) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/avm/creator.rb', line 76

def from_xml(image, document)
  contacts = []
  document.get_refs do |refs|
    refs[:dublin_core].search('.//dc:creator//rdf:li').each do |name|
      contacts << { :name => name.text.strip }
    end

    IPTC_MULTI_FIELD_MAP.each do |key, element_name|
      if node = refs[:iptc].at_xpath(".//Iptc4xmpCore:#{element_name}")
        node.text.split(',').collect(&:strip).each_with_index do |value, index|
          contacts[index][key] = value
        end
      end
    end

    IPTC_CORE_FIELDS_AND_NAMES.each do |key, element_name|
      if node = refs[:iptc].at_xpath("//Iptc4xmpCore:#{element_name}")
        contacts.each { |contact| contact[key] = node.text.strip }
      end
    end
  end

  if !(@contacts = contacts.collect { |contact| Contact.new(contact) }).empty?
    @contacts.first.primary = true
  end
end

#lengthObject



25
26
27
# File 'lib/avm/creator.rb', line 25

def length
  contacts.length
end

#merge!(hash) ⇒ Object



21
22
23
# File 'lib/avm/creator.rb', line 21

def merge!(hash)
  @options.merge!(hash)
end

#primary_contactObject



103
104
105
# File 'lib/avm/creator.rb', line 103

def primary_contact
  @contacts.find(&:primary) || @contacts.sort.first
end

#to_aObject



33
34
35
# File 'lib/avm/creator.rb', line 33

def to_a
  contacts.sort.collect(&:to_h)
end