Module: Mongoid::Fields::ClassMethods
- Defined in:
- lib/mongoid/fields.rb,
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.
-
#extract_id_field(attributes) ⇒ Object
private
Extracts the id field from the specified attributes hash based on aliases defined in this 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.
-
#id_fields ⇒ Array<Symbol | String>
private
Returns the list of id fields for this model class, as both strings and symbols.
-
#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.
273 274 275 |
# File 'lib/mongoid/fields.rb', line 273 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.
288 289 290 291 292 |
# File 'lib/mongoid/fields.rb', line 288 def database_field_name(name) return nil unless name normalized = name.to_s aliased_fields[normalized] || normalized end |
#extract_id_field(attributes) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Extracts the id field from the specified attributes hash based on aliases defined in this class.
74 75 76 77 78 79 80 81 |
# File 'lib/mongoid/fields.rb', line 74 def extract_id_field(attributes) id_fields.each do |k| if v = attributes[k] return v end end nil 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.
309 310 311 312 313 314 315 316 317 |
# File 'lib/mongoid/fields.rb', line 309 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 |
#id_fields ⇒ Array<Symbol | String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the list of id fields for this model class, as both strings and symbols.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mongoid/fields.rb', line 55 def id_fields IDS.dup.tap do |id_fields| aliased_fields.each do |k, v| if v == '_id' id_fields << k.to_sym id_fields << k end end end end |
#replace_field(name, type) ⇒ Serializable
Replace a field with a new type.
330 331 332 333 |
# File 'lib/mongoid/fields.rb', line 330 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.
344 345 346 |
# File 'lib/mongoid/fields.rb', line 344 def using_object_ids? fields["_id"].object_id_field? end |