i18n_translation_spawner 0.0.13 → 0.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,15 +1,15 @@
1
- require "yaml/encoding"
2
1
  require "i18n_translation_spawner/string"
3
2
  require "i18n_translation_spawner/hash"
3
+ require "i18n_translation_spawner/lambda_accessor"
4
4
 
5
5
  module I18n
6
6
  class TranslationSpawner
7
-
7
+ include LambdaAccessor
8
8
  class CannotDecodeTranslationFilePath < StandardError;
9
9
  end
10
10
 
11
- attr_accessor :skip_locales, :removable_prefixes, :default_translations, :exception_handler, :key_translations_handler,
12
- :translations_handler, :file_path_decoder, :cannot_decode_translation_file_path_handler
11
+ attr_accessor :skip_locales, :removable_prefixes, :default_translations, :exception_handler,
12
+ :translations_handler, :cannot_decode_translation_file_path_handler
13
13
 
14
14
  private
15
15
 
@@ -19,13 +19,15 @@ module I18n
19
19
  @default_translations = {}
20
20
  end
21
21
 
22
- def translation_for_key(key, locale)
23
- if key_translations_handler.respond_to?(:call)
24
- key_translations_handler.call(key, locale, self)
25
- else
26
- default_translation_for_key(key, locale)
27
- end
28
- end
22
+ lambda_accessor :translation_for_key, :file_path_decoder
23
+
24
+ #def translation_for_key(key, locale)
25
+ #if key_translations_handler.respond_to?(:call)
26
+ #key_translations_handler.call(key, locale, self)
27
+ #else
28
+ #default_translation_for_key(key, locale)
29
+ #end
30
+ #end
29
31
 
30
32
  def translation(key, locale)
31
33
  if translations_handler.respond_to?(:call)
@@ -35,18 +37,18 @@ module I18n
35
37
  end
36
38
  end
37
39
 
38
- def decode_file_path(key, locale)
39
- if file_path_decoder.respond_to?(:call)
40
- file_path_decoder.call(key, locale, self)
41
- else
42
- default_decode_file_path(key, locale)
43
- end
44
- end
40
+ #def decode_file_path(key, locale)
41
+ #if file_path_decoder.respond_to?(:call)
42
+ #file_path_decoder.call(key, locale, self)
43
+ #else
44
+ #default_decode_file_path(key, locale)
45
+ #end
46
+ #end
45
47
 
46
48
  def spawn_translation_key(key, locale, options, exception)
47
49
  I18n.available_locales.reject { |l| skip_locales.map(&:to_s).include?(l.to_s) }.each do |_locale|
48
50
  begin
49
- decode_file_path(key, _locale).tap do |path|
51
+ file_path_decoder(key, _locale).tap do |path|
50
52
  translations_hash = YAML::load_file(path)
51
53
  hash_to_merge = "#{_locale.to_s}.#{key}".to_hash(translation(key, _locale.to_s)).deep_stringify_keys!
52
54
  translations_hash = translations_hash.deep_merge(hash_to_merge).to_ordered_hash
@@ -66,7 +68,8 @@ module I18n
66
68
 
67
69
  public
68
70
 
69
- def default_decode_file_path(key, locale)
71
+ def default_file_path_decoder_handler(*args)
72
+ key, locale = args
70
73
  if File.file?(path = File.join(Rails.root, "config/locales", "#{locale.to_s}.yml"))
71
74
  path
72
75
  else
@@ -94,7 +97,8 @@ module I18n
94
97
  end
95
98
  end
96
99
 
97
- def default_translation_for_key(key, locale)
100
+ def default_translation_for_key_handler(*args)
101
+ key, locale = args
98
102
  key.split('.').last.sub(/\A#{removable_prefixes.map { |prefix| prefix+'_' }.join('|')}/, '').humanize
99
103
  end
100
104
 
@@ -116,4 +120,4 @@ module I18n
116
120
  end
117
121
 
118
122
  end
119
- end
123
+ end
@@ -37,4 +37,4 @@ class Hash
37
37
  def to_ordered_hash(deep = false)
38
38
  Hash.convert_hash_to_ordered_hash(self, deep)
39
39
  end
40
- end
40
+ end
@@ -0,0 +1,39 @@
1
+ module LambdaAccessor
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ end
6
+
7
+ module ClassMethods
8
+
9
+ def lambda_accessor(*args)
10
+ args.map(&:to_s).each do |name|
11
+ attr_accessor "#{name}_handler"
12
+
13
+ define_method(name){|*args|
14
+ if (handler = self.send("#{name}_handler")).respond_to?(:call)
15
+ handler.call(*[self, args].flatten)
16
+ else
17
+ self.send("default_#{name}_handler", *args)
18
+ end
19
+ }
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ =begin
26
+ class A
27
+ include LambdaAccessor
28
+ lambda_accessor :test
29
+
30
+ def default_test_handler(*args)
31
+ puts args.shift
32
+ end
33
+ end
34
+
35
+ b = A.new
36
+ b.test('goof')
37
+
38
+ b.test_handler = lambda {|*args| "---- #{args[1]} ----"}
39
+ =end
@@ -3,13 +3,12 @@
3
3
 
4
4
  class String
5
5
  def to_hash(val=nil)
6
- keys = self.split('.')
6
+ keys = split('.')
7
7
  Hash.new.tap do |hsh|
8
- while keys.present? do
9
- k = keys.shift
10
- hsh[k.to_s] = keys.blank? ? val : Hash.new
11
- hsh = hsh[k.to_s]
8
+ while k = keys.shift do
9
+ hsh[k] = keys.empty? ? val : Hash.new
10
+ hsh = hsh[k]
12
11
  end
13
12
  end
14
13
  end
15
- end
14
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n_translation_spawner
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 13
10
- version: 0.0.13
9
+ - 14
10
+ version: 0.0.14
11
11
  platform: ruby
12
12
  authors:
13
13
  - "B\xC5\x82a\xC5\xBCej --Stevo-- Kosmowski"
@@ -42,6 +42,7 @@ extra_rdoc_files: []
42
42
 
43
43
  files:
44
44
  - lib/i18n_translation_spawner/hash.rb
45
+ - lib/i18n_translation_spawner/lambda_accessor.rb
45
46
  - lib/i18n_translation_spawner/string.rb
46
47
  - lib/i18n_translation_spawner.rb
47
48
  homepage: https://github.com/stevo/i18n_translation_spawner