Module: RedisModel::Attribute::ClassMethods

Defined in:
lib/redis_model/attribute.rb

Instance Method Summary collapse

Instance Method Details

#define_redis_attribute_method(attribute_name, klass) ⇒ Object

Internal: Defines getter/setter method for specified attribute.

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

Returns nothing.



109
110
111
112
113
114
115
116
117
# 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 = {}) ⇒ Object

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.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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) ⇒ Object

Public: Defines multiple instance-level attributes.

block - A block contains definitions of multiple attributes.

Example:

class User
  redis_model_attributes
end

Returns nothing.



97
98
99
100
101
# 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