Defines the behaviour for defined fields in the document. Set readers for the instance variables.
Defines the behaviour for defined fields in the document. Set readers for the instance variables.
Defines the behaviour for defined fields in the document. Set readers for the instance variables.
Defines the behaviour for defined fields in the document. Set readers for the instance variables.
Create the new field with a name and optional additional options.
@example Create the new field.
Field.new(:name, :type => String)
@param [ Hash ] options The field options.
@option options [ Class ] :type The class of the field. @option options [ Object ] :default The default value for the field. @option options [ String ] :label The field's label.
@since 3.0.0
# File lib/mongoid/fields/standard.rb, line 83 def initialize(name, options = {}) @name = name @options = options @label = options[:label] @default_val = options[:default] # @todo: Durran, change API in 4.0 to take the class as a parameter. # This is here temporarily to address #2529 without changing the # constructor signature. if default_val.respond_to?(:call) define_default_method(options[:klass]) end end
Adds the atomic changes for this type of resizable field.
@example Add the atomic changes. field.add_atomic_changes(doc, "key", {}, [], [])
@param [ Document ] document The document to add to. @param [ String ] name The name of the field. @param [ String ] key The atomic location of the field. @param [ Hash ] mods The current modifications. @param [ Array ] new The new elements to add. @param [ Array ] old The old elements getting removed.
@since 2.4.0
# File lib/mongoid/fields/standard.rb, line 24 def add_atomic_changes(document, name, key, mods, new, old) mods[key] = new end
Get the constraint from the metadata once.
@example Get the constraint.
field.constraint
@return [ Constraint ] The relation's contraint.
@since 2.1.0
# File lib/mongoid/fields/standard.rb, line 36 def constraint @constraint ||= metadata.constraint end
Evaluate the default value and return it. Will handle the serialization, proc calls, and duplication if necessary.
@example Evaluate the default value.
field.eval_default(document)
@param [ Document ] doc The document the field belongs to.
@return [ Object ] The serialized default value.
@since 2.1.8
# File lib/mongoid/fields/standard.rb, line 51 def eval_default(doc) if fields = Threaded.selection(doc.criteria_instance_id) evaluated_default(doc) if included?(fields) else evaluated_default(doc) end end
Is this field a foreign key?
@example Is the field a foreign key?
field.foreign_key?
@return [ true, false ] If the field is a foreign key.
@since 2.4.0
# File lib/mongoid/fields/standard.rb, line 67 def foreign_key? false end
Does this field do lazy default evaluation?
@example Is the field lazy?
field.lazy?
@return [ true, false ] If the field is lazy.
@since 3.1.0
# File lib/mongoid/fields/standard.rb, line 105 def lazy? false end
Is the field localized or not?
@example Is the field localized?
field.localized?
@return [ true, false ] If the field is localized.
@since 2.3.0
# File lib/mongoid/fields/standard.rb, line 117 def localized? false end
Get the metadata for the field if its a foreign key.
@example Get the metadata.
field.metadata
@return [ Metadata ] The relation metadata.
@since 2.2.0
# File lib/mongoid/fields/standard.rb, line 129 def metadata @metadata ||= options[:metadata] end
Is the field a Moped::BSON::ObjectId?
@example Is the field a Moped::BSON::ObjectId?
field.object_id_field?
@return [ true, false ] If the field is a Moped::BSON::ObjectId.
@since 2.2.0
# File lib/mongoid/fields/standard.rb, line 141 def object_id_field? @object_id_field ||= (type == Moped::BSON::ObjectId) end
Does the field pre-process its default value?
@example Does the field pre-process the default?
field.pre_processed?
@return [ true, false ] If the field's default is pre-processed.
@since 3.0.0
# File lib/mongoid/fields/standard.rb, line 153 def pre_processed? @pre_processed ||= (options[:pre_processed] || (default_val && !default_val.is_a?(::Proc))) end
Get the type of this field - inferred from the class name.
@example Get the type.
field.type
@return [ Class ] The name of the class.
@since 2.1.0
# File lib/mongoid/fields/standard.rb, line 166 def type @type ||= options[:type] || Object end
Is this field included in versioned attributes?
@example Is the field versioned?
field.versioned?
@return [ true, false ] If the field is included in versioning.
@since 2.1.0
# File lib/mongoid/fields/standard.rb, line 178 def versioned? @versioned ||= (options[:versioned].nil? ? true : options[:versioned]) end