class RedisModel
Public Class Methods
all()
click to toggle source
# File lib/redis_model.rb, line 80 def self.all redis.scan_each(match: "distribot.#{table}.by.id:*").map do |key| id = key.gsub(/^distribot\.#{table}\.by\.id\:/,'') find_by_id(id) end end
delete_all()
click to toggle source
# File lib/redis_model.rb, line 95 def self.delete_all self.all.map(&:delete) end
find(id)
click to toggle source
# File lib/redis_model.rb, line 47 def self.find(id) find_by_id(id) end
find_by(args={})
click to toggle source
# File lib/redis_model.rb, line 68 def self.find_by(args={}) send("find_by_#{args.keys.first}", args.values.first) end
find_by_email(email)
click to toggle source
# File lib/redis_model.rb, line 60 def self.find_by_email(email) if id = redis.get("distribot.#{table}.by.email:#{email}") return self.find_by_id(id) else return nil end end
find_by_id(id)
click to toggle source
# File lib/redis_model.rb, line 51 def self.find_by_id(id) raw_json = redis.get("distribot.#{table}.by.id:#{id}") if raw_json self.new JSON.parse(raw_json).merge(id: id) else return nil end end
first()
click to toggle source
# File lib/redis_model.rb, line 19 def self.first all.first end
new(attributes = {})
click to toggle source
# File lib/redis_model.rb, line 8 def initialize(attributes = {}) @attributes = attributes attributes.each do |key,val| send "#{key}=", val end end
table()
click to toggle source
# File lib/redis_model.rb, line 87 def self.table self.class.to_s.downcase.pluralize end
Protected Class Methods
redis()
click to toggle source
# File lib/redis_model.rb, line 101 def self.redis Distribot.redis end
Public Instance Methods
delete()
click to toggle source
# File lib/redis_model.rb, line 72 def delete # Delete the email->id and the id->data values: redis.multi do redis.del("distribot.#{table}.by.id:#{self.id}") redis.del("distribot.#{table}.by.email:#{self.email}") end end
read_attribute_for_validation(key)
click to toggle source
# File lib/redis_model.rb, line 15 def read_attribute_for_validation(key) @attributes[key] end
save(*args)
click to toggle source
# File lib/redis_model.rb, line 27 def save(*args) unless self.valid? return false end if self.id run_callbacks :update do self.update end else run_callbacks :create do self.create end end end
save!()
click to toggle source
# File lib/redis_model.rb, line 23 def save! false unless self.save end
table()
click to toggle source
# File lib/redis_model.rb, line 91 def table self.class.table end
update()
click to toggle source
# File lib/redis_model.rb, line 43 def update raise NotImplementedError.new end
Protected Instance Methods
redis()
click to toggle source
# File lib/redis_model.rb, line 105 def redis self.class.redis end