Class: Ruber::World::DocumentList
- Inherits:
-
Object
- Object
- 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)
-
- (Boolean) ==(other)
Comparison operator.
-
- (Document, ...) [](arg)
Element access.
-
- (Array<Document>) document_array
protected
The internal array used to keep trace of the documents.
-
- (Document?) document_for_file(file)
The document associated with a given file.
-
- (Boolean) document_for_file?(file)
Whether or not the list contains a document associated with a given file.
-
- (Document?) document_for_url(url)
The document associated with a given URL.
-
- (Boolean) document_for_url?(url)
Whether or not the list contains a document associated with a given URL.
-
- (Document?) document_with_name(name)
The document with a given
document_name
. -
- (Boolean) document_with_name?(name)
Whether or not the list contains a document with a given document name.
-
- (Array<Document>) documents_with_file(which = :any)
A list of the documents having a file associated with them.
-
- (DocumentList, Enumerator) each(&blk)
Iterates on the documents.
-
- (Boolean) empty?
Whether or not the list is empty.
-
- (Boolean) eql?(other)
Comparison operator used by Hash.
-
- (Integer) hash
Override of
Object#hash
. -
- (DocumentList) initialize(docs = [])
constructor
A new instance of DocumentList.
-
- (Integer) size
The number of documents in the list.
Methods included from Enumerable
Constructor Details
- (DocumentList) initialize(docs = [])
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
- (Boolean) ==(other)
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 |
- (Document?) [](idx) - (Array<Document>?) [](range) - (Document?) [](url) - (Document?) [](file) - (Document?) [](name)
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 |
- (Array<Document>) document_array (protected)
The internal array used to keep trace of the documents
264 265 266 |
# File 'lib/ruber/world/document_list.rb', line 264 def document_array @documents end |
- (Document?) document_for_file(file)
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 |
- (Boolean) document_for_file?(file)
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?) document_for_url(url)
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 |
- (Boolean) document_for_url?(url)
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?) document_with_name(name)
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 |
- (Boolean) document_with_name?(name)
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 |
- (Array<Document>) documents_with_file(which = :any)
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 |
- (DocumentList) each {|doc| ... } - (Enumerator) each
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 |
- (Boolean) empty?
Whether or not the list is empty
76 77 78 |
# File 'lib/ruber/world/document_list.rb', line 76 def empty? @documents.empty? end |
- (Boolean) eql?(other)
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 |
- (Integer) hash
Override of Object#hash
255 256 257 |
# File 'lib/ruber/world/document_list.rb', line 255 def hash @documents.hash end |
- (Integer) size
The number of documents in the list
68 69 70 |
# File 'lib/ruber/world/document_list.rb', line 68 def size @documents.size end |