Class: Mongoid::Fields::Localized
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Standard ,
Forwardable
|
|
Instance Chain:
self,
Standard
|
|
Inherits: |
Mongoid::Fields::Standard
|
Defined in: | lib/mongoid/fields/localized.rb |
Overview
Represents a ::BSON
document field definition which stores different values for different user locale keys in a Ruby hashmap (BSON “Object” type). Used for internationalization (I18n) support.
Class Method Summary
Instance Attribute Summary
-
#localize_present? ⇒ true | false
readonly
Is the localized field enforcing values to be present?
-
#localized? ⇒ true | false
readonly
Is the field localized or not?
-
#fallbacks? ⇒ true | false
readonly
private
Internal use only
Internal use only
Are fallbacks being used for this localized field.
Standard
- Inherited
#default_val | Defines the behavior for defined fields in the document. |
#foreign_key? | Is this field a foreign key? |
#label | Defines the behavior for defined fields in the document. |
#lazy? | Does this field do lazy default evaluation? |
#localize_present? | Is the localized field enforcing values to be present? |
#localized? | Is the field localized or not? |
#name | Defines the behavior for defined fields in the document. |
#object_id_field? | Is the field a BSON::ObjectId? |
#options | Defines the behavior for defined fields in the document. |
#pre_processed? | Does the field pre-process its default value? |
Instance Method Summary
-
#demongoize(object) ⇒ Object
Demongoize the object based on the current locale.
-
#mongoize(object) ⇒ Hash
Convert the provided string into a hash for the locale.
-
#lookup(object) ⇒ Object
private
Internal use only
Internal use only
Lookup the value from the provided object.
Standard
- Inherited
#add_atomic_changes | Adds the atomic changes for this type of resizable field. |
#association | Get the metadata for the field if its a foreign key. |
#eval_default | Evaluate the default value and return it. |
#type | Get the type of this field - inferred from the class name. |
#default_name | Get the name of the default method for this field. |
#define_default_method | Define the method for getting the default on the document. |
#evaluate_default_proc | Evaluate the default proc. |
#evaluated_default | Get the evaluated default. |
#included? | Is the field included in the fields that were returned from the database? We can apply the default if: |
#serialize_default | This is used when default values need to be serialized. |
Constructor Details
This class inherits a constructor from Mongoid::Fields::Standard
Instance Attribute Details
#fallbacks? ⇒ true
| false
(readonly, private)
Are fallbacks being used for this localized field.
# File 'lib/mongoid/fields/localized.rb', line 72
def fallbacks? return true if [:fallbacks].nil? !! [:fallbacks] end
#localize_present? ⇒ true
| false
(readonly)
Is the localized field enforcing values to be present?
# File 'lib/mongoid/fields/localized.rb', line 46
def localize_present? [:localize] == :present end
#localized? ⇒ true
| false
(readonly)
Is the field localized or not?
# File 'lib/mongoid/fields/localized.rb', line 36
def localized? true end
Instance Method Details
#demongoize(object) ⇒ Object
Demongoize the object based on the current locale. Will look in the hash for the current locale.
#lookup(object) ⇒ Object
(private)
Lookup the value from the provided object.
# File 'lib/mongoid/fields/localized.rb', line 87
def lookup(object) locale = ::I18n.locale value = if object.key?(locale.to_s) object[locale.to_s] elsif object.key?(locale) object[locale] end return value unless value.nil? if fallbacks? && ::I18n.respond_to?(:fallbacks) fallback_key = ::I18n.fallbacks[locale].find do |loc| object.key?(loc.to_s) || object.key?(loc) end object[fallback_key.to_s] || object[fallback_key] end end
#mongoize(object) ⇒ Hash
Convert the provided string into a hash for the locale.
# File 'lib/mongoid/fields/localized.rb', line 58
def mongoize(object) { ::I18n.locale.to_s => type.mongoize(object) } end