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

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

Constant Summary

ID_STR =
'_id'

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Key) initialize(*args)

A new instance of Key



10
11
12
13
14
15
16
17
18
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 10

def initialize(*args)
  options_from_args = args.extract_options!
  @name, @type = args.shift.to_s, args.shift
  self.options = (options_from_args || {}).symbolize_keys

  if options.key?(:default)
    self.default = self.options[:default]
  end
end

Instance Attribute Details

- (Object) default

Returns the value of attribute default



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

def default
  @default
end

- (Object) name

Returns the value of attribute name



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

def name
  @name
end

- (Object) options

Returns the value of attribute options



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

def options
  @options
end

- (Object) type

Returns the value of attribute type



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

def type
  @type
end

Instance Method Details

- (Object) ==(other)



20
21
22
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 20

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

- (Boolean) default?

Returns:



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

def default?
  options.key?(:default)
end

- (Object) default_value



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 62

def default_value
  return unless default?

  if default.respond_to?(:call)
    default.call
  else
    # Using Marshal is easiest way to get a copy of mutable objects
    # without getting an error on immutable objects
    Marshal.load(Marshal.dump(default))
  end
end

- (Boolean) embeddable?

Returns:



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

def embeddable?
  # This is ugly, but it's fast. We can't use ||= because false is an expected and common value.
  @embeddable = @embeddable != nil ? @embeddable : begin
    if type.respond_to?(:embeddable?)
      type.embeddable?
    else
      false
    end
  end
end

- (Object) get(value)



43
44
45
46
47
48
49
50
51
52
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 43

def get(value)
  # Special Case: Generate default _id on access
  value = default_value if value.nil? and name == ID_STR

  if options[:typecast]
    type.from_mongo(value).map! { |v| typecast_class.from_mongo(v) }
  else
    type.from_mongo(value)
  end
end

- (Boolean) number?

Returns:



35
36
37
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 35

def number?
  type == Integer || type == Float
end

- (Object) set(value)



54
55
56
57
58
59
60
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 54

def set(value)
  type.to_mongo(value).tap do |values|
    if options[:typecast]
      values.map! { |v| typecast_class.to_mongo(v) }
    end
  end
end