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_array, #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

- (MutableDocumentList) initialize(docs = [])

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

- (MutableDocumentList) add(*docs)

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

- (MutableDocumentList) clear

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

- (MutableDocumentList) clone

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

- (MutableDocumentList) delete_if {|doc| ... }

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

- (MutableDocumentList) dup

Override of Object#dup

Returns:



289
290
291
# File 'lib/ruber/world/document_list.rb', line 289

def dup
  self.class.new self
end

- (MutableDocumentList) merge!(other, remove_duplicates = true)

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

- (Document?) remove(doc)

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

- (MutableDocumentList) uniq!

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