Class ArkanisDevelopment::SimpleLocalization::LangSectionProxy
In: lib/lang_section_proxy.rb
Parent: Object

This little thing is a big part of the magic of doing things behind Rails back. Basically it mimics an variable (ie. number, array, hash, …) by redirecting all calls to another variable of that kind. The target object will be accessed by the Language#[] accessor and therefore will always return the data for the currently selcted language without replacing the proxy object.

This is useful if Rails stors the target data only in a constant. With this proxy the constant can be replaced once (with a proxy) and will always return the language data of the currently selected language.

Methods

Public Class methods

Reads the specified options and the accociated block and save them to instance variables.

Available options are:

:sections

  Specifies the sections of the language file which contain the
  receiver. These sections will be used as parameters to the
  Language#[] method to actually get the receiver.

:orginal_receiver

  Specify the original variable that is replaced by this proxy to store
  this variable inside the proxy. This is useful if you want to combine
  the language data with the original data. The variable specified here
  is accessable as the second variable of the attached block.

:lang_class

  The class supplying the proxy with language data. Defaults to
  <code>ArkanisDevelopment::SimpleLocalization::Language</code>. You
  can for example write a mock class implementing the [] class method
  use and this option to make the proxy use this mock class.

    class LangMock
      def self.[](*sections)
        {:title => 'test data'}
      end
    end

    LangSectionProxy.new :lang_class => LangMock

If you want to combine the original data (supplied to the :orginal_receiver option) with the localized data in some way (ie. merging the old data with the localized data) you can specify a block. The block takes the localized data as the first parameter, the original data as the second parameter and should return the combined result.

  data = {:a => 'first', :b => 'second', :c => 'third'}
  LangSectionProxy.new :sections => [:letter_to_word, :mapping], :original_data => data do |localized, original|
    original.merge localized
  end

[Source]

    # File lib/lang_section_proxy.rb, line 62
62:       def initialize(options, &transformation)
63:         default_options = {:sections => nil, :orginal_receiver => nil, :lang_class => ArkanisDevelopment::SimpleLocalization::Language}
64:         options.reverse_merge! default_options
65:         options.assert_valid_keys default_options.keys
66:         
67:         @sections = options[:sections]
68:         @orginal_receiver = options[:orginal_receiver]
69:         @lang_class = options[:lang_class]
70:         @transformation = transformation
71:       end

Public Instance methods

Intercept all other messages and send them to the receiver.

[Source]

    # File lib/lang_section_proxy.rb, line 90
90:       def method_missing(name, *args, &block)
91:         self.receiver.send name, *args, &block
92:       end

Gets the receiver from the language class and combines this data with the original data if wanted (a block was specified to the constructor). If the lang class isn‘t loaded yet only the original data will be returned as a fallback value.

[Source]

    # File lib/lang_section_proxy.rb, line 77
77:       def receiver
78:         if @lang_class.loaded?
79:           receiver = @lang_class.entry(*@sections)
80:           if @transformation.respond_to?(:call)
81:             receiver = @transformation.arity == 1 ? @transformation.call(receiver) : @transformation.call(receiver, @orginal_receiver)
82:           end
83:           receiver
84:         else
85:           @orginal_receiver
86:         end
87:       end

[Validate]