Module: ActiveRedis::Attributes::ClassMethods
- Extended by:
- ClassMethods
- Included in:
- ClassMethods, Base
- Defined in:
- lib/active_redis/attributes.rb
Instance Method Summary collapse
-
#all ⇒ Array<ActiveRedis>
Get all objects.
-
#cname ⇒ String
Fromatted class name.
-
#connection ⇒ Redis
Redis base connection.
-
#create(attributes) ⇒ ActiveRedis::Base
Create a new instance and save it to redis store.
- #find(id) ⇒ ActiveRedis::Base
- #find_by(basename: nil) ⇒ ActiveRedis::Base
-
#keys(hash = nil) ⇒ Object
Set the keys/attributs for an ActiveRedis::Base child.
- #where(basename: nil) ⇒ Array
Instance Method Details
#all ⇒ Array<ActiveRedis>
Get all objects.
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 |
#cname ⇒ String
Fromatted class name.
210 211 212 |
# File 'lib/active_redis/attributes.rb', line 210 def cname self.name.downcase.pluralize end |
#connection ⇒ Redis
Redis base connection.
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.
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
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
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
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 |