module ActiveRedis::Attributes::InstanceMethods
Public Instance Methods
Get attribute paths by keys. Complex types needs to be stored at another path.
@return [Array]
# File lib/active_redis/attributes.rb, line 192 def attribute_paths keys.map do |k,v| case v.to_s when /Hash/, /Array/ then "#{basename}:#{k}" else basename end end.uniq end
Get the current attributes.
@return [Hash]
# File lib/active_redis/attributes.rb, line 50 def attributes get_attributes end
Redis basename.
@return [String]
# File lib/active_redis/attributes.rb, line 182 def basename "#{self.class.name.downcase.pluralize}:#{self.id}" end
@see ActiveRedis#connection
# File lib/active_redis/attributes.rb, line 64 def connection self.class.connection end
Create getter/setter/bool methods by attributes. This method should be called in a constructor.
@return [void]
# File lib/active_redis/attributes.rb, line 84 def create_attribute_methods self.class.instance_eval do keys.each do |attribute,type| define_method(attribute) do attributes[attribute] end define_method("#{attribute}=") do |arg| attributes[attribute] = format_attribute(attribute,arg.to_s) end define_method("#{attribute}?") do !attributes[attribute].to_s.empty? end end end end
Casting objects into original values. The type whould be defined at class method “keys”
@see ActiveRedis.keys
@param key [String,Symbol] @param value [Object]
@return [Object] Casted value.
# File lib/active_redis/attributes.rb, line 134 def format_attribute key, value case keys[key.to_sym].to_s when /Integer/ value.to_i when /Array/ connection.lrange("#{basename}:#{key}",0,-1) when /Hash/ connection.hgetall("#{basename}:#{key}") when /Time/ Time.parse(value) rescue nil else value end end
Get attributes from redis store.
@param reload [true,false] Force reload from redis.
@return [Hash]
# File lib/active_redis/attributes.rb, line 109 def get_attributes reload: false if reload or not @h h = (@h and @h[:id]) ? connection.hgetall(basename) : {} attribute_hash = {} unless h.empty? keys.each do |k,v| attribute_hash[k.to_sym] = format_attribute(k.to_sym, h[k.to_s]) end end @h = attribute_hash end @h end
Reformat object.
@return [String]
# File lib/active_redis/attributes.rb, line 20 def inspect a = keys.reject{|k,v| k == :id }.map do |attribute,type| %Q{#{attribute}=#{send(attribute).inspect}} end %Q{#<#{self.class.name} id=#{self.id} #{a.join(' ')}>} end
@see ActiveRedis#keys
# File lib/active_redis/attributes.rb, line 57 def keys self.class.keys end
Implement ActiveModel validation support.
# File lib/active_redis/attributes.rb, line 71 def read_attribute_for_validation key attributes[key] end
Remove the instance from redis store. Remove all relations to another objects.
@return [void]
# File lib/active_redis/attributes.rb, line 33 def remove blto = self.class.instance_variable_get(:@belongs_to) (blto || []).each do |rel| handle_relation relation_key: "#{rel.to_s.singularize}_id", action: :del end connection.del *attribute_paths end
Save the current record to redis store.
@return [true,false]
# File lib/active_redis/attributes.rb, line 11 def save valid? ? (set_attributes(attributes) && true) : false end
Save given hash to redis server. Refresh updated_at timestamp. Unless id isn’t set, get the next primary key.
@param arg [Hash]
@return [void]
# File lib/active_redis/attributes.rb, line 157 def set_attributes arg remove arg[:updated_at] = Time.now.to_s arg[:id] ||= connection.incr("#{self.class.name}_id") arg.each do |k,v| case v when Array v.each do |v| connection.lpush "#{basename}:#{k}", v end when Hash connection.mapped_hmset "#{basename}:#{k}", v else handle_relation relation_key: k, value: v, action: :add connection.hset basename, k, v end end get_attributes(reload: true) end