Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

lambda accessor refactoring

  • Loading branch information...
commit 44aee3f20cdaf8343ded60d06921da85c8e273a5 1 parent 1a90ccf
@stevo authored
View
47 lib/i18n_translation_spawner.rb
@@ -1,15 +1,16 @@
require "yaml/encoding"
require "i18n_translation_spawner/string"
require "i18n_translation_spawner/hash"
+require "i18n_translation_spawner/lambda_accessor"
module I18n
class TranslationSpawner
-
+ include LambdaAccessor
class CannotDecodeTranslationFilePath < StandardError;
end
- attr_accessor :skip_locales, :removable_prefixes, :default_translations, :exception_handler, :key_translations_handler,
- :translations_handler, :file_path_decoder, :cannot_decode_translation_file_path_handler
+ attr_accessor :skip_locales, :removable_prefixes, :default_translations, :exception_handler,
+ :translations_handler, :cannot_decode_translation_file_path_handler
private
@@ -19,13 +20,15 @@ def initialize
@default_translations = {}
end
- def translation_for_key(key, locale)
- if key_translations_handler.respond_to?(:call)
- key_translations_handler.call(key, locale, self)
- else
- default_translation_for_key(key, locale)
- end
- end
+ lambda_accessor :translation_for_key, :file_path_decoder
+
+ #def translation_for_key(key, locale)
+ #if key_translations_handler.respond_to?(:call)
+ #key_translations_handler.call(key, locale, self)
+ #else
+ #default_translation_for_key(key, locale)
+ #end
+ #end
def translation(key, locale)
if translations_handler.respond_to?(:call)
@@ -35,18 +38,18 @@ def translation(key, locale)
end
end
- def decode_file_path(key, locale)
- if file_path_decoder.respond_to?(:call)
- file_path_decoder.call(key, locale, self)
- else
- default_decode_file_path(key, locale)
- end
- end
+ #def decode_file_path(key, locale)
+ #if file_path_decoder.respond_to?(:call)
+ #file_path_decoder.call(key, locale, self)
+ #else
+ #default_decode_file_path(key, locale)
+ #end
+ #end
def spawn_translation_key(key, locale, options, exception)
I18n.available_locales.reject { |l| skip_locales.map(&:to_s).include?(l.to_s) }.each do |_locale|
begin
- decode_file_path(key, _locale).tap do |path|
+ file_path_decoder(key, _locale).tap do |path|
translations_hash = YAML::load_file(path)
hash_to_merge = "#{_locale.to_s}.#{key}".to_hash(translation(key, _locale.to_s)).deep_stringify_keys!
translations_hash = translations_hash.deep_merge(hash_to_merge).to_ordered_hash
@@ -66,7 +69,8 @@ def spawn_translation_key(key, locale, options, exception)
public
- def default_decode_file_path(key, locale)
+ def default_file_path_decoder_handler(*args)
+ key, locale = args
if File.file?(path = File.join(Rails.root, "config/locales", "#{locale.to_s}.yml"))
path
else
@@ -94,7 +98,8 @@ def default_translation(key, locale)
end
end
- def default_translation_for_key(key, locale)
+ def default_translation_for_key_handler(*args)
+ key, locale = args
key.split('.').last.sub(/\A#{removable_prefixes.map { |prefix| prefix+'_' }.join('|')}/, '').humanize
end
@@ -116,4 +121,4 @@ def handle_exception(exception, locale, key, options)
end
end
-end
+end
View
39 lib/i18n_translation_spawner/lambda_accessor.rb
@@ -0,0 +1,39 @@
+module LambdaAccessor
+ extend ActiveSupport::Concern
+
+ included do
+ end
+
+ module ClassMethods
+
+ def lambda_accessor(*args)
+ args.map(&:to_s).each do |name|
+ attr_accessor "#{name}_handler"
+
+ define_method(name){|*args|
+ if (handler = self.send("#{name}_handler")).respond_to?(:call)
+ handler.call(*[self, args].flatten)
+ else
+ self.send("default_#{name}_handler", *args)
+ end
+ }
+ end
+ end
+ end
+end
+
+=begin
+class A
+ include LambdaAccessor
+ lambda_accessor :test
+
+ def default_test_handler(*args)
+ puts args.shift
+ end
+end
+
+b = A.new
+b.test('goof')
+
+b.test_handler = lambda {|*args| "---- #{args[1]} ----"}
+=end
Please sign in to comment.
Something went wrong with that request. Please try again.