Class: Everypolitician::Popolo::Collection
- Inherits:
-
Object
- Object
- Everypolitician::Popolo::Collection
show all
- Includes:
- Enumerable
- Defined in:
- lib/everypolitician/popolo/collection.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(documents, popolo = nil) ⇒ Collection
Returns a new instance of Collection.
17
18
19
20
21
22
23
|
# File 'lib/everypolitician/popolo/collection.rb', line 17
def initialize(documents, popolo = nil)
@entity_class = {}
@documents = documents ? documents.map { |p| class_for_entity(p).new(p, popolo) } : []
@popolo = popolo
@indexes = {}
@of_collection = {}
end
|
Instance Attribute Details
#documents ⇒ Object
Returns the value of attribute documents.
8
9
10
|
# File 'lib/everypolitician/popolo/collection.rb', line 8
def documents
@documents
end
|
#popolo ⇒ Object
Returns the value of attribute popolo.
9
10
11
|
# File 'lib/everypolitician/popolo/collection.rb', line 9
def popolo
@popolo
end
|
Class Method Details
.entity_class(entity = nil) ⇒ Object
set the class that represents individual items in the collection
13
14
15
|
# File 'lib/everypolitician/popolo/collection.rb', line 13
def self.entity_class(entity = nil)
@entity_class ||= entity
end
|
Instance Method Details
#-(other) ⇒ Object
29
30
31
32
|
# File 'lib/everypolitician/popolo/collection.rb', line 29
def -(other)
other_ids = Set.new(other.documents.map(&:id))
documents.reject { |d| other_ids.include?(d.id) }
end
|
#each(&block) ⇒ Object
25
26
27
|
# File 'lib/everypolitician/popolo/collection.rb', line 25
def each(&block)
documents.each(&block)
end
|
#empty? ⇒ Boolean
42
43
44
|
# File 'lib/everypolitician/popolo/collection.rb', line 42
def empty?
count.zero?
end
|
#find_by(attributes = {}) ⇒ Object
34
35
36
|
# File 'lib/everypolitician/popolo/collection.rb', line 34
def find_by(attributes = {})
where(attributes).first
end
|
#of_collection(collection) ⇒ Object
46
47
48
|
# File 'lib/everypolitician/popolo/collection.rb', line 46
def of_collection(collection)
@of_collection[collection] ||= new_collection(select { |e| e.class == collection.entity_class }, collection)
end
|
#where(attributes = {}) ⇒ Object
38
39
40
|
# File 'lib/everypolitician/popolo/collection.rb', line 38
def where(attributes = {})
new_collection(attributes.map { |k, v| index_for(k.to_sym)[v].to_a }.reduce(:&))
end
|