Module: EPUB::Parser::Metadata

Included in:
OCF, Publication
Defined in:
lib/epub/parser/metadata.rb

Instance Method Summary collapse

Instance Method Details

#build_model(elem, klass = :DCMES, attributes = %w[id lang dir])) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/epub/parser/metadata.rb', line 87

def build_model(elem, klass=:DCMES, attributes=%w[id lang dir])
  model = EPUB::Metadata.const_get(klass).new
  attributes.each do |attr|
    writer_name = (attr == "content") ? "meta_content=" : "#{attr.gsub('-', '_')}="
    namespace = (attr == "lang") ? "xml" : nil
    model.__send__ writer_name, elem.attribute_with_prefix(attr, namespace)
  end
  model.content = elem.content unless klass == :Link
  model.content.strip! if klass == :Identifier
  model
end

#build_unsupported_model(elem) ⇒ Object



99
100
101
# File 'lib/epub/parser/metadata.rb', line 99

def build_unsupported_model(elem)
  EPUB::Metadata::UnsupportedModel.new(elem)
end

#parse_metadata(elem, unique_identifier_id, default_namespace) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/epub/parser/metadata.rb', line 6

def (elem, unique_identifier_id, default_namespace)
   = EPUB::Publication::Package::Metadata.new
  id_map = {}

  default_namespace_uri = EPUB::NAMESPACES[default_namespace]
  elem.each_element do |child|
    elem_name = child.name

    model =
      case child.namespace_uri
      when EPUB::NAMESPACES['dc']
        case elem_name
        when 'identifier'
          identifier = build_model(child, :Identifier, ['id'])
          .identifiers << identifier
          identifier.scheme = child.attribute_with_prefix('scheme', 'opf')
          identifier
        when 'title'
          title = build_model(child, :Title)
          .titles << title
          title
        when 'language'
          language = build_model(child, :DCMES, ['id'])
          .languages << language
          language
        when 'contributor', 'coverage', 'creator', 'date', 'description', 'format', 'publisher', 'relation', 'source', 'subject', 'rights', 'type'
          attr = elem_name == 'rights' ? elem_name : elem_name + 's'
          dcmes = build_model(child)
          .__send__(attr) << dcmes
          dcmes
        else
          build_unsupported_model(child)
        end
      when default_namespace_uri
        case elem_name
        when 'meta'
          meta = build_model(child, :Meta, %w[property id scheme content name])
          .metas << meta
          meta
        when 'link'
          link = build_model(child, :Link, %w[id media-type])
          .links << link
          link.href = child.attribute_with_prefix('href')
          link.rel = Set.new(child.attribute_with_prefix('rel').split(/\s+/))
          link
        else
          build_unsupported_model(child)
        end
      else
        build_unsupported_model(child)
      end

    .children << model

    if model.kind_of?(EPUB::Metadata::Identifier) &&
       model.id == unique_identifier_id
      .unique_identifier = model
    end

    if model.respond_to?(:id) && model.id
      id_map[model.id] = {refinee: model}
    end

    refines = child.attribute_with_prefix('refines')
    if refines && refines.start_with?('#')
      id = refines[1..-1]
      id_map[id] ||= {}
      id_map[id][:refiners] ||= []
      id_map[id][:refiners] << model
    end
  end

  id_map.values.each do |hsh|
    next unless hsh[:refiners]
    next unless hsh[:refinee]
    hsh[:refiners].each {|meta| meta.refines = hsh[:refinee]}
  end

  
end