Class: GEPUB::Meta
- Inherits:
-
Object
- Object
- GEPUB::Meta
- Defined in:
- lib/gepub/meta.rb
Overview
Holds one metadata with refine meta elements.
Direct Known Subclasses
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
- #[](x) ⇒ Object
- #[]=(x, y) ⇒ Object
-
#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.
- #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.
9 10 11 12 13 14 15 16 |
# File 'lib/gepub/meta.rb', line 9 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.
7 8 9 |
# File 'lib/gepub/meta.rb', line 7 def content @content end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/gepub/meta.rb', line 8 def name @name end |
Instance Method Details
#[](x) ⇒ Object
18 19 20 |
# File 'lib/gepub/meta.rb', line 18 def [](x) @attributes[x] end |
#[]=(x, y) ⇒ Object
22 23 24 |
# File 'lib/gepub/meta.rb', line 22 def []=(x,y) @attributes[x] = y end |
#add_alternates(alternates = {}) ⇒ Object
add alternate script refiner.
73 74 75 76 77 78 79 |
# File 'lib/gepub/meta.rb', line 73 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.
50 51 52 53 |
# File 'lib/gepub/meta.rb', line 50 def add_refiner(property, content, attributes = {}) (@refiners[property] ||= []) << refiner = Meta.new('meta', content, @parent, { 'property' => property }.merge(attributes)) unless content.nil? self end |
#list_alternates ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/gepub/meta.rb', line 81 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.
56 57 58 59 60 61 62 |
# File 'lib/gepub/meta.rb', line 56 def refine(property, content, attributes = {}) if !content.nil? refiner_clear(property) add_refiner(property, content, attributes) end self end |
#refiner(name) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/gepub/meta.rb', line 40 def refiner(name) refiner = @refiners[name] if refiner.nil? || refiner.size == 0 nil else refiner[0] end end |
#refiner_clear(name) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/gepub/meta.rb', line 30 def refiner_clear(name) if !@refiners[name].nil? @refiners[name].each { |refiner| @parent.(refiner) } end @refiners[name]= [] end |
#refiner_list(name) ⇒ Object
26 27 28 |
# File 'lib/gepub/meta.rb', line 26 def refiner_list(name) return @refiners[name].dup end |
#to_s(locale = nil) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/gepub/meta.rb', line 110 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
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/gepub/meta.rb', line 89 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 eval to parametarize Namespace and content. eval "builder#{ ns.nil? || @name == 'meta' ? '' : '[ns]'}.#{@name}(@attributes.reject{|k,v| v.nil?}.merge(additional_attr)#{@content.nil? ? '' : ', self.to_s'})" 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 |