Class: GEPUB::Meta

Inherits:
Object
  • Object
show all
Includes:
DSLUtil
Defined in:
lib/gepub/meta.rb

Overview

Holds one metadata with refine meta elements.

Direct Known Subclasses

DateMeta

Constant Summary collapse

REFINERS =
['title-type', 'identifier-type', 'display-seq', 'file-as', 'group-position', 'role'].each {
  |name|
  methodbase = name.sub('-','_')
  define_method(methodbase + '=') { |val| refine(name, val) }
  define_method('set_' + methodbase) { |val|
    warn "set_#{methodbase} is obsolete. use #{methodbase} instead."
    refine(name, val)
  }        
  define_method(methodbase, ->(value = UNASSIGNED) {
                  if unassigned?(value)
                    refiner(name)
                  else
                    refine(name,value)
                  end
                })
}

Constants included from DSLUtil

DSLUtil::UNASSIGNED

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, content, parent, attributes = {}, refiners = {}) ⇒ Meta

Returns a new instance of Meta


10
11
12
13
14
15
16
17
# File 'lib/gepub/meta.rb', line 10

def initialize(name, content, parent, attributes= {}, refiners = {})
  @parent = parent
  @name = name
  @content = content
  @attributes = attributes
  @refiners = refiners
  @parent.register_meta(self) unless @parent.nil?
end

Instance Attribute Details

#contentObject

Returns the value of attribute content


8
9
10
# File 'lib/gepub/meta.rb', line 8

def content
  @content
end

#nameObject (readonly)

Returns the value of attribute name


9
10
11
# File 'lib/gepub/meta.rb', line 9

def name
  @name
end

Instance Method Details

#[](x) ⇒ Object


19
20
21
# File 'lib/gepub/meta.rb', line 19

def [](x)
  @attributes[x]
end

#[]=(x, y) ⇒ Object


23
24
25
# File 'lib/gepub/meta.rb', line 23

def []=(x,y)
  @attributes[x] = y
end

#add_alternates(alternates = {}) ⇒ Object

add alternate script refiner.


95
96
97
98
99
100
101
# File 'lib/gepub/meta.rb', line 95

def add_alternates(alternates = {})
  alternates.each {
    |locale, content|
    add_refiner('alternate-script', content, { 'xml:lang' => locale })
  }
  self
end

#add_refiner(property, content, attributes = {}) ⇒ Object

add a refiner.


51
52
53
54
# File 'lib/gepub/meta.rb', line 51

def add_refiner(property, content, attributes = {})
  (@refiners[property] ||= []) <<  Meta.new('meta', content, @parent, { 'property' => property }.merge(attributes)) unless content.nil?
  self
end

#lang(lang = UNASSIGNED) ⇒ Object


86
87
88
89
90
91
92
# File 'lib/gepub/meta.rb', line 86

def lang(lang = UNASSIGNED)
  if unassigned?(lang)
    @attributes['xml:lang']
  else
    self.lang=(lang)
  end
end

#lang=(lang) ⇒ Object


82
83
84
# File 'lib/gepub/meta.rb', line 82

def lang=(lang)
  @attributes['xml:lang'] = lang
end

#list_alternatesObject


103
104
105
106
107
108
109
# File 'lib/gepub/meta.rb', line 103

def list_alternates
  list = refiner_list('alternate-script').map {
    |refiner|
    [ refiner['xml:lang'], refiner.content ]
  }
  Hash[*list.flatten]
end

#refine(property, content, attributes = {}) ⇒ Object

set a 'unique' refiner. all other refiners with same property will be removed.


57
58
59
60
61
62
63
# File 'lib/gepub/meta.rb', line 57

def refine(property, content, attributes = {})
  if !content.nil?
    refiner_clear(property)
    add_refiner(property, content, attributes)
  end
  self
end

#refiner(name) ⇒ Object


41
42
43
44
45
46
47
48
# File 'lib/gepub/meta.rb', line 41

def refiner(name)
  refiner = @refiners[name]
  if refiner.nil? || refiner.size == 0
    nil
  else
    refiner[0]
  end
end

#refiner_clear(name) ⇒ Object


31
32
33
34
35
36
37
38
39
# File 'lib/gepub/meta.rb', line 31

def refiner_clear(name)
  if !@refiners[name].nil?
    @refiners[name].each {
      |refiner|
      @parent.unregister_meta(refiner)
    }
  end
  @refiners[name]= []
end

#refiner_list(name) ⇒ Object


27
28
29
# File 'lib/gepub/meta.rb', line 27

def refiner_list(name)
  return @refiners[name].dup
end

#to_s(locale = nil) ⇒ Object


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/gepub/meta.rb', line 138

def to_s(locale=nil)
  localized = nil
  if !locale.nil?
    prefix = locale.sub(/^(.+?)-.*/, '\1')
    regex = Regexp.new("^((" + locale.split('-').join(')?-?(') + ")?)")
    candidates = @refiners['alternate-script'].select {
      |refiner|
      refiner['xml:lang'] =~ /^#{prefix}-?.*/
    }.sort_by {
      |x|
      x['xml:lang'] =~ regex; $1.size
    }.reverse
    localized = candidates[0].content if candidates.size > 0
  end
  (localized || self.content || super()).to_s
end

#to_xml(builder, id_pool, ns = nil, additional_attr = {}, opf_version = '3.0') ⇒ Object


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/gepub/meta.rb', line 111

def to_xml(builder, id_pool, ns = nil, additional_attr = {}, opf_version = '3.0')
  additional_attr ||= {}
  if @refiners.size > 0 && opf_version.to_f >= 3.0
    @attributes['id'] = id_pool.generate_key(:prefix => name) if @attributes['id'].nil?
  end

  # using __send__ to parametarize Namespace and content.
  target = ns.nil? || @name == 'meta' ? builder : builder[ns]
  attr = @attributes.reject{|_k,v| v.nil?}.merge(additional_attr)
  if @content.nil?
    target.__send__(@name, attr)
  else
    target.__send__(@name, attr, self.to_s)
  end

  if @refiners.size > 0 && opf_version.to_f >= 3.0
    additional_attr['refines'] = "##{@attributes['id']}"
    @refiners.each {
      |_k, ref_list|
      ref_list.each {
        |ref|
        ref.to_xml(builder, id_pool, nil, additional_attr)
      }
    }
  end
end