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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Key

Returns a new instance of Key.



7
8
9
10
11
12
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 7

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.



5
6
7
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 5

def default_value
  @default_value
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 5

def name
  @name
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 5

def options
  @options
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 5

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



14
15
16
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 14

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

#embeddable?Boolean

Returns:



18
19
20
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 18

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

#get(value) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 26

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:



22
23
24
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 22

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

#set(value) ⇒ Object



38
39
40
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 38

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