Module: RedisModelExtension::ClassOldInitialize

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

Overview

Old Initialize

port for old initialize method to new structure

Constant Summary collapse

TYPE_TRANSLATIONS =
{ 
  :integer => :to_i, 
  :string => :to_s, 
  :bool => :to_bool, 
  :symbol => :to_sym, 
  :array => :to_array, 
  :hash => :to_hash, 
  :time => :to_time, 
  :date => :to_date 
}

Instance Method Summary collapse

Instance Method Details

#confObject

get config hash



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/redis-model-extension/old_initialize.rb', line 89

def conf
  fields = {}
  redis_fields_config.each do |key, type|
    fields[key] = TYPE_TRANSLATIONS[type] if TYPE_TRANSLATIONS.has_key?(type)
  end
  {
    fields: fields,
    required: @required_config.sort,
    redis_key: redis_key_config,
    redis_aliases: redis_alias_config.inject({}){|o,(k,v)| o[k] = v[:main_fields]; o},
    reject_nil_values: !redis_save_fields_with_nil_conf,
  }
end

#initialize_redis_model_methods(conf) ⇒ Object

old method to initialize redis model extenstion Usage:

REDIS_MODEL_CONF = {
 :fields => { 
   :integer => :to_i,
   :boolean => :to_bool,
   :string => :to_s,
   :symbol => :to_sym,
  }, 
  :required => [:integer, :string],
  :redis_key => [:string, :symbol],
  :redis_aliases => {
    :token => [:symbol]
  },
  # (default is true) if true all nil values will not be saved into redis,
  # there should be problem when you want to set some value to nil and same
  # it will not be saved (use false to prevent this)
  :reject_nil_values => false

} include RedisModel initialize_redis_model_methods REDIS_MODEL_CONF



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/redis-model-extension/old_initialize.rb', line 38

def initialize_redis_model_methods conf
  puts "WARNING: This initilization method is deprecated and will be removed in future! \n Please read documentation how to change your model to use new initialization methods"

  remove_redis_autoincrement_key

  @conf = {:reject_nil_values => true}.merge(conf)
  #take all fields and make methods for them
  conf[:fields].each do |name, action|
    redis_fields_config[name] = TYPE_TRANSLATIONS.invert[action]
    redis_fields_defaults_config[name] = nil

    # define getter method for field
    define_method "#{name}" do
      value_get name
    end
    
    # define setter method for field
    define_method "#{name}=" do |new_value|
      value_set name, new_value
    end
    
    # define exists? method for field
    define_method "#{name}?" do
      value_get(name) && !value_get(name).blank? ? true : false
    end
  end
  
  # save nil values?
  redis_save_fields_with_nil false if !conf.has_key?(:reject_nil_values) || conf[:reject_nil_values] == true

  # save into class config about redis key
  @redis_key_config = conf[:redis_key]

  #validate presence of all fields in key
  @required_config = (@redis_key_config | conf[:required]) 
  (@redis_key_config | conf[:required]).each do |field|
    validates field, :presence => :true
  end

  # save into class config about redis keys
  @redis_alias_config = {}
  conf[:redis_aliases].each do |key, fields|
    @redis_alias_config[key] = { 
      main_fields: fields,
      order_field: nil,
      args_field: nil,
    }
  end
end