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
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.
235 236 237 |
# File 'lib/mongoid/fields.rb', line 235 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.
250 251 252 253 254 |
# File 'lib/mongoid/fields.rb', line 250 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.
271 272 273 274 275 276 277 278 279 |
# File 'lib/mongoid/fields.rb', line 271 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.
292 293 294 295 |
# File 'lib/mongoid/fields.rb', line 292 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 BSON::ObjectIds
as our id.
306 307 308 |
# File 'lib/mongoid/fields.rb', line 306 def using_object_ids? fields["_id"].object_id_field? end |