i18n_scopes 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -38,7 +38,6 @@ module I18nScopes
38
38
  def i18n_scope(name, *path)
39
39
  @i18n_scope_initialized ||= begin
40
40
  self.send :include, I18nScopes::TranslationMethods
41
- # self.i18n_scope_helper ||= self.i18n_scope_helper.nil? ? {} : self.i18n_scope_helper.dup
42
41
  self.i18n_scope_helper ||= {}
43
42
  true
44
43
  end
@@ -3,8 +3,6 @@ module I18nScopes
3
3
  # this class provides helper methods to determine the actual scope in your class
4
4
  class TranslationHelper
5
5
 
6
- CONTROLLER_SUFFIX = "_controller"
7
-
8
6
  attr_accessor :path
9
7
  attr_accessor :options
10
8
 
@@ -30,7 +28,7 @@ module I18nScopes
30
28
  custom_path += [*options.delete(:path_extension)] if options.include?(:path_extension)
31
29
  respondables = [object]
32
30
  respondables += [*options.delete(:respondable)] if options.include?(:respondable)
33
- respondables << self # add self at last
31
+ respondables << I18nScopes::TranslationUtils # add utils at last
34
32
  custom_path.each do |path_name|
35
33
  if path_name.is_a?(Symbol)
36
34
  # find a respondable which responds_to path_name
@@ -64,7 +62,7 @@ module I18nScopes
64
62
  # * +method_name+: the method name to call
65
63
  # * +parameter_matchings+: additional parameter_matchings as a Hash
66
64
  def value_from_method_call(object, respondables, method_name, parameter_matchings)
67
- method = object.class.instance_method(method_name)
65
+ method = object.method(method_name)
68
66
  if method.arity == 0
69
67
  return object.send(method_name)
70
68
  else
@@ -100,51 +98,7 @@ module I18nScopes
100
98
  return args
101
99
  end
102
100
 
