Class: GEPUB::Meta
Overview
Holds one metadata with refine meta elements.
Direct Known Subclasses
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
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#[](attribute) ⇒ Object
get
attribute
. -
#[]=(attribute, value) ⇒ Object
set
attribute
. -
#add_alternates(alternates = {}) ⇒ Object
add alternate script refiner.
-
#add_refiner(property, content, attributes = {}) ⇒ Object
add a refiner.
-
#initialize(name, content, parent, attributes = {}, refiners = {}) ⇒ Meta
constructor
A new instance of Meta.
- #lang(lang = UNASSIGNED) ⇒ Object
- #lang=(lang) ⇒ Object
- #list_alternates ⇒ Object
-
#refine(property, content, attributes = {}) ⇒ Object
set a ‘unique’ refiner.
- #refiner(name) ⇒ Object
- #refiner_clear(name) ⇒ Object
- #refiner_list(name) ⇒ Object
- #to_s(locale = nil) ⇒ Object
- #to_xml(builder, id_pool, ns = nil, additional_attr = {}, opf_version = '3.0') ⇒ Object
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.(self) unless @parent.nil? end |
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
8 9 10 |
# File 'lib/gepub/meta.rb', line 8 def content @content end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/gepub/meta.rb', line 9 def name @name end |
Instance Method Details
#[](attribute) ⇒ Object
get attribute
20 21 22 |
# File 'lib/gepub/meta.rb', line 20 def [](attribute) @attributes[attribute] end |
#[]=(attribute, value) ⇒ Object
set attribute
25 26 27 |
# File 'lib/gepub/meta.rb', line 25 def []=(attribute, value) @attributes[attribute] = value end |
#add_alternates(alternates = {}) ⇒ Object
add alternate script refiner.
97 98 99 100 101 102 103 |
# File 'lib/gepub/meta.rb', line 97 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.
53 54 55 56 |
# File 'lib/gepub/meta.rb', line 53 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
88 89 90 91 92 93 94 |
# File 'lib/gepub/meta.rb', line 88 def lang(lang = UNASSIGNED) if unassigned?(lang) @attributes['xml:lang'] else self.lang=(lang) end end |
#lang=(lang) ⇒ Object
84 85 86 |
# File 'lib/gepub/meta.rb', line 84 def lang=(lang) @attributes['xml:lang'] = lang end |
#list_alternates ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/gepub/meta.rb', line 105 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.
59 60 61 62 63 64 65 |
# File 'lib/gepub/meta.rb', line 59 def refine(property, content, attributes = {}) if !content.nil? refiner_clear(property) add_refiner(property, content, attributes) end self end |
#refiner(name) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/gepub/meta.rb', line 43 def refiner(name) refiner = @refiners[name] if refiner.nil? || refiner.size == 0 nil else refiner[0] end end |
#refiner_clear(name) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/gepub/meta.rb', line 33 def refiner_clear(name) if !@refiners[name].nil? @refiners[name].each { |refiner| @parent.(refiner) } end @refiners[name]= [] end |
#refiner_list(name) ⇒ Object
29 30 31 |
# File 'lib/gepub/meta.rb', line 29 def refiner_list(name) return @refiners[name].dup end |
#to_s(locale = nil) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/gepub/meta.rb', line 140 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
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/gepub/meta.rb', line 113 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 |