Class: RBook::Onix::Contributor

Inherits:
Object
  • Object
show all
Defined in:
lib/rbook/onix/contributor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/rbook/onix/contributor.rb', line 9

def name
  @name
end

#name_invertedObject

Returns the value of attribute name_inverted.



9
10
11
# File 'lib/rbook/onix/contributor.rb', line 9

def name_inverted
  @name_inverted
end

#roleObject

Returns the value of attribute role.



9
10
11
# File 'lib/rbook/onix/contributor.rb', line 9

def role
  @role
end

#sequence_numberObject

Returns the value of attribute sequence_number.



9
10
11
# File 'lib/rbook/onix/contributor.rb', line 9

def sequence_number
  @sequence_number
end

Class Method Details

.load_from_element(element) ⇒ Object

Attempts to create a contributor object using the supplied xml element WARNING: deprecated method

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
# File 'lib/rbook/onix/contributor.rb', line 32

def Contributor.load_from_element(element)
  raise ArgumentError, 'load_from_element expects a REXML element object' unless element.class == REXML::Element
  
  if REXML::XPath.first(element, '//Contributor').nil? 
    raise LoadError, 'supplied REXML document object does not appear contain a valid contributor fragment'
  end

  self.load_from_string(element.to_s)
end

.load_from_string(str) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rbook/onix/contributor.rb', line 11

def Contributor.load_from_string(str)
  doc = Hpricot::XML(str)
  contributor = Contributor.new

  tmp = doc.search('//Contributor/sequencenumber')
  contributor.sequence_number = tmp.inner_html unless tmp.inner_html.blank?

  tmp = doc.search('//Contributor/contributorrole')
  contributor.role = tmp.inner_html unless tmp.inner_html.blank?

  tmp = doc.search('//Contributor/personname')
  contributor.name = tmp.inner_html unless tmp.inner_html.blank?
  
  tmp = doc.search('//Contributor/personnameinverted')
  contributor.name_inverted = tmp.inner_html unless tmp.inner_html.blank?

  return contributor
end

Instance Method Details

#to_elementObject



58
59
60
# File 'lib/rbook/onix/contributor.rb', line 58

def to_element
  REXML::Document.new(to_s).root
end

#to_sObject

Return an XML string representing this contributor



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rbook/onix/contributor.rb', line 43

def to_s
  raise 'Contributor must have a sequence number to create an element' if @sequence_number.nil?
  raise 'Contributor must have a role to create an element' if @role.nil?
  raise '' if @name_inverted.nil? && @name.nil?

  builder = Builder::XmlMarkup.new(:indent => 2)

  builder.Contributor do |c|
    c.SequenceNumber  self.sequence_number
    c.ContributorRole self.role
    c.PersonName self.name unless self.name.nil?
    c.PersonNameInverted self.name_inverted unless self.name_inverted.nil?
  end
end