Class: Usergrid::Collection

Inherits:
Entity show all
Includes:
Enumerable
Defined in:
lib/usergrid/core/collection.rb

Constant Summary

Constants inherited from Resource

Resource::DEFAULT_API_URL, Resource::RESERVED, Resource::TYPE_HEADERS

Instance Attribute Summary collapse

Attributes inherited from Resource

#api_url, #current_user, #response

Instance Method Summary collapse

Methods inherited from Entity

#data, #data?, #resource, #to_json, #to_s

Methods inherited from Resource

#api_resource, #application, #auth_token, #auth_token=, #entity, #get, #logged_in?, #login, #logout, #management, #post, #put

Constructor Details

#initialize(url, api_url, options = {}, response = nil) ⇒ Collection



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

def initialize(url, api_url, options={}, response=nil)
  super url, api_url, options, response
  @iterator_follows_cursor = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Usergrid::Entity

Instance Attribute Details

#iterator_follows_cursorObject

Returns the value of attribute iterator_follows_cursor.



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

def iterator_follows_cursor
  @iterator_follows_cursor
end

Instance Method Details

#[](k) ⇒ Object



21
22
23
# File 'lib/usergrid/core/collection.rb', line 21

def [](k)
  entities[k]
end

#[]=(k, v) ⇒ Object



25
26
27
# File 'lib/usergrid/core/collection.rb', line 25

def []=(k,v)
  raise "unsupported operation"
end

#collectionObject



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

def collection
  self
end

#create_entity(data) ⇒ Object Also known as: create_entities



49
50
51
# File 'lib/usergrid/core/collection.rb', line 49

def create_entity(data)
  self.post data
end

#cursorObject



86
87
88
# File 'lib/usergrid/core/collection.rb', line 86

def cursor
  response.data['cursor']
end

#delete_query(query = nil, options = {}) ⇒ Object

options: ‘reversed’, ‘start’, ‘cursor’, ‘limit’, ‘permission’



63
64
65
66
67
68
# File 'lib/usergrid/core/collection.rb', line 63

def delete_query(query=nil, options={})
  options = options.symbolize_keys
  query_params = query ? options.merge({ql: query}) : options
  self.delete({params: query_params })
  self
end

#each(&block) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/usergrid/core/collection.rb', line 34

def each(&block)
  entities.each &block
  while cursor
    next_page
    entities.each &block
  end if iterator_follows_cursor
end

#empty?Boolean



82
83
84
# File 'lib/usergrid/core/collection.rb', line 82

def empty?
  entities.empty?
end

#entitiesObject



16
17
18
19
# File 'lib/usergrid/core/collection.rb', line 16

def entities
  get unless response
  response.entities
end

#follow_cursorObject

use in conjunction with each() like: collection.follow_cursor.each {|e| }



43
44
45
46
47
# File 'lib/usergrid/core/collection.rb', line 43

def follow_cursor
  my_clone = self.clone
  my_clone.iterator_follows_cursor = true
  my_clone
end

#next_pageObject



90
91
92
# File 'lib/usergrid/core/collection.rb', line 90

def next_page
  query(nil, query_params.merge({cursor: cursor}))
end

#query(query = nil, options = {}) ⇒ Object

options: ‘reversed’, ‘start’, ‘cursor’, ‘limit’, ‘permission’



71
72
73
74
75
76
# File 'lib/usergrid/core/collection.rb', line 71

def query(query=nil, options={})
  options = options.symbolize_keys
  query_params = query ? options.merge({ql: query}) : options
  self.get({params: query_params })
  self
end

#saveObject

does not save entities! just fields (eg. ‘name’)



30
31
32
# File 'lib/usergrid/core/collection.rb', line 30

def save
  super save
end

#sizeObject



78
79
80
# File 'lib/usergrid/core/collection.rb', line 78

def size
  entities.size
end

#update_query(updates, query = nil, options = {}) ⇒ Object

options: ‘reversed’, ‘start’, ‘cursor’, ‘limit’, ‘permission’



55
56
57
58
59
60
# File 'lib/usergrid/core/collection.rb', line 55

def update_query(updates, query=nil, options={})
  options = options.symbolize_keys
  query_params = query ? options.merge({ql: query}) : options
  self.put(updates, {params: query_params })
  self
end