Class: Ruber::World::DocumentList
- Includes:
- Enumerable
- Defined in:
- lib/ruber/world/document_list.rb
Overview
A list of documents
It’s an immutable @Enumerable@ class with some convenience methods for dealing with documents.
The documents in the list are set in the constructor and can’t be changed later.
Direct Known Subclasses
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Comparison operator.
-
#[](arg) ⇒ Document, ...
Element access.
-
#document_for_file(file) ⇒ Document?
The document associated with a given file.
-
#document_for_file?(file) ⇒ Boolean
Whether or not the list contains a document associated with a given file.
-
#document_for_url(url) ⇒ Document?
The document associated with a given URL.
-
#document_for_url?(url) ⇒ Boolean
Whether or not the list contains a document associated with a given URL.
-
#document_with_name(name) ⇒ Document?
The document with a given @document_name@.
-
#document_with_name?(name) ⇒ Boolean
Whether or not the list contains a document with a given document name.
-
#documents_with_file(which = :any) ⇒ Array<Document>
A list of the documents having a file associated with them.
-
#each(&blk) ⇒ DocumentList, Enumerator
Iterates on the documents.
-
#empty? ⇒ Boolean
Whether or not the list is empty.
-
#eql?(other) ⇒ Boolean
Comparison operator used by Hash.
-
#hash ⇒ Integer
Override of @Object#hash@.
-
#initialize(docs = []) ⇒ DocumentList
constructor
A new instance of DocumentList.
-
#size ⇒ Integer
The number of documents in the list.
Methods included from Enumerable
Constructor Details
#initialize(docs = []) ⇒ DocumentList
Returns a new instance of DocumentList.
42 43 44 |
# File 'lib/ruber/world/document_list.rb', line 42 def initialize docs = [] @documents = docs.is_a?(DocumentList) ? docs.document_array : docs.dup end |
Instance Method Details
#==(other) ⇒ Boolean
Comparison operator
227 228 229 230 231 232 233 234 |
# File 'lib/ruber/world/document_list.rb', line 227 def == other case other when DocumentList @documents == other.instance_variable_get(:@documents) when Array then @documents == other else false end end |
#[](idx) ⇒ Document? #[](range) ⇒ Array<Document>? #[](url) ⇒ Document? #[](file) ⇒ Document? #[](name) ⇒ Document?
Element access
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/ruber/world/document_list.rb', line 115 def [] arg case arg when Integer, Range then @documents[arg] when KDE::Url then @documents.find{|doc| doc.url == arg} when String if arg.start_with? '/' @documents.find do |doc| doc.url.local_file? and doc.path == arg end else @documents.find{|doc| doc.document_name == arg} end end end |
#document_for_file(file) ⇒ Document?
The document associated with a given file
138 139 140 141 |
# File 'lib/ruber/world/document_list.rb', line 138 def document_for_file file raise ArgumentError, "#{file} is not an absolute path" unless file.start_with?('/') @documents.find{|doc| doc.url.local_file? && doc.path == file} end |
#document_for_file?(file) ⇒ Boolean
Whether or not the list contains a document associated with a given file
150 151 152 |
# File 'lib/ruber/world/document_list.rb', line 150 def document_for_file? file document_for_file(file).to_bool end |
#document_for_url(url) ⇒ Document?
The document associated with a given URL
162 163 164 |
# File 'lib/ruber/world/document_list.rb', line 162 def document_for_url url @documents.find{|doc| doc.url == url} end |
#document_for_url?(url) ⇒ Boolean
Whether or not the list contains a document associated with a given URL
172 173 174 |
# File 'lib/ruber/world/document_list.rb', line 172 def document_for_url? url document_for_url(url).to_bool end |
#document_with_name(name) ⇒ Document?
The document with a given @document_name@
183 184 185 |
# File 'lib/ruber/world/document_list.rb', line 183 def document_with_name name @documents.find{|doc| doc.document_name == name} end |
#document_with_name?(name) ⇒ Boolean
Whether or not the list contains a document with a given document name
193 194 195 |
# File 'lib/ruber/world/document_list.rb', line 193 def document_with_name? name document_with_name(name).to_bool end |
#documents_with_file(which = :any) ⇒ Array<Document>
A list of the documents having a file associated with them
According to the which argument, this method can return all the documents in the list which have a file associated with them, only those which have a local file associated with them or only those which have a remote file associated with them.
212 213 214 215 216 217 218 |
# File 'lib/ruber/world/document_list.rb', line 212 def documents_with_file which = :any case which when :local then @documents.select{|doc| doc.url.local_file?} when :remote then @documents.select{|doc| doc.url.remote_file?} when :any then @documents.select{|doc| doc.has_file?} end end |
#each {|doc| ... } ⇒ DocumentList #each ⇒ Enumerator
Iterates on the documents
57 58 59 60 61 62 63 |
# File 'lib/ruber/world/document_list.rb', line 57 def each &blk if block_given? @documents.each &blk self else self.to_enum end end |
#empty? ⇒ Boolean
Whether or not the list is empty
76 77 78 |
# File 'lib/ruber/world/document_list.rb', line 76 def empty? @documents.empty? end |
#eql?(other) ⇒ Boolean
Comparison operator used by Hash
243 244 245 246 247 248 |
# File 'lib/ruber/world/document_list.rb', line 243 def eql? other if other.is_a? DocumentList @documents == other.instance_variable_get(:@documents) else false end end |
#hash ⇒ Integer
Override of @Object#hash@
255 256 257 |
# File 'lib/ruber/world/document_list.rb', line 255 def hash @documents.hash end |
#size ⇒ Integer
Returns the number of documents in the list.
68 69 70 |
# File 'lib/ruber/world/document_list.rb', line 68 def size @documents.size end |