Class: RubyFFDB::DocumentCollection

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/rffdb/document_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list, type = Document) ⇒ DocumentCollection

Returns a new instance of DocumentCollection.

Parameters:

  • list (#to_a)

    the list of Documents to reference

  • type (Class) (defaults to: Document)

    the type of Document this collection references



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

#typeClass (readonly)

Returns this is a collection of this RubyFFDB::Document subclass.

Returns:



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

Returns:



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

Returns:



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

Returns:

  • (Boolean)

    do the collections contain the same document ids?



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

Returns:



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?

Returns:

  • (Boolean)


110
111
112
# File 'lib/rffdb/document_collection.rb', line 110

def empty?
  @list.empty?
end

#firstDocument

Return the first item in the collection

Returns:

  • (Document)

    the first item in the collection



29
30
31
# File 'lib/rffdb/document_collection.rb', line 29

def first
  @list.first
end

#lastDocument

Return the last item in the collection

Returns:

  • (Document)

    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

Returns:



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

#sizeFixnum

Returns the number of Document instances in the collection

Returns:

  • (Fixnum)


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

Returns:



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.

Parameters:

  • attribute (Symbol)

    the attribute to query

  • value (Object)

    the value to compare against

  • comparison_method (String, Symbol) (defaults to: '==')

    the method to use for comparison

    • allowed options are “‘==’, ‘>’, ‘>=’, ‘<’, ‘<=’, and ‘match’”

Returns:

Raises:



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