Class: OpenURL::ScholarlyCommon

Inherits:
ContextObjectEntity show all
Defined in:
lib/openurl/metadata_formats/scholarly_common.rb

Direct Known Subclasses

Book, Dissertation, Journal

Instance Attribute Summary collapse

Attributes inherited from ContextObjectEntity

#abbr, #format, #identifiers, #label, #metadata, #private_data, #reference

Instance Method Summary collapse

Methods inherited from ContextObjectEntity

#add_identifier, #delete_identifier, #empty?, #get_metadata, #identifier, #kev, new_from_format, normalize_id, #set_format, #set_private_data, #set_reference, #to_hash, #xml, #xml_for_ref_entity

Constructor Details

#initializeScholarlyCommon

Returns a new instance of ScholarlyCommon.



15
16
17
18
19
20
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 15

def initialize
  super()
  @authors = [OpenURL::Author.new]
  @author_keys = ['aulast','aufirst','auinit','auinit1','auinitm','ausuffix',
    'au', 'aucorp']       
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(metadata, value = nil) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 22

def method_missing(, value=nil)
  meta = .to_s.sub(/=$/,'')
  raise ArgumentError, "#{meta.to_s} is not a valid #{self.class} metadata field." unless (@author_keys+@metadata_keys).index(meta)
  if .to_s.match(/=$/)
    self.(meta, value)
    if @author_keys.index(meta)
      @authors[0].instance_variable_set("@#{meta}", value)
    end
  else
    return self.[meta]
  end      
end

Instance Attribute Details

#authorsObject (readonly)

Returns the value of attribute authors.



14
15
16
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 14

def authors
  @authors
end

Instance Method Details

#add_author(author) ⇒ Object

Raises:

  • (ArgumentError)


58
59
60
61
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 58

def add_author(author)
  raise ArgumentError, "Argument must be an OpenURL::Author!" unless author.is_a?(OpenURL::Author)
  @authors << author
end

#genreObject



54
55
56
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 54

def genre
  return self.["genre"]
end

#genre=(genre) ⇒ Object

Raises:

  • (ArgumentError)


49
50
51
52
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 49

def genre=(genre)
  raise ArgumentError, "#{genre} is not a valid #{self.class} genre." unless @valid_genres.index(genre)
  self.('genre', genre)      
end

#import_xml_metadata(node) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 88

def (node)         
  mbv = REXML::XPath.first(node, "./ctx:metadata-by-val/ctx:metadata/fmt:#{@format}", {"ctx"=>"info:ofi/fmt:xml:xsd:ctx", "fmt"=>@xml_ns})					              
  if mbv
    mbv.to_a.each do |m|
      self.(m.name(), m.get_text.value) if m.has_text?                                  
      if m.has_elements?
        m.to_a.each do | md |
          self.(md.name(), md.get_text.value) if md.has_text?                              
        end
      end
    end
    auth_num = 0
    REXML::XPath.each(mbv, "fmt:authors/fmt:author | fmt:authors/fmt:au | fmt:authors/fmt:aucorp", {"fmt"=>@xml_ns}) do | author |                    
      empty_node = true
      if author.name == "author"            
        author.elements.each do | auth_elem |            
          next unless @author_keys.index(auth_elem.name) and auth_elem.has_text?
          empty_node = false
          @authors << OpenURL::Author.new unless @authors[auth_num]
          @authors[auth_num].instance_variable_set("@#{auth_elem.name}".to_sym, auth_elem.get_text.value)
          self.(auth_elem.name, auth_elem.get_text.value) if auth_num == 0
        end
      elsif author.name.match(/^au$|^aucorp$/)
        next unless author.has_text? 
        empty_node = false
        @authors << OpenURL::Author.new unless @authors[auth_num]
        # This next line is causing an exception, replaced it with following line modeling from above clause. Don't entirely understand it. 
        # @authors[auth_num][author.name] = author.get_text.value
        @authors[auth_num].instance_variable_set("@#{author.name}".to_sym, author.get_text.value)
        self.(author.name, author.get_text.value) if auth_num == 0            
      end
      auth_num += 1 unless empty_node
    end        
  end					
end

#remove_author(author) ⇒ Object

Raises:

  • (ArgumentError)


63
64
65
66
67
68
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 63

def remove_author(author)
  idx = author
  idx = @authors.index(author)
  raise ArgumentError unless idx
  @authors.delete_at(idx)      
end

#respond_to?(message, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 35

def respond_to?(message, include_all=false)
  # make it match method_messing
  key = message.to_s.sub(/=$/,'')
  super || (@author_keys+@metadata_keys).index(key)
end

#serialize_metadata(elem, label) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 70

def (elem, label)
  meta = {}
   = elem.add_element("ctx:metadata")
  meta["format_container"] = .add_element("#{label}:#{@format}")
  meta["format_container"].add_namespace(label, @xml_ns)
  meta["format_container"].add_attribute("xsi:schemaLocation", "#{@xml_ns} http://www.openurl.info/registry/docs/info:ofi/fmt:xml:xsd:#{@format}")          
  @metadata.each do |k,v|
    next if ['au', 'aucorp', 'auinit', 'auinitm', 'aulast',
      'aufirst', 'auinit1', 'ausuffix'].index(k)
    meta[k] = meta["format_container"].add_element("#{label}:#{k}")
    meta[k].text = v
  end            
  meta["author_container"] = meta["format_container"].add_element("#{label}:authors")
  @authors.each do | author |
    author.xml(meta["author_container"])
  end      
end

#set_metadata(key, val) ⇒ Object



42
43
44
45
46
47
# File 'lib/openurl/metadata_formats/scholarly_common.rb', line 42

def (key, val)
  @metadata[key] = val.to_s
  if @author_keys.index(key)
    @authors[0].instance_variable_set("@#{key}", val)
  end      
end