Class: Everypolitician::Popolo::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/everypolitician/popolo/collection.rb

Direct Known Subclasses

Areas, Events, Memberships, Organizations, People, Posts

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(documents, popolo = nil) ⇒ Collection

Returns a new instance of Collection.



9
10
11
12
# File 'lib/everypolitician/popolo/collection.rb', line 9

def initialize(documents, popolo = nil)
  @documents = documents ? documents.map { |p| klass.new(p, popolo) } : []
  @popolo = popolo
end

Instance Attribute Details

#documentsObject (readonly)

Returns the value of attribute documents.



6
7
8
# File 'lib/everypolitician/popolo/collection.rb', line 6

def documents
  @documents
end

#popoloObject (readonly)

Returns the value of attribute popolo.



7
8
9
# File 'lib/everypolitician/popolo/collection.rb', line 7

def popolo
  @popolo
end

Instance Method Details

#-(other) ⇒ Object



18
19
20
21
# File 'lib/everypolitician/popolo/collection.rb', line 18

def -(other)
  other_ids = Set.new(other.documents.map(&:id))
  documents.reject { |d| other_ids.include?(d.id) }
end

#each(&block) ⇒ Object



14
15
16
# File 'lib/everypolitician/popolo/collection.rb', line 14

def each(&block)
  documents.each(&block)
end

#find_by(attributes = {}) ⇒ Object



23
24
25
# File 'lib/everypolitician/popolo/collection.rb', line 23

def find_by(attributes = {})
  where(attributes).first
end

#where(attributes = {}) ⇒ Object



27
28
29
30
31
# File 'lib/everypolitician/popolo/collection.rb', line 27

def where(attributes = {})
  find_all do |object|
    attributes.all? { |k, v| object.send(k) == v }
  end
end