Class: ReaPack::Index::Metadata
- Inherits:
-
Object
- Object
- ReaPack::Index::Metadata
- Defined in:
- lib/reapack/index/metadata.rb
Constant Summary collapse
- TAG =
'metadata'.freeze
- DESC =
'description'.freeze
Instance Method Summary collapse
- #about ⇒ Object
- #about=(content) ⇒ Object
-
#initialize(parent) ⇒ Metadata
constructor
A new instance of Metadata.
- #links(type) ⇒ Object
- #modified? ⇒ Boolean
- #push_link(type, name = nil, url) ⇒ Object
- #remove_link(type, search) ⇒ Object
- #replace_links(type) ⇒ Object
Constructor Details
Instance Method Details
#about ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/reapack/index/metadata.rb', line 89 def about cdata = nil if @root desc = @root.element_children.find {|node| node.name == DESC } cdata = desc.children.first if desc end cdata ? cdata.content : String.new end |
#about=(content) ⇒ Object
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 |
# File 'lib/reapack/index/metadata.rb', line 100 def about=(content) content = content.to_s if !content.empty? && !content.start_with?("{\\rtf") content = make_rtf content end return if content == self.about make_root desc = @root.element_children.find {|node| node.name == DESC } @dirty = true if content.empty? desc.remove auto_remove return end if desc desc.children.each {|n| n.remove } else desc = Nokogiri::XML::Node.new DESC, @root.document desc.parent = @root end cdata = Nokogiri::XML::CDATA.new desc, content cdata.parent = desc end |
#links(type) ⇒ Object
16 17 18 19 |
# File 'lib/reapack/index/metadata.rb', line 16 def links(type) Link.find_all(type, @root).map {|node| Link.from_node node } .select {|link| link.url.index('http') == 0 } end |
#modified? ⇒ Boolean
12 13 14 |
# File 'lib/reapack/index/metadata.rb', line 12 def modified? !!@dirty end |
#push_link(type, name = nil, url) ⇒ Object
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 |
# File 'lib/reapack/index/metadata.rb', line 21 def push_link(type, name = nil, url) Link.check_type type begin uri = Addressable::URI.parse url unless ['http', 'https'].include? uri.scheme raise Addressable::URI::InvalidURIError end rescue Addressable::URI::InvalidURIError raise Error, "invalid link '#{url}'" end make_root link = Link.new name || url, url node = Link.find type, link.name, @root node ||= Link.find type, link.url, @root if node link.instance_variable_set :@is_new, false link.instance_variable_set :@modified, link != Link.from_node(node) node.remove_attribute Link::URL else link.instance_variable_set :@is_new, true link.instance_variable_set :@modified, true node = Nokogiri::XML::Node.new Link::TAG, @root.document node.parent = @root node[Link::REL] = type end if name node[Link::URL] = url node.content = name else node.content = url end @dirty = true if link.modified? link end |
#remove_link(type, search) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/reapack/index/metadata.rb', line 66 def remove_link(type, search) node = Link.find type, search, @root raise Error, "no such #{type} link '#{search}'" unless node node.remove auto_remove @dirty = true end |
#replace_links(type) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/reapack/index/metadata.rb', line 77 def replace_links(type) was_dirty = @dirty old_links = hash_links Link.find_all(type, @root) .each {|node| node.remove } yield new_links = hash_links Link.find_all(type, @root) @dirty = old_links != new_links unless was_dirty end |