Module: Mongoid::Fields::ClassMethods
- Defined in:
- lib/mongoid/fields.rb
Instance Method Summary collapse
-
#attribute_names ⇒ Array<String>
Returns an array of names for the attributes available on this object.
-
#database_field_name(name) ⇒ String
Get the name of the provided field as it is stored in the database.
-
#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.
-
#replace_field(name, type) ⇒ Serializable
Replace a field with a new type.
-
#using_object_ids? ⇒ true, false
Convenience method for determining if we are using
Moped::BSON::ObjectIds
as our id.
Instance Method Details
#attribute_names ⇒ Array<String>
Returns an array of names for the attributes available on this object.
Provides the field names in an ORM-agnostic way. Rails v3.1+ uses this method to automatically wrap params in JSON requests.
205 206 207 |
# File 'lib/mongoid/fields.rb', line 205 def attribute_names fields.keys end |
#database_field_name(name) ⇒ String
Get the name of the provided field as it is stored in the database. Used in determining if the field is aliased or not.
220 221 222 223 224 |
# File 'lib/mongoid/fields.rb', line 220 def database_field_name(name) return nil unless name normalized = name.to_s aliased_fields[normalized] || normalized 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.
241 242 243 244 245 246 247 248 249 |
# File 'lib/mongoid/fields.rb', line 241 def field(name, = {}) named = name.to_s Validators::Macro.validate(self, name, ) added = add_field(named, ) descendants.each do |subclass| subclass.add_field(named, ) end added end |
#replace_field(name, type) ⇒ Serializable
Replace a field with a new type.
262 263 264 265 |
# File 'lib/mongoid/fields.rb', line 262 def replace_field(name, type) remove_defaults(name) add_field(name, fields[name]..merge(type: type)) end |
#using_object_ids? ⇒ true, false
Convenience method for determining if we are using Moped::BSON::ObjectIds
as our id.
276 277 278 |
# File 'lib/mongoid/fields.rb', line 276 def using_object_ids? fields["_id"].object_id_field? end |