Class: PocketAPI::Collection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, hashes = []) ⇒ Collection

Returns a new instance of Collection.



7
8
9
10
# File 'lib/pocket_api/collection.rb', line 7

def initialize(klass, hashes=[])
  @klass = klass
  @data = parse_hashes(hashes)
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/pocket_api/collection.rb', line 5

def data
  @data
end

#klassObject (readonly)

Returns the value of attribute klass.



4
5
6
# File 'lib/pocket_api/collection.rb', line 4

def klass
  @klass
end

Instance Method Details

#each(&block) ⇒ Object



12
13
14
# File 'lib/pocket_api/collection.rb', line 12

def each(&block)
  @data.each{ |o| block.call(o) }
end

#where(key_value) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/pocket_api/collection.rb', line 16

def where(key_value)
  key, value = key_value.first

  raise InvalidAttributeError, "Invalid attribute: #{key}" unless @klass.method_defined? key.to_sym

  dup_select { |o| o.send(key) == value }
end