Class: Mongoid::Field
Overview
Defines the behaviour for defined fields in the document.
Instance Attribute Summary collapse
-
#copyable ⇒ Object
readonly
Returns the value of attribute copyable.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
-
#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.
34 35 36 37 38 39 40 |
# File 'lib/mongoid/field.rb', line 34 def initialize(name, = {}) @type = [:type] || Object @name, @default, @label = name, [:default], [:label] @copyable = (@default.is_a?(Array) || @default.is_a?(Hash)) @options = check_default! end |
Instance Attribute Details
#copyable ⇒ Object (readonly)
Returns the value of attribute copyable.
8 9 10 |
# File 'lib/mongoid/field.rb', line 8 def copyable @copyable end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
8 9 10 |
# File 'lib/mongoid/field.rb', line 8 def klass @klass end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
8 9 10 |
# File 'lib/mongoid/field.rb', line 8 def label @label end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/mongoid/field.rb', line 8 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/mongoid/field.rb', line 8 def @options end |
#type ⇒ Object
Returns the value of attribute type.
7 8 9 |
# File 'lib/mongoid/field.rb', line 7 def type @type end |
Instance Method Details
#default ⇒ Object
Get the default value for the field.
18 19 20 |
# File 'lib/mongoid/field.rb', line 18 def default copy.respond_to?(:call) ? copy : set(copy) end |
#get(object) ⇒ Object
Used for retrieving the object out of the attributes hash.
79 80 81 |
# File 'lib/mongoid/field.rb', line 79 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.
If the field is an identity field, ie an id, it performs the necessary cast.
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/mongoid/field.rb', line 57 def set(object) unless [:identity] type.set(object) else if object.blank? type.set(object) if object.is_a?(Array) else [:metadata].constraint.convert(object) end end end |