Class: RubyFFDB::DocumentCollection
- Inherits:
-
Object
- Object
- RubyFFDB::DocumentCollection
- Includes:
- Comparable, Enumerable
- Defined in:
- lib/rffdb/document_collection.rb
Instance Attribute Summary collapse
-
#type ⇒ Class
readonly
This is a collection of this Document subclass.
Instance Method Summary collapse
-
#+(other) ⇒ DocumentCollection
Return a collection after adding to the original Warning: this may cause duplicates or mixed type joins! For safety, use #merge.
-
#-(other) ⇒ DocumentCollection
Return a collection after subtracting from the original.
-
#==(other) ⇒ Boolean
Allow comparison of collection.
-
#[](index) ⇒ Document, DocumentCollection
Return the collection item at the specified index.
-
#each(&block) ⇒ Object
Iterates over the list of Document instances.
-
#empty? ⇒ Boolean
Does the collection contain anything?.
-
#first ⇒ Document
Return the first item in the collection.
-
#initialize(list, type = Document) ⇒ DocumentCollection
constructor
A new instance of DocumentCollection.
-
#last ⇒ Document
Return the last item in the collection.
-
#merge(other) ⇒ DocumentCollection
Merge two collections.
-
#size ⇒ Fixnum
Returns the number of Document instances in the collection.
-
#sort(&block) ⇒ DocumentCollection
Allow complex sorting like an Array.
-
#where(attribute, value, comparison_method = '==') ⇒ DocumentCollection
Horribly inefficient way to allow querying Documents by their attributes.
Constructor Details
#initialize(list, type = Document) ⇒ DocumentCollection
Returns a new instance of DocumentCollection.
11 12 13 14 |
# File 'lib/rffdb/document_collection.rb', line 11 def initialize(list, type = Document) @list = list.to_a @type = type end |
Instance Attribute Details
#type ⇒ Class (readonly)
Returns this is a collection of this RubyFFDB::Document subclass.
7 8 9 |
# File 'lib/rffdb/document_collection.rb', line 7 def type @type end |
Instance Method Details
#+(other) ⇒ DocumentCollection
Return a collection after adding to the original
Warning: this may cause duplicates or mixed type joins! For safety,
use #merge
69 70 71 72 73 74 75 76 77 |
# File 'lib/rffdb/document_collection.rb', line 69 def +(other) if other.respond_to?(:to_a) self.class.new(@list + other.to_a, @type) elsif other.is_a?(@type) self.class.new(@list + [other], @type) else fail Exceptions::InvalidInput end end |
#-(other) ⇒ DocumentCollection
Return a collection after subtracting from the original
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rffdb/document_collection.rb', line 51 def -(other) new_list = @list.dup if other.respond_to?(:to_a) other.to_a.each do |item| new_list.delete_if { |document| document.id == item.id } end elsif other.is_a?(@type) new_list.delete_if { |document| document.id == other.id } else fail Exceptions::InvalidInput end self.class.new(new_list, @type) end |
#==(other) ⇒ Boolean
Allow comparison of collection
100 101 102 103 104 105 106 |
# File 'lib/rffdb/document_collection.rb', line 100 def ==(other) if other.is_a? self.class collect(&:id).sort == other.collect(&:id).sort else false end end |
#[](index) ⇒ Document, DocumentCollection
Return the collection item at the specified index
41 42 43 44 45 46 47 |
# File 'lib/rffdb/document_collection.rb', line 41 def [](index) if index.is_a?(Range) self.class.new(@list[index], @type) else @list[index] end end |
#each(&block) ⇒ Object
Iterates over the list of Document instances
17 18 19 |
# File 'lib/rffdb/document_collection.rb', line 17 def each(&block) @list.each(&block) end |
#empty? ⇒ Boolean
Does the collection contain anything?
110 111 112 |
# File 'lib/rffdb/document_collection.rb', line 110 def empty? @list.empty? end |
#first ⇒ Document
Return the first item in the collection
29 30 31 |
# File 'lib/rffdb/document_collection.rb', line 29 def first @list.first end |
#last ⇒ Document
Return the last item in the collection
35 36 37 |
# File 'lib/rffdb/document_collection.rb', line 35 def last @list.last end |
#merge(other) ⇒ DocumentCollection
Merge two collections
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rffdb/document_collection.rb', line 81 def merge(other) if other.is_a?(self.class) && other.type == @type new_list = [] new_keys = collect(&:id) new_keys += other.collect(&:id) new_keys.sort.uniq.each do |doc_id| new_list << self.class.get(doc_id) end self.class.new(new_list, @type) else fail Exceptions::InvalidInput end end |
#size ⇒ Fixnum
Returns the number of Document instances in the collection
23 24 25 |
# File 'lib/rffdb/document_collection.rb', line 23 def size @list.size end |
#sort(&block) ⇒ DocumentCollection
Allow complex sorting like an Array
116 117 118 |
# File 'lib/rffdb/document_collection.rb', line 116 def sort(&block) self.class.new(super(&block), @type) end |
#where(attribute, value, comparison_method = '==') ⇒ DocumentCollection
Horribly inefficient way to allow querying Documents by their attributes. This method can be chained for multiple / more specific queries.
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/rffdb/document_collection.rb', line 129 def where(attribute, value, comparison_method = '==') valid_comparison_methods = [:'==', :'>', :'>=', :'<', :'<=', :match] unless valid_comparison_methods.include?(comparison_method.to_sym) fail Exceptions::InvalidWhereQuery end self.class.new( @list.collect do |item| item if item.send(attribute).send(comparison_method.to_sym, value) end.compact, @type ) end |