module ActiveRedis::Attributes::ClassMethods
Attributes
defined_attributes[RW]
Public Instance Methods
attributes(attrs = {})
click to toggle source
# File lib/active_redis/attributes.rb, line 26 def attributes(attrs = {}) raise ActiveRedis::InvalidArgumentError, "Value must be a Hash!" unless attrs.is_a?(Hash) attrs = attrs.merge(ActiveRedis::Constants::DEFAULT_ATTRIBUTES) class << self; attr_accessor :defined_attributes; end self.defined_attributes ||= {} define_attributes_accessors attrs end
attributes_list()
click to toggle source
# File lib/active_redis/attributes.rb, line 42 def attributes_list self.defined_attributes.keys.map(&:to_s) end
define_attributes_accessors(attrs)
click to toggle source
# File lib/active_redis/attributes.rb, line 34 def define_attributes_accessors(attrs) attrs.each do |attribute, type| next unless register_attribute(attribute, type) read_attribute attribute write_attribute attribute end end
Private Instance Methods
attribute_class(attribute)
click to toggle source
# File lib/active_redis/attributes.rb, line 57 def attribute_class(attribute) self.defined_attributes[attribute.to_sym][:class] end
read_attribute(attribute)
click to toggle source
# File lib/active_redis/attributes.rb, line 61 def read_attribute(attribute) define_method attribute do klass = self.class.send :attribute_class, attribute klass.load(instance_variable_get("@#{attribute}")) end end
register_attribute(attribute, type)
click to toggle source
# File lib/active_redis/attributes.rb, line 48 def register_attribute(attribute, type) return false if self.defined_attributes.has_key? attribute.to_sym self.defined_attributes[attribute.to_sym] = { class: "ActiveRedis::Attributes::#{type.to_s.capitalize}Attribute".constantize, type: type } true end
write_attribute(attribute)
click to toggle source
# File lib/active_redis/attributes.rb, line 68 def write_attribute(attribute) define_method "#{attribute}=" do |value| klass = self.class.send :attribute_class, attribute instance_variable_set("@#{attribute}", klass.dump(value)) end end