Module: RedisModelExtension::Initialize

Extended by:
ActiveSupport::Concern
Defined in:
lib/redis-model-extension/initialize.rb

Instance Method Summary collapse

Instance Method Details

#attribute_method?(attr_name) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/redis-model-extension/initialize.rb', line 149

def attribute_method?(attr_name)
  self.class.redis_user_field_config.include? attr_name.to_sym
end

#initialize(args = {}) ⇒ Object

initialize instance



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/redis-model-extension/initialize.rb', line 154

def initialize(args={})
  args = HashWithIndifferentAccess.new(args)
  # look for fields in input hash
  redis_fields_config.each do |key, type|
    # disable to set nonexisting ID!
    raise ArgumentError, "You cannot specify #{key} (it is auto incremented)" if args[key] && type == :autoincrement && get_last_id.to_i < args[key].to_i

    # input hash has known field
    value = if args.has_key?(key)
      value_parse(args[key], type)
    else #there is no value set default valued
      redis_fields_defaults_config[key]
    end
    send(:attribute=, key, value)
  end

  return self
end