Module: ActiveRedis::Attributes::ClassMethods

Extended by:
ClassMethods
Included in:
ClassMethods, Base
Defined in:
lib/active_redis/attributes.rb

Instance Method Summary collapse

Instance Method Details

#allArray<ActiveRedis>

Get all objects.

Returns:



230
231
232
233
234
# File 'lib/active_redis/attributes.rb', line 230

def all
  connection.keys("#{cname}:*").map do |key|
    self.new({id: key[/#{cname}:(\d+)/,1].to_i},reload: true)
  end
end

#cnameString

Fromatted class name.

Returns:

  • (String)


210
211
212
# File 'lib/active_redis/attributes.rb', line 210

def cname
  self.name.downcase.pluralize
end

#connectionRedis

Redis base connection.

Returns:

  • (Redis)


241
242
243
# File 'lib/active_redis/attributes.rb', line 241

def connection
  ActiveRedis.redis
end

#create(attributes) ⇒ ActiveRedis::Base

Create a new instance and save it to redis store.

Returns:



219
220
221
222
223
# File 'lib/active_redis/attributes.rb', line 219

def create attributes
  a = self.new(attributes)
  a.save
  a
end

#find(id) ⇒ ActiveRedis::Base

Parameters:

  • id (Integer)

Returns:



250
251
252
253
# File 'lib/active_redis/attributes.rb', line 250

def find id
  a = connection.hgetall("#{cname}:#{id}")
  self.new({id: id},reload: true) unless a.empty?
end

#find_by(basename: nil) ⇒ ActiveRedis::Base

Returns:

See Also:



260
261
262
263
264
# File 'lib/active_redis/attributes.rb', line 260

def find_by basename: nil
  if basename
    where(basename: basename).first
  end
end

#keys(hash = nil) ⇒ Object

Set the keys/attributs for an ActiveRedis::Base child.

Example:

keys title: String,
  number: Integer


288
289
290
# File 'lib/active_redis/attributes.rb', line 288

def keys hash=nil
  hash ? (@keys ||= hash) : @keys
end

#where(basename: nil) ⇒ Array

Returns:

  • (Array)


271
272
273
274
275
276
277
278
# File 'lib/active_redis/attributes.rb', line 271

def where basename: nil
  if basename
    a = connection.smembers(basename)
    a.map{ |entry| self.find(entry) }
  else
    []
  end
end