Class: Usergrid::Entity

Inherits:
Resource
  • Object
show all
Defined in:
lib/usergrid/core/entity.rb

Direct Known Subclasses

Collection

Constant Summary

Constants inherited from Resource

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

Instance Attribute Summary

Attributes inherited from Resource

#api_url, #current_user, #response

Instance Method Summary collapse

Methods inherited from Resource

#api_resource, #application, #auth_token, #auth_token=, #delete_query, #entities, #entity, #get, #logged_in?, #login, #logout, #management, #post, #put, #query, #update_query

Constructor Details

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

Returns a new instance of Entity.



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



51
52
53
54
55
56
57
58
59
# File 'lib/usergrid/core/entity.rb', line 51

def method_missing(method, *args, &block)
  if data.respond_to?(method)
    data.send(method, *args, &block)
  elsif method[-1] == '=' && args.size == 1
    data[method[0..-2]] = args[0]
  else
    super method, args, block
  end
end

Instance Method Details

#[](k) ⇒ Object



23
24
25
# File 'lib/usergrid/core/entity.rb', line 23

def [](k)
  data[k]
end

#[]=(k, v) ⇒ Object



27
28
29
# File 'lib/usergrid/core/entity.rb', line 27

def []=(k,v)
  data[k] = v
end

#collectionObject



31
32
33
# File 'lib/usergrid/core/entity.rb', line 31

def collection
  Collection.new url[0..url[0..-2].rindex('/')-1], api_url, options
end

#dataObject



10
11
12
13
# File 'lib/usergrid/core/entity.rb', line 10

def data
  get unless response
  @index ? response.entities_data[@index] : response.entity_data
end

#data?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/usergrid/core/entity.rb', line 15

def data?
  !!response
end

#resourceObject



19
20
21
# File 'lib/usergrid/core/entity.rb', line 19

def resource
  Resource.new url, api_url, options
end

#saveObject



35
36
37
# File 'lib/usergrid/core/entity.rb', line 35

def save
  self.put data
end

#to_json(*args) ⇒ Object Also known as: encode, dump



43
44
45
# File 'lib/usergrid/core/entity.rb', line 43

def to_json(*args)
  data.except(RESERVED).to_json *args
end

#to_sObject



39
40
41
# File 'lib/usergrid/core/entity.rb', line 39

def to_s
  "resource: #{url}\ndata: #{data}"
end