Module: RedisModelExtension::ClassInitialize

Defined in:
lib/redis-model-extension/initialize.rb

Overview

Class Initialize

redis_field - defines fields to be stored into redis redis_alias - defines aliases for finding models redis_key - defines wich fields will be in redis key redis_key_normalize - normalization of redis key (downcase, transliterate) redis_save_fields_with_nil - enable/disable save of nil fields into redis

Constant Summary collapse

VALID_NORMALIZATIONS =
[:downcase, :transliterate]

Instance Method Summary collapse

Instance Method Details

#redis_alias(name, main_fields, name_of_field_for_order = nil, name_of_field_for_args = nil) ⇒ Object

store informations about redis aliases



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/redis-model-extension/initialize.rb', line 92

def redis_alias name, main_fields, name_of_field_for_order = nil, name_of_field_for_args = nil
  #set fields if they are not allready set!
  if name_of_field_for_order && name_of_field_for_args
    redis_field name_of_field_for_order, :array, [] unless redis_fields_config.has_key?(name_of_field_for_order)
    redis_field name_of_field_for_args, :hash, {} unless redis_fields_config.has_key?(name_of_field_for_args)
  end

  @redis_alias_config ||= {}
  #add specification of dynamic alias
  @redis_alias_config[name] = { 
    main_fields: main_fields,
    order_field: name_of_field_for_order,
    args_field: name_of_field_for_args,
  }

  #create alias methods for find and get (find_by_name, get_by_name)
  create_class_alias_method(name)
end

#redis_field(name, type, default = nil) ⇒ Object

add new field which will be saved into redis

  • name - name of your variable

  • type - type of your variable (:integer, :float, :string, :array, :hash)

  • (default) - default value of your variable



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/redis-model-extension/initialize.rb', line 17

def redis_field name, type, default = nil
  redis_user_field_config << name

  # remember field to save into redis
  redis_fields_config[name] = type

  # remember field default value
  redis_fields_defaults_config[name] = default

  define_attribute_methods [name]
end

#redis_key(*fields) ⇒ Object

set redis key which will be used for storing model



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/redis-model-extension/initialize.rb', line 66

def redis_key *fields
  @redis_key_config = fields.flatten
  
  validate_redis_key
  
  #own specification of redis key - delete autoincrement
  remove_redis_autoincrement_key unless redis_user_field_config.include?(:id) || @redis_key_config.include?(:id)

  # automaticaly add all fields from key to validation
  # if any of fields in redis key is nil
  # then prevent to save it
  @redis_key_config.each do |field|
    validates field, :presence => :true if field != :id
  end
end

#redis_key_normalize(*metrics) ⇒ Object

set redis model to normalize redis keys



83
84
85
86
87
88
89
# File 'lib/redis-model-extension/initialize.rb', line 83

def redis_key_normalize *metrics
  @redis_key_normalize_conf ||= []
  metrics.each do |metric|
    raise ArgumentError, "Please provide valid normalization: #{VALID_NORMALIZATIONS.join(", ")}" unless VALID_NORMALIZATIONS.include?(metric)
    @redis_key_normalize_conf << metric
  end
end

#redis_key_normalize_confObject

store informations about redis key normalization



117
118
119
# File 'lib/redis-model-extension/initialize.rb', line 117

def redis_key_normalize_conf
  @redis_key_normalize_conf ||= []
end

#redis_save_fields_with_nil(store) ⇒ Object

store informations about saving nil values



112
113
114
# File 'lib/redis-model-extension/initialize.rb', line 112

def redis_save_fields_with_nil store
  @redis_save_fields_with_nil_conf = store
end

#remove_redis_autoincrement_keyObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/redis-model-extension/initialize.rb', line 51

def remove_redis_autoincrement_key
  # remove get value
  remove_method :id

  # remove value exists? (not nil and not blank?)
  remove_method "id?"

  # remove set value
  remove_method "id="

  redis_fields_config.delete(:id)
end

#set_redis_autoincrement_keyObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/redis-model-extension/initialize.rb', line 29

def set_redis_autoincrement_key
  @redis_key_config ||= [:id]

  # get value
  define_method :id do
    value_get :id 
  end

  # value exists? (not nil and not blank?)
  define_method "id?" do 
    value_get(:id) && !value_get(:id).blank? ? true : false
  end

  # set value
  define_method "id=" do |new_value|
    value_set :id, new_value
  end
  private :id= #set it as private

  redis_fields_config[:id] = :autoincrement
end