Module: Cache2base::ClassMethods

Defined in:
lib/cache2base/core.rb

Instance Method Summary collapse

Instance Method Details

#all(fields, params = {}) ⇒ Object



248
249
250
251
# File 'lib/cache2base/core.rb', line 248

def all(fields, params = {})
  arr = server.get(collection_key(fields))
  find_by_keys(Array(arr)).compact
end

#basenameObject



165
166
167
# File 'lib/cache2base/core.rb', line 165

def basename
  @basename
end

#collection_key(vhsh) ⇒ Object



207
208
209
210
# File 'lib/cache2base/core.rb', line 207

def collection_key(vhsh)
  keys = vhsh.keys.sort {|a,b| a.to_s <=> b.to_s}
  "#{@basename}_c_#{hash_collection?(keys) ? hash_key(keys.collect {|field| vhsh[field].to_s.gsub('_','-') }.join("_")) : keys.collect {|field| vhsh[field].to_s.gsub('_','-') }.join("_")}"
end

#collectionsObject



203
204
205
# File 'lib/cache2base/core.rb', line 203

def collections
  @collections
end

#create(params) ⇒ Object



243
244
245
246
# File 'lib/cache2base/core.rb', line 243

def create(params)
  o = self.new(params)
  o.save
end

#fieldsObject



212
213
214
# File 'lib/cache2base/core.rb', line 212

def fields
  @fields
end

#find(fields, params = {}) ⇒ Object



220
221
222
223
224
# File 'lib/cache2base/core.rb', line 220

def find(fields, params = {})
  o = server.get(key(fields))
  return nil unless o
  self.from_hash(Marshal.load(o))
end

#find_by_key(key) ⇒ Object



226
227
228
229
230
# File 'lib/cache2base/core.rb', line 226

def find_by_key(key)
  o = server.get(key)
  return nil unless o
  self.from_hash(Marshal.load(o))
end

#find_by_keys(keys) ⇒ Object



232
233
234
235
236
237
# File 'lib/cache2base/core.rb', line 232

def find_by_keys(keys)
  hsh = server.get_multi(keys)
  keys.collect do |key| # to get it back in order since get_multi results in a hash
    self.from_hash(Marshal.load(hsh[key]))
  end.compact
end

#from_hash(hsh) ⇒ Object



239
240
241
# File 'lib/cache2base/core.rb', line 239

def from_hash(hsh)
  self.new(hsh, :new_instance => false)
end

#hash_collection?(field) ⇒ Boolean

Returns:

  • (Boolean)


216
217
218
# File 'lib/cache2base/core.rb', line 216

def hash_collection?(field)
  @hashed_collections[Array(field).join(',').to_s]
end

#hash_key(k) ⇒ Object



169
170
171
# File 'lib/cache2base/core.rb', line 169

def hash_key(k)
  Digest::SHA1.hexdigest(k.to_s)
end

#member_of_collection(fields, params = {}) ⇒ Object



195
196
197
198
199
200
201
# File 'lib/cache2base/core.rb', line 195

def member_of_collection(fields, params = {})
  fields = Array(fields).sort { |a,b| a.to_s <=> b.to_s }
  @collections ||= []
  @collections << fields
  @hashed_collections ||= {}
  @hashed_collections[fields.join(",").to_s] = true if params[:hash_key]
end

#primary_keyObject



135
136
137
# File 'lib/cache2base/core.rb', line 135

def primary_key
  @primary_key
end

#serverObject



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

def server
  @server
end

#set_basename(name) ⇒ Object



131
132
133
# File 'lib/cache2base/core.rb', line 131

def set_basename(name)
  @basename = name.to_s
end

#set_field(field, params) ⇒ Object



180
181
182
183
184
185
186
187
188
189
# File 'lib/cache2base/core.rb', line 180

def set_field(field, params)
  @fields ||= []
  @fields << field
  @field_meta ||= {}
  if params[:hash]
    @field_meta[field] ||= {}
    @field_meta[field][:hash] = true
  end
  class_eval "attr_accessor :#{field}"
end

#set_fields(*fields) ⇒ Object



173
174
175
176
177
178
# File 'lib/cache2base/core.rb', line 173

def set_fields(*fields)
  @fields = @fields ? (@fields + (fields)) : (fields)
  fields.each do |field|
    class_eval "attr_accessor :#{field}"
  end
end

#set_primary_key(mk, params = {}) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/cache2base/core.rb', line 143

def set_primary_key(mk, params = {})
  @primary_key = Array(mk)
  #o = '#{self.class}'
  #c = "#{self}"
  #h = "#{self}"
  o = []
  c = []
  h = []
  Array(mk).each_with_index do |v, i|
    o << '#{self.send(:'+v.to_s+').to_s.gsub(\'_\',\'-\')}'
    c << '#{Array(pk)['+i.to_s+'].to_s.gsub(\'_\',\'-\')}'
    h << '#{pk[0][:'+v.to_s+'].to_s.gsub(\'_\',\'-\')}'
  end
  
  o = "#{@basename}_\#{#{params[:hash_key] ? "self.class.hash_key(\"#{o.join("_")}\")" : "\"#{o.join("_")}\""}}"
  c = "#{@basename}_\#{#{params[:hash_key] ? "hash_key(\"#{c.join("_")}\")" : "\"#{c.join("_")}\""}}"
  h = "#{@basename}_\#{#{params[:hash_key] ? "hash_key(\"#{h.join("_")}\")" : "\"#{h.join("_")}\""}}"
  
  class_eval "def key; \"#{o}\"; end"
  class_eval "def self.key(*pk); pk.first.is_a?(Hash) ? \"#{h}\" : \"#{c}\"; end"
end

#set_ttl(i) ⇒ Object



127
128
129
# File 'lib/cache2base/core.rb', line 127

def set_ttl(i)
  @ttl = i.to_i
end

#ttlObject



123
124
125
# File 'lib/cache2base/core.rb', line 123

def ttl
  @ttl
end

#uses_hash?(field) ⇒ Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/cache2base/core.rb', line 191

def uses_hash?(field)
  @field_meta[field] && @field_meta[field][:hash]
end