Class: Ruber::World::MutableDocumentList

Inherits:
DocumentList show all
Defined in:
lib/ruber/world/document_list.rb

Overview

A DocumentList which allows to change the contents of the list.

Instance Method Summary collapse

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

#find!

Constructor Details

#initialize(docs = []) ⇒ MutableDocumentList

Returns a new instance of MutableDocumentList.

Parameters:

  • docs (Array<Document>, DocumentList) (defaults to: [])

    the documents to insert in the list when created. Further changes to docs won’t change the new instance and vice versa



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

Note:

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

Parameters:

Returns:



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

#clearMutableDocumentList

Removes all the elements from the list

Returns:



362
363
364
365
# File 'lib/ruber/world/document_list.rb', line 362

def clear
  @documents.clear
  self
end

#cloneMutableDocumentList

Override of @Object#clone@

Returns:



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

Yield Parameters:

  • doc (Document)

    the documents in the list

Yield Returns:

  • (Boolean)

    true for documents which should be removed from the list and false otherwise

Returns:



375
376
377
378
# File 'lib/ruber/world/document_list.rb', line 375

def delete_if &blk
  @documents.delete_if &blk
  self
end

#dupMutableDocumentList

Override of @Object#dup@

Returns:



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.

Parameters:

  • other (Array<Document>, DocumentList)

    the list whose contents should be added to this list contents

  • remove_duplicates (Boolean) (defaults to: true)

    if true, after adding the contents of other to the list, #uniq! will be called to ensure there are no duplicates. If false, #uniq! won’t be called

Returns:



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

Parameters:

  • doc (Document)

    the document to remove

Returns:

  • (Document, nil)

    the removed document or nil if no document was removed



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

Returns:



387
388
389
390
# File 'lib/ruber/world/document_list.rb', line 387

def uniq!
  @documents.uniq!
  self
end