Module: Mongoid::Attributes::Dynamic
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
ActiveSupport::Concern
|
|
Defined in: | lib/mongoid/attributes/dynamic.rb |
Overview
This module contains the behavior for dynamic attributes.
Instance Method Summary
-
#define_dynamic_before_type_cast_reader(name)
Internal use only
Internal use only
Define a reader method for a dynamic attribute before type cast.
-
#define_dynamic_reader(name)
Internal use only
Internal use only
Define a reader method for a dynamic attribute.
-
#define_dynamic_writer(name)
Internal use only
Internal use only
Define a writer method for a dynamic attribute.
-
#inspect_dynamic_fields ⇒ String
Get an array of inspected dynamic fields for the document.
-
#method_missing(name, *args) ⇒ Object
Internal use only
Internal use only
Used for allowing accessor methods for dynamic attributes.
-
#process_attribute(name, value)
If the attribute is dynamic, add a field for it with a type of object and set the value.
-
#respond_to?(name, include_private = false) ⇒ true | false
Override respond_to? so it responds properly for dynamic attributes.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
Used for allowing accessor methods for dynamic attributes.
# File 'lib/mongoid/attributes/dynamic.rb', line 122
def method_missing(name, *args) attr = name.to_s return super unless attributes.has_key?(attr.reader) if attr.writer? getter = attr.reader define_dynamic_writer(getter) write_attribute(getter, args.first) elsif attr.before_type_cast? define_dynamic_before_type_cast_reader(attr.reader) attribute_will_change!(attr.reader) read_attribute_before_type_cast(attr.reader) else getter = attr.reader define_dynamic_reader(getter) attribute_will_change!(attr.reader) read_raw_attribute(getter) end end
Instance Method Details
#define_dynamic_before_type_cast_reader(name)
Define a reader method for a dynamic attribute before type cast.
# File 'lib/mongoid/attributes/dynamic.rb', line 54
def define_dynamic_before_type_cast_reader(name) class_eval do define_method("#{name}_before_type_cast") do attribute_will_change!(name) read_attribute_before_type_cast(name) end end end
#define_dynamic_reader(name)
Define a reader method for a dynamic attribute.
# File 'lib/mongoid/attributes/dynamic.rb', line 35
def define_dynamic_reader(name) return unless name.valid_method_name? class_eval do define_method(name) do attribute_will_change!(name) read_raw_attribute(name) end end end
#define_dynamic_writer(name)
Define a writer method for a dynamic attribute.
# File 'lib/mongoid/attributes/dynamic.rb', line 71
def define_dynamic_writer(name) return unless name.valid_method_name? class_eval do define_method("#{name}=") do |value| write_attribute(name, value) end end end
#inspect_dynamic_fields ⇒ String
Get an array of inspected dynamic fields for the document.
# File 'lib/mongoid/attributes/dynamic.rb', line 104
def inspect_dynamic_fields keys = attributes.keys - fields.keys - relations.keys - ["_id", self.class.discriminator_key] return keys.map do |name| "#{name}: #{attributes[name].inspect}" end end
#process_attribute(name, value)
If the attribute is dynamic, add a field for it with a type of object and set the value.
# File 'lib/mongoid/attributes/dynamic.rb', line 89
def process_attribute(name, value) responds = respond_to?("#{name}=") if !responds write_attribute(name, value) else send("#{name}=", value) end end
#respond_to?(name, include_private = false) ⇒ true
| false
Override respond_to? so it responds properly for dynamic attributes.
# File 'lib/mongoid/attributes/dynamic.rb', line 20
def respond_to?(name, include_private = false) super || ( attributes && attributes.has_key?(name.to_s.reader) ) end