Class: Matterhorn::DublinCore

Inherits:
Object
  • Object
show all
Defined in:
lib/matterhorn/dublin_core.rb

Overview

<?xml version=“1.0”?> <dublincore xmlns=“www.opencastproject.org/xsd/1.0/dublincore/” xmlns:xsi=“www.w3.org/2001/XMLSchema-instance

xsi:schemaLocation="http://www.opencastproject.org http://www.opencastproject.org/schema.xsd" xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/" xmlns:oc="http://www.opencastproject.org/matterhorn/">
<dcterms:title xml:lang="en">
  Land and Vegetation: Key players on the Climate Scene
  </dcterms:title>
<dcterms:subject>
  climate, land, vegetation
  </dcterms:subject>
<dcterms:description xml:lang="en">
  Introduction lecture from the Institute for
  Atmospheric and Climate Science.
  </dcterms:description>
<dcterms:publisher>
  ETH Zurich, Switzerland
  </dcterms:publisher>
<dcterms:identifier>
  10.0000/5819
  </dcterms:identifier>
<dcterms:modified xsi:type="dcterms:W3CDTF">
  2007-12-05
  </dcterms:modified>
<dcterms:format xsi:type="dcterms:IMT">
  video/x-dv
  </dcterms:format>
<oc:promoted>
  true
</oc:promoted>

</dublincore>

Constant Summary collapse

NS =

————————————————————————– const definitions —

{
  'xmlns'              => "http://www.opencastproject.org/xsd/1.0/dublincore/",
  'xmlns:xsi'          => "http://www.w3.org/2001/XMLSchema-instance",
  'xsi:schemaLocation' => "http://www.opencastproject.org http://www.opencastproject.org/schema.xsd",
  'xmlns:dc'           => "http://purl.org/dc/elements/1.1/",
  'xmlns:dcterms'      => "http://purl.org/dc/terms/",
  'xmlns:oc'           => "http://www.opencastproject.org/matterhorn/"
}

Instance Method Summary collapse

Constructor Details

#initialize(xml = nil) ⇒ DublinCore

—————————————————————————– initialization —



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/matterhorn/dublin_core.rb', line 55

def initialize(xml = nil)
  if !xml.nil?
    @document = Nokogiri::XML(xml)
  else
    @document = Nokogiri::XML::Builder.new do |xml|
      xml.dublincore(NS) do
      end
    end
    .doc
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



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
123
124
125
126
127
128
129
130
131
132
# File 'lib/matterhorn/dublin_core.rb', line 97

def method_missing(method, *args, &block)
  # analyse mehtod
  splitted_method = method.to_s.split('_')
  if splitted_method.first == 'add'
    method_name = :add_value
    splitted_method.shift
  elsif splitted_method.first == 'list'
    method_name = :list_value
    splitted_method.shift
  elsif splitted_method.last.end_with?('=')
    method_name = :set_value
    splitted_method.last.chop!
  else
    method_name = :get_value
  end

  # namespace, key and value
  namespace = if !get_ns(splitted_method.first).nil?
                splitted_method.shift
              else
                'xmlns'
              end
  key       = splitted_method.join('_')
  value     = args[0]
  MatterhornWhymper.logger.debug { "#{self.class.name}#method_missing | " +
                                   "method: #{method_name.to_s}; namespace: #{namespace}; " +
                                   "key: #{key}; value: #{value.to_s}" }

  # call method
  params = case method_name
           when :get_value then [namespace, key]
           when :set_value then [namespace, key, args[0].to_s]
           when :add_value then [namespace, key, args[0].to_s]
           end
  send(method_name, *params)
end

Instance Method Details

#add_value(ns, key, value) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/matterhorn/dublin_core.rb', line 162

def add_value(ns, key, value)
  sibling = @document.xpath("/xmlns:dublincore/#{ns}:#{key}").last
  elem = Nokogiri::XML::Element.new(key, @document)
  elem.content = value.to_s.strip
  elem.namespace = get_ns(ns)    unless ns.nil?
  if !sibling.nil?
    sibling.after(elem)
  else
    @document.root << elem
  end
  elem.content
end

#documentObject

———————————————————————————– methodes —



70
71
72
# File 'lib/matterhorn/dublin_core.rb', line 70

def document
  @document
end

#each_dcterms_element(&block) ⇒ Object



75
76
77
# File 'lib/matterhorn/dublin_core.rb', line 75

def each_dcterms_element(&block)
  each_element('dcterms', &block)
end

#each_element(ns, &block) ⇒ Object



176
177
178
179
180
# File 'lib/matterhorn/dublin_core.rb', line 176

def each_element(ns, &block) 
  @document.xpath("/xmlns:dublincore/#{ns}:*").each do |elem|
    yield elem.name, elem.content.to_s.strip
  end
end

#get_ns(ns) ⇒ Object

———————————————————————————— helpers —



137
138
139
# File 'lib/matterhorn/dublin_core.rb', line 137

def get_ns(ns)
  @document.root.namespace_definitions.find { |n| n.prefix == ns }
end

#get_value(ns, key) ⇒ Object



142
143
144
145
146
# File 'lib/matterhorn/dublin_core.rb', line 142

def get_value(ns, key)
  elem = @document.xpath("/xmlns:dublincore/#{ns}:#{key}").first
  return nil    if elem.nil?
  elem.content.to_s.strip
end

#inspectObject



92
93
94
# File 'lib/matterhorn/dublin_core.rb', line 92

def inspect
  to_xml.to_s
end

#save(file) ⇒ Object



80
81
82
83
84
# File 'lib/matterhorn/dublin_core.rb', line 80

def save(file)
  File.open(file, 'w') do |file|
    file.write(@document.to_xml)
  end
end

#set_value(ns, key, value) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/matterhorn/dublin_core.rb', line 149

def set_value(ns, key, value)
  if !(elem = @document.at_xpath("/xmlns:dublincore/#{ns}:#{key}")).nil?
    elem.content = value.to_s.strip
  else
    elem = Nokogiri::XML::Element.new(key, @document)
    elem.content = value.to_s.strip
    elem.namespace = get_ns(ns)    unless ns.nil?
    @document.root << elem
  end
  elem.content
end

#to_xmlObject



87
88
89
# File 'lib/matterhorn/dublin_core.rb', line 87

def to_xml
  @document.to_xml
end