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.
203 204 205 |
# File 'lib/mongoid/fields.rb', line 203 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.
218 219 220 221 222 |
# File 'lib/mongoid/fields.rb', line 218 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.
239 240 241 242 243 244 245 246 247 |
# File 'lib/mongoid/fields.rb', line 239 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.
260 261 262 263 |
# File 'lib/mongoid/fields.rb', line 260 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.
274 275 276 |
# File 'lib/mongoid/fields.rb', line 274 def using_object_ids? fields["_id"].object_id_field? end |