Class: Mongoid::Field
Overview
Defines the behaviour for defined fields in the document.
Constant Summary collapse
- NO_CAST_ON_READ =
[ Array, Binary, Boolean, Float, Hash, Integer, BSON::ObjectId, Set, String, Symbol ]
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.
Class Method Summary collapse
-
.option(option_name, &block) ⇒ Object
Stores the provided block to be run when the option name specified is defined on a field.
-
.options ⇒ Hash
Return a map of custom option names to their handlers.
Instance Method Summary collapse
-
#cast_on_read? ⇒ true, false
When reading the field do we need to cast the value? This holds true when times are stored or for big decimals which are stored as strings.
-
#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.
84 85 86 87 88 89 90 |
# File 'lib/mongoid/field.rb', line 84 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.
13 14 15 |
# File 'lib/mongoid/field.rb', line 13 def copyable @copyable end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
13 14 15 |
# File 'lib/mongoid/field.rb', line 13 def klass @klass end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
13 14 15 |
# File 'lib/mongoid/field.rb', line 13 def label @label end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/mongoid/field.rb', line 13 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/mongoid/field.rb', line 13 def @options end |
#type ⇒ Object
Returns the value of attribute type.
12 13 14 |
# File 'lib/mongoid/field.rb', line 12 def type @type end |
Class Method Details
.option(option_name, &block) ⇒ Object
Stores the provided block to be run when the option name specified is defined on a field.
No assumptions are made about what sort of work the handler might perform, so it will always be called if the ‘option_name` key is provided in the field definition – even if it is false or nil.
43 44 45 |
# File 'lib/mongoid/field.rb', line 43 def option(option_name, &block) [option_name] = block end |
.options ⇒ Hash
Return a map of custom option names to their handlers.
24 25 26 |
# File 'lib/mongoid/field.rb', line 24 def @options ||= {} end |
Instance Method Details
#cast_on_read? ⇒ true, false
When reading the field do we need to cast the value? This holds true when times are stored or for big decimals which are stored as strings.
56 57 58 |
# File 'lib/mongoid/field.rb', line 56 def cast_on_read? !NO_CAST_ON_READ.include?(type) end |
#default ⇒ Object
Get the default value for the field.
68 69 70 |
# File 'lib/mongoid/field.rb', line 68 def default copy.respond_to?(:call) ? copy : set(copy) end |
#get(object) ⇒ Object
Used for retrieving the object out of the attributes hash.
129 130 131 |
# File 'lib/mongoid/field.rb', line 129 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.
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/mongoid/field.rb', line 107 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 |