Class: ChillDB::List

Inherits:
Array
  • Object
show all
Defined in:
lib/chill.rb

Overview

A special sort of Array designed for storing lists of documents, and particularly the results of ChillDB::Design#query. It works sort of like a hash, in supporting lookups by key, and sort of like an array, in its sorted nature. It keeps the association of keys, documents, values, id’s, and also provides a simple way to bulk commit all the documents in the list or delete all of them form the server in one efficient request.

The recomended way to create a list from a regular array is ChillDB.list() as some of List’s functionality depends on it having a link to the database.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#databaseObject

Returns the value of attribute database.



528
529
530
# File 'lib/chill.rb', line 528

def database
  @database
end

#offsetObject

Returns the value of attribute offset.



528
529
530
# File 'lib/chill.rb', line 528

def offset
  @offset
end

#total_rowsObject

Returns the value of attribute total_rows.



528
529
530
# File 'lib/chill.rb', line 528

def total_rows
  @total_rows
end

Class Method Details

.from_array(array) ⇒ Object

to make a list from a simple array (not a couchdb response…)



545
546
547
548
549
550
# File 'lib/chill.rb', line 545

def self.from_array array # :nodoc:
  new_list = self.new
  new_list.replace(array.map { |item|
    { 'id'=> item['_id'] || item[:_id], 'key'=> item['_id'] || item[:_id], 'value'=> item, 'doc'=> item }
  })
end

.load(list, extras = {}) ⇒ Object

creates a new List from a couchdb response



531
532
533
534
535
536
537
538
539
540
541
542
# File 'lib/chill.rb', line 531

def self.load list, extras = {} # :nodoc:
  new_list = self.new
  
  raise "#{list['error']} - #{list['reason']}" if list['error']
  [extras, list].each do |properties|
    properties.each do |key, value|
      new_list.send("#{key}=", value)
    end
  end
  
  return new_list
end

Instance Method Details

#[](key) ⇒ Object

Lookup a document, by a key in the list. If there are multiple entries in the view with this key, the first document is returned.

Note this will return nil for lists returned by ChillDB::Design#query unless the query was done with the include_docs option set to true.



635
636
637
638
# File 'lib/chill.rb', line 635

def [] key
  return key(key)['doc'] unless key.respond_to? :to_int
  super
end

#commit!Object Also known as: commit_all!

Commit every document in this list to the server in a single quick request. Every document which can be committed will be, and if any fail a ChillDB::BulkUpdateErrors will be raised.

Returns self.



660
661
662
# File 'lib/chill.rb', line 660

def commit!
  commit_documents! convert
end

#delete!Object Also known as: delete_all!

Delete every document in this list from the server. If this list was returned by ChillDB::Design#query, your view’s values will need to be a Hash containing rev or _rev - indicating the document’s revision. You can also query your view with { include_docs: true } specified as an option. Deletion cannot happen without a revision identifier. All documents which can be deleted, will be. If there are any errors, they’ll be raised as a ChillDB::BulkUpdateErrors.

Returns self.



674
675
676
677
678
679
680
681
682
683
# File 'lib/chill.rb', line 674

def delete!
  # check all the entries have a _rev - if they don't they cannot be deleted!
  each do |item|
    rev = item['value']['_rev'] || item['value']['rev'] || item['doc']['_rev']
    raise "Some (all?) items in list do not contain _rev properties in their values" unless rev
  end
  
  # mark all documents for deletion and commit the order!
  commit_documents! convert.map { |item| item.delete }
end

#docsObject

Returns an Array of all the documents in this list.

Note that querying a view with ChillDB::Design#query does not include documents by default. Calling ChillDB::Design#query with the include_docs option set to true causes the database to lookup all of the documents.



595
596
597
# File 'lib/chill.rb', line 595

def docs
  self.map { |i| i['doc']  }
end

#each_pair(&proc) ⇒ Object

Iterates each key/value pair using a supplied proc.

Example:

list.each_pair do |key, value|
  puts "#{key}: #{value.inspect}"
end

:yeild: key, value



608
609
610
# File 'lib/chill.rb', line 608

def each_pair &proc
  self.each { |item| proc.call(item['key'], item['value']) }
end

#id(value) ⇒ Object

fetch a document by it’s id from this list. Useful only for queries with <pp>{ include_docs: true }</pp> set



614
615
616
# File 'lib/chill.rb', line 614

def id value
  self.find { |i| i['id'] == value }['doc']
end

#idsObject

Returns an Array of all the document _id strings in the list



576
577
578
# File 'lib/chill.rb', line 576

def ids
  self.map { |i| i['id'] }
end

#key(value) ⇒ Object

Lookup a key from the list. Returns a ChillDB::IndifferentHash containing:

{
  'key'   => key from database
  'value' => emitted value in view, if any
  'doc'   => document, if view queried with { include_docs: true }
  'id'    => id of document which emitted this result
}


626
627
628
# File 'lib/chill.rb', line 626

def key value
  ChillDB::IndifferentHash.new.replace(self.find { |i| i['key'] == value })
end

#keysObject

Returns an Array of keys in the list



581
582
583
# File 'lib/chill.rb', line 581

def keys
  self.map { |i| i['key'] }
end

#rowsObject

Grab all the rows - actually just self.



571
572
573
# File 'lib/chill.rb', line 571

def rows
  self
end

#rows=(arr) ⇒ Object

store rows nicely in mah belleh



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'lib/chill.rb', line 553

def rows=(arr) # :nodoc:
  self.replace(arr.map { |item|
    if item['value'].is_a? Hash
      if item['value'].respond_to?(:[]) && item['value']['_id']
        item['value']['_id'] ||= item['id']
        item['value'] = ChillDB::Document.new(@database, item['value'])
      else
        item['value'] = ChillDB::IndifferentHash.new.replace(item['value'])
      end
    end
    
    item['doc'] = ChillDB::Document.new(@database, item['doc']) if item['doc']
    
    item
  })
end

#to_h(value = :value) ⇒ Object

make a regular ruby hash version

By default returns a hash containing { key: value } pairs from the list. Passing :doc as the optional argument instead creates a hash of { key: doc } pairs.



645
646
647
648
649
650
651
652
653
# File 'lib/chill.rb', line 645

def to_h value = :value
  hash = ChillDB::IndifferentHash.new
  
  each do |item|
    hash[item['key']] = item[value.to_s]
  end
  
  return hash
end

#valuesObject

Returns an Array of all the emitted values in the list



586
587
588
# File 'lib/chill.rb', line 586

def values
  self.map { |i| i['value'] }
end