Class: Mongoid::Field

Inherits:
Object show all
Defined in:
lib/mongoid/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Field

Create the new field with a name and optional additional options. Valid options are :default

Options:

name: The name of the field as a Symbol. options: A Hash of options for the field.

Example:

Field.new(:score, :default => 0)



20
21
22
23
24
# File 'lib/mongoid/field.rb', line 20

def initialize(name, options = {})
  @name = name
  @default = options[:default]
  @type = options[:type] || String
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



4
5
6
# File 'lib/mongoid/field.rb', line 4

def default
  @default
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/mongoid/field.rb', line 4

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/mongoid/field.rb', line 4

def type
  @type
end

Instance Method Details

#value(object) ⇒ Object

Gets the value for the supplied object. If the object is nil, then the default value will be returned.



28
29
30
# File 'lib/mongoid/field.rb', line 28

def value(object)
  object ? type.cast(object) : default
end