Class: Flareshow::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/resource.rb

Direct Known Subclasses

Comment, FileAttachment, Flow, Invitation, Membership, Post, User

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}, source = :client) ⇒ Resource

constructor build a new Flareshow::Base resource



62
63
64
65
66
67
# File 'lib/resource.rb', line 62

def initialize(data={}, source = :client)
  @data = {}
  Flareshow::Util.log_info("creating #{self.class.name} with data from #{source}")
  update(data, source)
  @data["id"] = UUID.generate.upcase if source == :client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

fallback to getter or setter



195
196
197
198
# File 'lib/resource.rb', line 195

def method_missing(meth, *args)
  meth = meth.to_s
  meth.match(/\=/) ? set(meth.gsub(/\=/,''), *args) : get(meth)
end

Class Attribute Details

.attr_accessibleObject

Returns the value of attribute attr_accessible.



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

def attr_accessible
  @attr_accessible
end

.attr_requiredObject

Returns the value of attribute attr_required.



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

def attr_required
  @attr_required
end

.read_onlyObject

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



28
29
30
# File 'lib/resource.rb', line 28

def cache_response(response)
  Flareshow::CacheManager.assimilate_resources(response["resources"])
end

.create(params = {}) ⇒ Object

create a resource local and sync it to the server



50
51
52
# File 'lib/resource.rb', line 50

def create(params={})
  new(params).save
end

.default_paramsObject



6
7
8
9
10
# File 'lib/resource.rb', line 6

def default_params
  # default parameters that are included with query
  # requests unless they are explicitly overridden
  {:order => "created_at desc"}
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



35
36
37
38
39
# File 'lib/resource.rb', line 35

def find(params={})
  params = default_params.merge(params)
  response = Flareshow::Service.query({resource_key => params})
  (cache_response(response) || {})[resource_key]
end

.first(params = {}) ⇒ Object

find just one resource matching the conditions specified



42
43
44
45
46
47
# File 'lib/resource.rb', line 42

def first(params={})
  params = default_params.merge(params)
  params = params.merge({"limit" => 1})
  response = Flareshow::Service.query({resource_key => params})
  (cache_response(response) || {})[resource_key].first
end

.get_from_cache(id) ⇒ Object

find an existing instance of this object in the client or create a new one



18
19
20
# File 'lib/resource.rb', line 18

def get_from_cache(id)
  store.get_resource(resource_key, id)
end

.list_cacheObject

list out the instances in memory



23
24
25
# File 'lib/resource.rb', line 23

def list_cache
  store.list_resource(resource_key).map{|k,v|v}
end

.resource_keyObject

return the resource key for this resource



13
14
15
# File 'lib/resource.rb', line 13

def resource_key
  Flareshow::ClassToResourceMap[self.name]
end

.storeObject



55
56
57
# File 'lib/resource.rb', line 55

def store
  Flareshow::CacheManager.cache
end

Instance Method Details

#cacheObject

store a resource in the cache



75
76
77
# File 'lib/resource.rb', line 75

def cache
  self.class.store.store.set_resource(resource_key, id, self)
end

#changesObject

all the state that has been modified on the client



184
185
186
187
188
189
190
191
192
# File 'lib/resource.rb', line 184

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

#destroyObject

destroy the resource on the server



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/resource.rb', line 106

def destroy
  raise Flareshow::APIAccessException if self.class.read_only
  response = Flareshow::Service.commit({resource_key => [{"id" => id, "_removed" => true}]})
  cache_response(response)
  mark_destroyed!
  self
rescue Exception => e
  Flareshow::Util.log_error e.message
  throw e
  false
end

#destroyed?Boolean

has this resource been destroyed

Returns:

  • (Boolean)


119
120
121
# File 'lib/resource.rb', line 119

def destroyed?
  self._removed || self.frozen?
end

#get(key) ⇒ Object

get a data value



179
180
181
# File 'lib/resource.rb', line 179

def get(key)
  @data[key]
end

#idObject

return the server id of a resource



139
140
141
# File 'lib/resource.rb', line 139

def id
  @data["id"]
end

#method_nameObject

has this model been removed on the server



201
202
203
# File 'lib/resource.rb', line 201

def method_name
  !!self._removed
end

#refreshObject

reload the resource from the server



84
85
86
87
88
# File 'lib/resource.rb', line 84

def refresh
  results = self.find({"id" => id})
  mark_destroyed! if results.empty?
  self
end

#resource_keyObject

return the resource key for this resource



70
71
72
# File 'lib/resource.rb', line 70

def resource_key
  Flareshow::ClassToResourceMap[self.class.name]
end

#saveObject

save a resource to the server



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/resource.rb', line 91

def save
  raise Flareshow::APIAccessException if self.class.read_only
  key = Flareshow::ClassToResourceMap[self.class.name]
  raise Flareshow::MissingRequiredField unless !self.class.attr_required || (self.class.attr_required.map{|a|a.to_s} - @data.keys).empty?
  response = Flareshow::Service.commit({resource_key => [(self.changes || {}).merge({"id" => id})] })
  cache_response(response)
  self
rescue Exception => e
  Flareshow::Util.log_error e.message
  Flareshow::Util.log_error e.backtrace.join("\n")
  throw e
  false
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



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/resource.rb', line 161

def set(key, value, source = :client)
  raise Flareshow::APIAccessException if self.class.read_only && source == :client
  if self.class.attr_accessible && 
    !(/_removed/).match(key.to_s) &&
    !self.class.attr_accessible.include?(key.to_sym) && source == :client
    Flareshow::Util.log_error "#{self.class.name}.#{key} is not a writable field"
    raise Flareshow::APIAccessException 
  end
  # Flareshow::Util.log_info("setting #{key} : #{value}")
  @data["original_#{key}"] = value if source == :server
  @data[key.to_s]=value
rescue Exception => e
  Flareshow::Util.log_error e.message
  throw e
  false
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



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/resource.rb', line 146

def update(attributes, source = :client)
  raise Flareshow::APIAccessException if self.class.read_only && source == :client
  attributes.each do |p|
    key, value = p[0], p[1]
    self.set(key, value, source)
  end
rescue Exception => e
  Flareshow::Util.log_error e.message
  throw e
  false
end