module RedisModel::Attribute::ClassMethods

Public Instance Methods

define_redis_attribute_method(attribute_name, klass) click to toggle source

Internal: Defines getter/setter method for specified attribute.

attribute_name - Name of the attribute. klass - Class for the attribute.

Returns nothing.

# File lib/redis_model/attribute.rb, line 109
def define_redis_attribute_method(attribute_name, klass)
  define_method(attribute_name) do
    klass.new(parent: self).to_value
  end

  define_method("#{attribute_name}=") do |value|
    klass.new(parent: self).set(value)
  end
end
redis_model_attribute(attribute_name, type, options = {}) click to toggle source

Public: Defines an instance-level attribute.

attribute_name - Name of the attribute. type - Data type of the attribute. options - Additional options for the attribute definition.

:foreign_key - Foreign key used for the attribute.

Returns nothing.

# File lib/redis_model/attribute.rb, line 69
def redis_model_attribute(attribute_name, type, options = {})
  Class.new(RedisModel::BelongedTo) do
    data_type type

    if options[:foreign_key]
      custom_key_label do |redis_model|
        redis_model.parent.send(options[:foreign_key])
      end
    else
      custom_key_label(&:parent_id)
    end
  end.tap do |klass|
    const_set(attribute_name.to_s.camelize, klass)
    define_redis_attribute_method(attribute_name, klass)
  end
end
redis_model_attributes(options = {}, &block) click to toggle source

Public: Defines multiple instance-level attributes.

block - A block contains definitions of multiple attributes.

Example:

class User
  redis_model_attributes
end

Returns nothing.

# File lib/redis_model/attribute.rb, line 97
def redis_model_attributes(options = {}, &block)
  RedisModel::Attribute::DefinitionHelper.new(self, options).tap do |definition_helper|
    definition_helper.instance_eval(&block) if block_given?
  end
end