Class: Flareshow::Resource
- Inherits:
-
Object
- Object
- Flareshow::Resource
- Defined in:
- lib/resource.rb
Direct Known Subclasses
Comment, FileAttachment, Flow, Invitation, Membership, Post, User
Class Attribute Summary collapse
-
.attr_accessible ⇒ Object
Returns the value of attribute attr_accessible.
-
.read_only ⇒ Object
Returns the value of attribute read_only.
Class Method Summary collapse
-
.cache_response(response) ⇒ Object
store the response resources in the cache.
-
.create(params = {}) ⇒ Object
create a resource local and sync it to the server.
-
.find(params = {}) ⇒ Object
find a resource by querying the server store the results in the cache and return the keyed resources for the model performing the query.
-
.get_from_cache(id) ⇒ Object
find an existing instance of this object in the client or create a new one.
-
.list_cache ⇒ Object
list out the instances in memory.
-
.resource_key ⇒ Object
return the resource key for this resource.
- .store ⇒ Object
Instance Method Summary collapse
-
#cache ⇒ Object
store a resource in the cache.
-
#changes ⇒ Object
all the state that has been modified on the client.
-
#destroy ⇒ Object
destroy the resource on the server.
-
#destroyed? ⇒ Boolean
has this resource been destroyed.
-
#get(key) ⇒ Object
get a data value.
-
#id ⇒ Object
return the server id of a resource.
-
#initialize(data = {}, source = :client) ⇒ Resource
constructor
constructor build a new Flareshow::Base resource.
-
#method_missing(meth, *args) ⇒ Object
fallback to getter or setter.
-
#method_name ⇒ Object
has this model been removed on the server.
-
#refresh ⇒ Object
reload the resource from the server.
-
#resource_key ⇒ Object
return the resource key for this resource.
-
#save ⇒ Object
save a resource to the server.
-
#set(key, value, source = :client) ⇒ Object
keep track of dirty state on the client by maintaining a copy of the original state of each intstance variable when it is provided by the server.
-
#update(attributes, source = :client) ⇒ Object
update the instance data for this resource keeping track of dirty state if the update came from the client.
Constructor Details
#initialize(data = {}, source = :client) ⇒ Resource
constructor build a new Flareshow::Base resource
47 48 49 50 51 |
# File 'lib/resource.rb', line 47 def initialize(data={}, source = :client) @data = {} data["id"] = UUID.generate.upcase if source == :client update(data, source) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
fallback to getter or setter
151 152 153 154 |
# File 'lib/resource.rb', line 151 def method_missing(meth, *args) meth = meth.to_s meth.match(/\=/) ? set(meth.gsub(/\=/,''), *args) : get(meth) end |
Class Attribute Details
.attr_accessible ⇒ Object
Returns the value of attribute attr_accessible.
4 5 6 |
# File 'lib/resource.rb', line 4 def attr_accessible @attr_accessible end |
.read_only ⇒ Object
Returns the value of attribute read_only.
4 5 6 |
# File 'lib/resource.rb', line 4 def read_only @read_only end |
Class Method Details
.cache_response(response) ⇒ Object
store the response resources in the cache
22 23 24 |
# File 'lib/resource.rb', line 22 def cache_response(response) Flareshow::CacheManager.assimilate_resources(response["resources"]) end |
.create(params = {}) ⇒ Object
create a resource local and sync it to the server
35 36 37 |
# File 'lib/resource.rb', line 35 def create(params={}) new(params).save end |
.find(params = {}) ⇒ Object
find a resource by querying the server store the results in the cache and return the keyed resources for the model performing the query
29 30 31 32 |
# File 'lib/resource.rb', line 29 def find(params={}) response = Flareshow::Service.query({resource_key => params}) (cache_response(response) || {})[resource_key] end |
.get_from_cache(id) ⇒ Object
find an existing instance of this object in the client or create a new one
12 13 14 |
# File 'lib/resource.rb', line 12 def get_from_cache(id) store.get_resource(resource_key, id) end |
.list_cache ⇒ Object
list out the instances in memory
17 18 19 |
# File 'lib/resource.rb', line 17 def list_cache store.list_resource(resource_key) end |
.resource_key ⇒ Object
return the resource key for this resource
7 8 9 |
# File 'lib/resource.rb', line 7 def resource_key Flareshow::ClassToResourceMap[self.name] end |
.store ⇒ Object
40 41 42 |
# File 'lib/resource.rb', line 40 def store Flareshow::CacheManager.cache end |
Instance Method Details
#cache ⇒ Object
store a resource in the cache
59 60 61 |
# File 'lib/resource.rb', line 59 def cache self.class.store.store.set_resource(resource_key, id, self) end |
#changes ⇒ Object
all the state that has been modified on the client
140 141 142 143 144 145 146 147 148 |
# File 'lib/resource.rb', line 140 def changes attributes = @data.inject({}) do |memo, pair| key, value = *pair if @data[key] != @data["original_#{key}"] && !key.to_s.match(/original/) memo[key] = value end memo end end |
#destroy ⇒ Object
destroy the resource on the server
83 84 85 86 87 88 |
# File 'lib/resource.rb', line 83 def destroy response = Flareshow::Service.commit({resource_key => [{"id" => id, "_removed" => true}]}) cache_response(response) mark_destroyed! self end |
#destroyed? ⇒ Boolean
has this resource been destroyed
91 92 93 |
# File 'lib/resource.rb', line 91 def destroyed? self._removed || self.frozen? end |
#get(key) ⇒ Object
get a data value
135 136 137 |
# File 'lib/resource.rb', line 135 def get(key) @data[key] end |
#id ⇒ Object
return the server id of a resource
111 112 113 |
# File 'lib/resource.rb', line 111 def id @data["id"] end |
#method_name ⇒ Object
has this model been removed on the server
157 158 159 |
# File 'lib/resource.rb', line 157 def method_name !!self._removed end |
#refresh ⇒ Object
reload the resource from the server
68 69 70 71 72 |
# File 'lib/resource.rb', line 68 def refresh results = self.find({"id" => id}) mark_destroyed! if results.empty? self end |
#resource_key ⇒ Object
return the resource key for this resource
54 55 56 |
# File 'lib/resource.rb', line 54 def resource_key Flareshow::ClassToResourceMap[self.class.name] end |
#save ⇒ Object
save a resource to the server
75 76 77 78 79 80 |
# File 'lib/resource.rb', line 75 def save key = Flareshow::ClassToResourceMap[self.class.name] response = Flareshow::Service.commit({resource_key => [(self.changes || {}).merge({"id" => id})] }) cache_response(response) self end |
#set(key, value, source = :client) ⇒ Object
keep track of dirty state on the client by maintaining a copy of the original state of each intstance variable when it is provided by the server
128 129 130 131 132 |
# File 'lib/resource.rb', line 128 def set(key, value, source = :client) # Flareshow::Util.log_info("setting #{key} : #{value}") @data["original_#{key}"] = value if source == :server @data[key.to_s]=value end |
#update(attributes, source = :client) ⇒ Object
update the instance data for this resource keeping track of dirty state if the update came from the client
118 119 120 121 122 123 |
# File 'lib/resource.rb', line 118 def update(attributes, source = :client) attributes.each do |p| key, value = p[0], p[1] self.set(key, value, source) end end |