Class: GoogleSpreadsheets::Enhanced::Collection

Inherits:
ActiveResource::Collection
  • Object
show all
Defined in:
lib/google_spreadsheets/enhanced/collection.rb

Instance Method Summary collapse

Instance Method Details

#find(id) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/google_spreadsheets/enhanced/collection.rb', line 6

def find(id)
  if block_given?
    to_a.find{|*block_args| yield(*block_args) }
  else
    find_by!(id: id)
  end
end

#find_by(condition_hash) ⇒ Object



14
15
16
17
18
19
# File 'lib/google_spreadsheets/enhanced/collection.rb', line 14

def find_by(condition_hash)
  to_a.find do |element|
    # TODO: compare with consideration of type cast
    condition_hash.all?{|attr, value| element.send(attr) == value }
  end
end

#find_by!(condition_hash) ⇒ Object



21
22
23
24
# File 'lib/google_spreadsheets/enhanced/collection.rb', line 21

def find_by!(condition_hash)
  find_by(condition_hash) ||
    raise(ActiveResource::ResourceNotFound.new(OpenStruct.new(message: "Couldn't find #{self.class.name} with #{condition_hash}")))
end

#where(condition_hash) ⇒ Object



26
27
28
29
30
# File 'lib/google_spreadsheets/enhanced/collection.rb', line 26

def where(condition_hash)
  condition_hash.inject(self.to_a) do |array, (attr, value)|
    array.find_all{|element| element.send(attr) == value }
  end
end