Class: Mongoid::Field
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(name, options = {}) ⇒ Field
constructor
Create the new field with a name and optional additional options.
-
#value(object) ⇒ Object
Gets the value for the supplied object.
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, = {}) @name = name @default = [:default] @type = [:type] || String end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
4 5 6 |
# File 'lib/mongoid/field.rb', line 4 def default @default end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/mongoid/field.rb', line 4 def name @name end |
#type ⇒ Object (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 |