Class: I18n::Alchemy::Proxy
- Inherits:
- ActiveSupport::BasicObject
- Includes:
- AttributesParsing
- Defined in:
- lib/i18n_alchemy/proxy.rb
Overview
Depend on AS::BasicObject which has a “blank slate” - no methods.
Instance Method Summary collapse
-
#initialize(target, attributes = nil, *args) ⇒ 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_json
on 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, *args) ⇒ Proxy
TODO: cannot assume _id is always a foreign key. Find a better way to find that and skip these columns.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/i18n_alchemy/proxy.rb', line 9 def initialize(target, attributes=nil, *args) @target = target @localized_attributes = {} @localized_associations = [] build_methods if active_record_compatible? build_attributes build_associations end assign_attributes(attributes, *args) 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.
55 56 57 |
# File 'lib/i18n_alchemy/proxy.rb', line 55 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.
32 33 34 |
# File 'lib/i18n_alchemy/proxy.rb', line 32 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.
38 39 40 |
# File 'lib/i18n_alchemy/proxy.rb', line 38 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.
26 27 28 |
# File 'lib/i18n_alchemy/proxy.rb', line 26 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.
48 49 50 |
# File 'lib/i18n_alchemy/proxy.rb', line 48 def try(*a, &b) __send__(*a, &b) end |