103
- # this method will return all modules in a array, each module will be underscored
104
- #
105
- # === Params:
106
- # * +klass+: the klass of which the modules should be used
107
- def modules(klass)
108
- modules = ActiveSupport::Inflector.deconstantize(klass.name)
109
- modules = ActiveSupport::Inflector.underscore(modules)
110
- modules.split("::")
111
- end
112
-
113
- # this method will return a underscored class_name
114
- #
115
- # Note: plural_class and strip_controller_suffix will be used here
116
- #
117
- # === Params:
118
- # * +klass+: the class on which the name should be get
119
- # * +options+: options like plural_class and strip_controller_suffix
120
- def class_name(klass, options = {})
121
- class_name_options = self.options.merge(options)
122
- class_name = ActiveSupport::Inflector.demodulize(klass.name)
123
- class_name = ActiveSupport::Inflector.underscore(class_name)
124
- class_name = class_name[0, class_name.length - CONTROLLER_SUFFIX.length] if class_name_options[:strip_controller_suffix] && class_name.end_with?(CONTROLLER_SUFFIX)
125
- if class_name_options[:plural_class]
126
- ActiveSupport::Inflector.pluralize(class_name)
127
- else
128
- ActiveSupport::Inflector.singularize(class_name)
129
- end
130
- end
131
-
132
- # returns the method name of the original caller
133
- def method_name
134
- found_scoped_t_at = nil
135
- caller.each_with_index do |call, index|
136
- if call.include?("scoped_translation")
137
- found_scoped_t_at = index
138
- break
139
- end
140
- end
141
- if found_scoped_t_at && call = caller[found_scoped_t_at + 1]
142
- if /^(.+?):(\d+)(?::in `(.*)')?/ =~ call
143
- return Regexp.last_match[3]
144
- end
145
- end
146
- raise "Failed to get method_name"
147
- end
101
+
148
102
 
149
103
  end
150
104
 
@@ -0,0 +1,55 @@
1
+ module I18nScopes
2
+
3
+ module TranslationUtils
4
+
5
+ CONTROLLER_SUFFIX = "_controller"
6
+
7
+ # this method will return all modules in a array, each module will be underscored
8
+ #
9
+ # === Params:
10
+ # * +klass+: the klass of which the modules should be used
11
+ def self.modules(klass)
12
+ modules = ActiveSupport::Inflector.deconstantize(klass.name)
13
+ modules = ActiveSupport::Inflector.underscore(modules)
14
+ modules.split("::")
15
+ end
16
+
17
+ # this method will return a underscored class_name
18
+ #
19
+ # Note: plural_class and strip_controller_suffix will be used here
20
+ #
21
+ # === Params:
22
+ # * +klass+: the class on which the name should be get
23
+ # * +options+: options like plural_class and strip_controller_suffix
24
+ def self.class_name(klass, options = {})
25
+ class_name_options = options.merge(options)
26
+ class_name = ActiveSupport::Inflector.demodulize(klass.name)
27
+ class_name = ActiveSupport::Inflector.underscore(class_name)
28
+ class_name = class_name[0, class_name.length - CONTROLLER_SUFFIX.length] if class_name_options[:strip_controller_suffix] && class_name.end_with?(CONTROLLER_SUFFIX)
29
+ if class_name_options[:plural_class]
30
+ ActiveSupport::Inflector.pluralize(class_name)
31
+ else
32
+ ActiveSupport::Inflector.singularize(class_name)
33
+ end
34
+ end
35
+
36
+ # returns the method name of the original caller
37
+ def self.method_name
38
+ found_scoped_t_at = nil
39
+ caller.each_with_index do |call, index|
40
+ if call.include?("scoped_translation")
41
+ found_scoped_t_at = index
42
+ break
43
+ end
44
+ end
45
+ if found_scoped_t_at && call = caller[found_scoped_t_at + 1]
46
+ if /^(.+?):(\d+)(?::in `(.*)')?/ =~ call
47
+ return Regexp.last_match[3]
48
+ end
49
+ end
50
+ raise "Failed to get method_name"
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -1,3 +1,3 @@
1
1
  module I18nScopes
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/i18n_scopes.rb CHANGED
@@ -7,5 +7,5 @@ require 'i18n_scopes/option_accessor'
7
7
  require 'i18n_scopes/configuration'
8
8
  require 'i18n_scopes/translation_methods'
9
9
  require 'i18n_scopes/translation_helper'
10
+ require 'i18n_scopes/translation_utils'
10
11
 
11
- Object.send :include, I18nScopes::Scope
@@ -1,5 +1,7 @@
1
1
  class ScopeTWithPath
2
2
 
3
+ include I18nScopes::Scope
4
+
3
5
  i18n_default_scope "static_prefix", :class_prefix, :with_a_parameter, :class_name
4
6
  i18n_scope :flash, :class_name, :flashes
5
7
 
@@ -1,6 +1,8 @@
1
1
  module SomeModule
2
2
  class ScopeTWithPathInModule
3
3
 
4
+ include I18nScopes::Scope
5
+
4
6
  i18n_default_scope :modules, :local, :class_name
5
7
 
6
8
  def translate
@@ -1,5 +1,7 @@
1
1
  class ShouldDefineScope
2
2
 
3
+ include I18nScopes::Scope
4
+
3
5
  i18n_default_scope "prefix"
4
6
 
5
7
  end
@@ -1,3 +1,5 @@
1
1
  class ShouldHaveAttachedScopedT
2
+
3
+ include I18nScopes::Scope
2
4
 
3
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n_scopes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-16 00:00:00.000000000 Z
12
+ date: 2012-07-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -47,6 +47,7 @@ files:
47
47
  - lib/i18n_scopes/scope.rb
48
48
  - lib/i18n_scopes/translation_helper.rb
49
49
  - lib/i18n_scopes/translation_methods.rb
50
+ - lib/i18n_scopes/translation_utils.rb
50
51
  - lib/i18n_scopes/version.rb
51
52
  - spec/configuration_spec.rb
52
53
  - spec/example_classes/inherit_from_parent.rb