module ActiveRedis::Attributes::ClassMethods

Public Instance Methods

all() click to toggle source

Get all objects.

@return [Array<ActiveRedis>]

# 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
cname() click to toggle source

Fromatted class name.

@return [String]

# File lib/active_redis/attributes.rb, line 210
def cname
  self.name.downcase.pluralize
end
connection() click to toggle source

Redis base connection.

@return [Redis]

# File lib/active_redis/attributes.rb, line 241
def connection
  ActiveRedis.redis
end
create(attributes) click to toggle source

Create a new instance and save it to redis store.

@return [ActiveRedis::Base]

# File lib/active_redis/attributes.rb, line 219
def create attributes
  a = self.new(attributes)
  a.save
  a
end
find(id) click to toggle source

@param id [Integer]

@return [ActiveRedis::Base]

# 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) click to toggle source

@see where

@return [ActiveRedis::Base]

# File lib/active_redis/attributes.rb, line 260
def find_by basename: nil
  if basename
    where(basename: basename).first
  end
end
keys(hash=nil) click to toggle source

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

Example:

keys title: String,
  number: Integer
# File lib/active_redis/attributes.rb, line 288
def keys hash=nil
  hash ? (@keys ||= hash) : @keys
end
where(basename: nil) click to toggle source

@params basename [String]

@return [Array]

# 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