Class: MongoMapper::Plugins::Keys::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo_mapper/plugins/keys.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Key

Returns a new instance of Key.



299
300
301
302
303
304
# File 'lib/mongo_mapper/plugins/keys.rb', line 299

def initialize(*args)
  options = args.extract_options!
  @name, @type = args.shift.to_s, args.shift
  self.options = (options || {}).symbolize_keys
  self.default_value = self.options.delete(:default)
end

Instance Attribute Details

#default_valueObject

Returns the value of attribute default_value.



297
298
299
# File 'lib/mongo_mapper/plugins/keys.rb', line 297

def default_value
  @default_value
end

#nameObject

Returns the value of attribute name.



297
298
299
# File 'lib/mongo_mapper/plugins/keys.rb', line 297

def name
  @name
end

#optionsObject

Returns the value of attribute options.



297
298
299
# File 'lib/mongo_mapper/plugins/keys.rb', line 297

def options
  @options
end

#typeObject

Returns the value of attribute type.



297
298
299
# File 'lib/mongo_mapper/plugins/keys.rb', line 297

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



306
307
308
# File 'lib/mongo_mapper/plugins/keys.rb', line 306

def ==(other)
  @name == other.name && @type == other.type
end

#embeddable?Boolean

Returns:



310
311
312
# File 'lib/mongo_mapper/plugins/keys.rb', line 310

def embeddable?
  type.respond_to?(:embeddable?) && type.embeddable? ? true : false
end

#get(value) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
# File 'lib/mongo_mapper/plugins/keys.rb', line 318

def get(value)
  if value.nil? && !default_value.nil?
    if default_value.respond_to?(:call)
      return default_value.call
    else
      return default_value
    end
  end

  type.from_mongo(value)
end

#number?Boolean

Returns:



314
315
316
# File 'lib/mongo_mapper/plugins/keys.rb', line 314

def number?
  [Integer, Float].include?(type)
end

#set(value) ⇒ Object



330
331
332
# File 'lib/mongo_mapper/plugins/keys.rb', line 330

def set(value)
  type.to_mongo(value)
end