Class: Mongoid::Field
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#accessible? ⇒ Boolean
Determine if the field is able to be accessible via a mass update.
-
#default ⇒ Object
Get the default value for the field.
-
#get(object) ⇒ Object
Used for retrieving the object out of the attributes hash.
-
#initialize(name, options = {}) ⇒ Field
constructor
Create the new field with a name and optional additional options.
-
#set(object) ⇒ Object
Used for setting an object in the attributes hash.
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)
35 36 37 38 39 40 |
# File 'lib/mongoid/field.rb', line 35 def initialize(name, = {}) @name, @default = name, [:default] @copyable = (@default.is_a?(Array) || @default.is_a?(Hash)) @type = [:type] || String @accessible = .has_key?(:accessible) ? [:accessible] : true end |
Instance Attribute Details
#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
#accessible? ⇒ Boolean
Determine if the field is able to be accessible via a mass update.
Returns:
true if accessible, false if not.
11 12 13 |
# File 'lib/mongoid/field.rb', line 11 def accessible? !!@accessible end |
#default ⇒ Object
Get the default value for the field.
Returns:
The primitive value or a copy of the default.
20 21 22 |
# File 'lib/mongoid/field.rb', line 20 def default copy end |
#get(object) ⇒ Object
Used for retrieving the object out of the attributes hash.
49 50 51 |
# File 'lib/mongoid/field.rb', line 49 def get(object) type.get(object) end |
#set(object) ⇒ Object
Used for setting an object in the attributes hash. If nil is provided the default will get returned if it exists.
44 45 46 |
# File 'lib/mongoid/field.rb', line 44 def set(object) type.set(object) end |