Class: I18n::Alchemy::Proxy
- Inherits:
-
Object
- Object
- I18n::Alchemy::Proxy
- Includes:
- AttributesParsing
- Defined in:
- lib/i18n_alchemy/proxy.rb
Instance Method Summary collapse
-
#initialize(target, attributes = nil) ⇒ Proxy
constructor
TODO: cannot assume _id is always a foreign key.
-
#method_missing(*args, &block) ⇒ Object
Delegate all method calls that are not translated to the target object.
-
#to_json(options = nil) ⇒ Object
Override to_json to always call
to_jsonon the target object, instead of serializing the proxy object, that may issue circular references on Ruby 1.8. -
#to_model ⇒ Object
Override to_model to always return the proxy, otherwise it returns the target object.
-
#to_param ⇒ Object
Override to_param to always return the
proxy.to_param. -
#try(*a, &b) ⇒ Object
Allow calling localized methods with :try.
Methods included from AttributesParsing
#assign_attributes, #attributes=, #update_attribute, #update_attributes, #update_attributes!
Constructor Details
#initialize(target, attributes = nil) ⇒ Proxy
TODO: cannot assume _id is always a foreign key. Find a better way to find that and skip these columns.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/i18n_alchemy/proxy.rb', line 12 def initialize(target, attributes = nil) @target = target @target_class = target.class @localized_attributes = {} @localized_associations = [] build_methods if active_record_compatible? build_attributes build_associations end assign_attributes(attributes) if attributes end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args, &block) ⇒ Object
Delegate all method calls that are not translated to the target object. As the proxy does not have any other method, there is no need to override :respond_to, just delegate it to the target as well.
60 61 62 |
# File 'lib/i18n_alchemy/proxy.rb', line 60 def method_missing(*args, &block) @target.send(*args, &block) end |
Instance Method Details
#to_json(options = nil) ⇒ Object
Override to_json to always call to_json on the target object, instead of serializing the proxy object, that may issue circular references on Ruby 1.8.
36 37 38 |
# File 'lib/i18n_alchemy/proxy.rb', line 36 def to_json( = nil) @target.to_json() end |
#to_model ⇒ Object
Override to_model to always return the proxy, otherwise it returns the target object. This allows us to integrate with action view.
42 43 44 |
# File 'lib/i18n_alchemy/proxy.rb', line 42 def to_model self end |
#to_param ⇒ Object
Override to_param to always return the proxy.to_param. This allow us to integrate with action view.
30 31 32 |
# File 'lib/i18n_alchemy/proxy.rb', line 30 def to_param @target.to_param end |
#try(*a, &b) ⇒ Object
Allow calling localized methods with :try. If the method is not declared here, it’ll be delegated to the target, losing localization capabilities.
53 54 55 |
# File 'lib/i18n_alchemy/proxy.rb', line 53 def try(*a, &b) __send__(*a, &b) end |