Class: Ruber::World::MutableDocumentList
- Inherits:
-
DocumentList
- Object
- DocumentList
- Ruber::World::MutableDocumentList
- Defined in:
- lib/ruber/world/document_list.rb
Overview
A DocumentList which allows to change the contents of the list.
Instance Method Summary collapse
-
#add(*docs) ⇒ MutableDocumentList
Adds documents to the list.
-
#clear ⇒ MutableDocumentList
Removes all the elements from the list.
-
#clone ⇒ MutableDocumentList
Override of @Object#clone@.
-
#delete_if {|doc| ... } ⇒ MutableDocumentList
Removes from the list all documents for which the block returns true.
-
#dup ⇒ MutableDocumentList
Override of @Object#dup@.
-
#initialize(docs = []) ⇒ MutableDocumentList
constructor
A new instance of MutableDocumentList.
-
#merge!(other, remove_duplicates = true) ⇒ MutableDocumentList
Adds the contents of another array or MutableDocumentList to the list.
-
#remove(doc) ⇒ Document?
Removes a document from the list.
-
#uniq! ⇒ MutableDocumentList
Ensures that the list doesn’t contain duplicates.
Methods inherited from DocumentList
#==, #[], #document_for_file, #document_for_file?, #document_for_url, #document_for_url?, #document_with_name, #document_with_name?, #documents_with_file, #each, #empty?, #eql?, #hash, #size
Methods included from Enumerable
Constructor Details
#initialize(docs = []) ⇒ MutableDocumentList
Returns a new instance of MutableDocumentList.
280 281 282 283 |
# File 'lib/ruber/world/document_list.rb', line 280 def initialize docs = [] docs = docs.document_array if docs.is_a? DocumentList @documents = docs.dup end |
Instance Method Details
#add(*docs) ⇒ MutableDocumentList
this method doesn’t check for duplicate documents. While having multiple copies of the same document shouldn’t cause troubles, it’s better to avoid them. To do so, either check beforehand that docs contains no duplicates and no document already in the list, or use #uniq! afterwards
Adds documents to the list
318 319 320 321 322 |
# File 'lib/ruber/world/document_list.rb', line 318 def add *docs docs.flatten! @documents.insert -1, *docs self end |
#clear ⇒ MutableDocumentList
Removes all the elements from the list
362 363 364 365 |
# File 'lib/ruber/world/document_list.rb', line 362 def clear @documents.clear self end |
#clone ⇒ MutableDocumentList
Override of @Object#clone@
297 298 299 300 301 302 303 304 |
# File 'lib/ruber/world/document_list.rb', line 297 def clone res = self.class.new self if frozen? res.freeze res.document_array.freeze end res end |
#delete_if {|doc| ... } ⇒ MutableDocumentList
Removes from the list all documents for which the block returns true
375 376 377 378 |
# File 'lib/ruber/world/document_list.rb', line 375 def delete_if &blk @documents.delete_if &blk self end |
#dup ⇒ MutableDocumentList
Override of @Object#dup@
289 290 291 |
# File 'lib/ruber/world/document_list.rb', line 289 def dup self.class.new self end |
#merge!(other, remove_duplicates = true) ⇒ MutableDocumentList
Adds the contents of another array or Ruber::World::MutableDocumentList to the list
The documents from the other list will be added at the end of this list.
336 337 338 339 340 341 342 343 |
# File 'lib/ruber/world/document_list.rb', line 336 def merge! other, remove_duplicates = true if other.is_a? DocumentList @documents.concat other.document_array else @documents.concat other end uniq! if remove_duplicates self end |
#remove(doc) ⇒ Document?
Removes a document from the list
If the given document isn’t in the list, nothing is done
353 354 355 |
# File 'lib/ruber/world/document_list.rb', line 353 def remove doc @documents.delete doc end |
#uniq! ⇒ MutableDocumentList
Ensures that the list doesn’t contain duplicates
After calling this method, the list won’t contain the same document in multiple places
387 388 389 390 |
# File 'lib/ruber/world/document_list.rb', line 387 def uniq! @documents.uniq! self end |