Module: Mongoid::Fields::ClassMethods
- Defined in:
- lib/mongoid/fields.rb
Overview
:nodoc
Instance Method Summary collapse
-
#defaults ⇒ Hash
Returns the default values for the fields on the document.
-
#defaults=(defaults) ⇒ Object
Set the defaults for the class.
-
#field(name, options = {}) ⇒ Field
Defines all the fields that are accessible on the Document For each field that is defined, a getter and setter will be added as an instance method to the Document.
-
#fields ⇒ Hash
Return the fields for this class.
-
#fields=(fields) ⇒ Object
Set the fields for the class.
-
#inherited(subclass) ⇒ Object
When inheriting, we want to copy the fields from the parent class and set the on the child to start, mimicking the behaviour of the old class_inheritable_accessor that was deprecated in Rails edge.
-
#object_id_field?(name) ⇒ true, false
Is the field with the provided name a BSON::ObjectId?.
-
#replace_field(name, type) ⇒ Serializable
Replace a field with a new type.
Instance Method Details
#defaults ⇒ Hash
Returns the default values for the fields on the document.
112 113 114 |
# File 'lib/mongoid/fields.rb', line 112 def defaults @defaults ||= [] end |
#defaults=(defaults) ⇒ Object
Set the defaults for the class.
124 125 126 |
# File 'lib/mongoid/fields.rb', line 124 def defaults=(defaults) @defaults = defaults end |
#field(name, options = {}) ⇒ Field
Defines all the fields that are accessible on the Document For each field that is defined, a getter and setter will be added as an instance method to the Document.
143 144 145 146 |
# File 'lib/mongoid/fields.rb', line 143 def field(name, = {}) check_field_name!(name) add_field(name.to_s, ) end |
#fields ⇒ Hash
Return the fields for this class.
156 157 158 |
# File 'lib/mongoid/fields.rb', line 156 def fields @fields ||= {} end |
#fields=(fields) ⇒ Object
Set the fields for the class.
168 169 170 |
# File 'lib/mongoid/fields.rb', line 168 def fields=(fields) @fields = fields end |
#inherited(subclass) ⇒ Object
When inheriting, we want to copy the fields from the parent class and set the on the child to start, mimicking the behaviour of the old class_inheritable_accessor that was deprecated in Rails edge.
182 183 184 185 |
# File 'lib/mongoid/fields.rb', line 182 def inherited(subclass) super subclass.defaults, subclass.fields = defaults.dup, fields.dup end |
#object_id_field?(name) ⇒ true, false
Is the field with the provided name a BSON::ObjectId?
197 198 199 200 201 202 |
# File 'lib/mongoid/fields.rb', line 197 def object_id_field?(name) field_name = name.to_s field_name = "_id" if field_name == "id" field = fields[field_name] field ? field.object_id_field? : false end |
#replace_field(name, type) ⇒ Serializable
Replace a field with a new type.
215 216 217 218 |
# File 'lib/mongoid/fields.rb', line 215 def replace_field(name, type) defaults.delete_one(name) add_field(name, fields[name]..merge(:type => type)) end |