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.



277
278
279
280
281
282
# File 'lib/mongo_mapper/plugins/keys.rb', line 277

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.



275
276
277
# File 'lib/mongo_mapper/plugins/keys.rb', line 275

def default_value
  @default_value
end

#nameObject

Returns the value of attribute name.



275
276
277
# File 'lib/mongo_mapper/plugins/keys.rb', line 275

def name
  @name
end

#optionsObject

Returns the value of attribute options.



275
276
277
# File 'lib/mongo_mapper/plugins/keys.rb', line 275

def options
  @options
end

#typeObject

Returns the value of attribute type.



275
276
277
# File 'lib/mongo_mapper/plugins/keys.rb', line 275

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



284
285
286
# File 'lib/mongo_mapper/plugins/keys.rb', line 284

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

#embeddable?Boolean

Returns:



288
289
290
# File 'lib/mongo_mapper/plugins/keys.rb', line 288

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

#get(value) ⇒ Object



296
297
298
299
300
301
302
# File 'lib/mongo_mapper/plugins/keys.rb', line 296

def get(value)
  if value.nil? && !default_value.nil?
    return default_value
  end

  type.from_mongo(value)
end

#number?Boolean

Returns:



292
293
294
# File 'lib/mongo_mapper/plugins/keys.rb', line 292

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

#set(value) ⇒ Object



304
305
306
# File 'lib/mongo_mapper/plugins/keys.rb', line 304

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