Module: I18n::Inflector::Rails::ClassMethods
- Defined in:
- lib/i18n-inflector-rails/inflector.rb
Overview
This module contains class methods for ActionController.
Instance Method Summary collapse
-
#i18n_inflector_kinds ⇒ Hash
This method reads the internal Hash
i18n_inflector_kinds
containing registered inflection methods and the assigned kinds. -
#inflection_method(*args)
(also: #inflection_methods)
This method allows to assign methods (typically attribute readers) to inflection kinds that are defined in translation files and supported by I18n::Inflector module.
-
#no_inflection_method(*names)
(also: #no_inflection_methods)
This method unregisters inflection kinds from assignments created by #inflection_method.
-
#no_inflection_method_for(*names)
(also: #no_inflection_methods_for, #no_inflection_kind)
This method unregisters the given inflection kinds from assignments created by #inflection_method.
Instance Method Details
#i18n_inflector_kinds ⇒ Hash
This method reads the internal Hash i18n_inflector_kinds
containing registered inflection methods and the assigned kinds. It also reads any methods assignments that were defined earlier in the inheritance path and merges them with current results; the most current entries will override the entries defined before.
44 45 46 47 |
# File 'lib/i18n-inflector-rails/inflector.rb', line 44 def i18n_inflector_kinds prev = superclass.respond_to?(:i18n_inflector_kinds) ? superclass.i18n_inflector_kinds : {} return @i18n_inflector_kinds.nil? ? prev : prev.merge(@i18n_inflector_kinds) end |
#inflection_method(*args) Also known as: inflection_methods
Any added method will become a helper unless InflectionOptions#auto_helper swtich is set to false
!
This method returns an undefined value.
This method allows to assign methods (typically attribute readers) to inflection kinds that are defined in translation files and supported by I18n::Inflector module. Methods registered like that will be tracked when translate is used and their returning values will be passed as inflection options along with assigned kinds. If the kind is not given then method assumes that the name of a kind is the same as the given name of a method.
If the given kind begins with @ then strict kind is assumed. If there is no kind given but the given method name begins with @ character then also strict kind of the same name is assumed but method name is memorized without the leading symbol.
Registering method for feeding an inflection option describing a strict kind might be good idea when using some regular kind of the same name, but note that regular kind inflection option is also tried by the translation method when strict kind is in use.
In case of registering two methods of different names but assigned to kind and to a strict kind using the same base name, a named inflection pattern will first use an inflection option obtained from a method assigned to a strict kind. Note that it is impossible to use short formed inflection_method
calls to register a method for both strict and regular inflection kind, since the method names will be the same and the second call will overwrite the first one.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/i18n-inflector-rails/inflector.rb', line 154 def inflection_method(*args) args = args.flatten if args.empty? raise I18n::Inflector::Rails::BadInflectionMethod.new(assignment) end assignment = {} args.each do |e| if (e.is_a?(Symbol) || e.is_a?(String)) assignment[e] = e elsif e.is_a?(Hash) raise I18n::Inflector::Rails::BadInflectionMethod.new(assignment) if e.empty? assignment.merge!(e) else raise I18n::Inflector::Rails::BadInflectionMethod.new(assignment) end end @i18n_inflector_kinds ||= {} assignment.each_pair do |method, kind| method = method.to_s if (method.empty? || I18n::Inflector::Config::Reserved::Kinds.invalid?(kind, :OPTION)) raise I18n::Inflector::Rails::BadInflectionMethod.new("#{method.inspect} => #{kind.inspect}") end kind = kind.to_s method = method[1..-1] if (method[0..0] == I18n::Inflector::Config::Markers::PATTERN && method == kind) kind = kind.to_sym method = method.to_sym helper_method(method) if I18n.backend.inflector..auto_helper @i18n_inflector_kinds[kind] = method end end |
#no_inflection_method(*names) Also known as: no_inflection_methods
This method returns an undefined value.
This method unregisters inflection kinds from assignments created by #inflection_method. It is useful when there is a need to break inheritance in some controller, but there was a method assigned to some inflection kind in a parrent class.
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/i18n-inflector-rails/inflector.rb', line 199 def no_inflection_method(*names) names = names.flatten if (names.nil? || names.empty?) raise I18n::Inflector::Rails::BadInflectionMethod.new(names) end @i18n_inflector_kinds ||= {} names.each do |meth| unless (meth.is_a?(Symbol) || meth.is_a?(String)) raise I18n::Inflector::Rails::BadInflectionMethod.new(meth) end meth = meth.to_s meth = meth[1..-1] if meth[0..0] == I18n::Inflector::Config::Markers::PATTERN # for dummies raise I18n::Inflector::Rails::BadInflectionMethod.new(meth) if meth.empty? meth = meth.to_sym i18n_inflector_kinds.each_pair do |kind, obj| if obj == meth @i18n_inflector_kinds[kind] = nil end end end end |
#no_inflection_method_for(*names) Also known as: no_inflection_methods_for, no_inflection_kind
This method returns an undefined value.
This method unregisters the given inflection kinds from assignments created by #inflection_method. It is useful when there is a need to break inheritance in some controller, but there was a method assigned to some inflection kind in a parrent class.
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/i18n-inflector-rails/inflector.rb', line 233 def no_inflection_method_for(*names) names = names.flatten if (names.nil? || names.empty?) raise I18n::Inflector::Rails::BadInflectionMethod.new(names) end @i18n_inflector_kinds ||= {} names.each do |kind| unless (kind.is_a?(Symbol) || kind.is_a?(String)) raise I18n::Inflector::Rails::BadInflectionKind.new(kind) end if (I18n::Inflector::Config::Reserved::Kinds.invalid?(kind, :OPTION) || kind.to_s == I18n::Inflector::Config::Markers::PATTERN) raise I18n::Inflector::Rails::BadInflectionKind.new(kind) end @i18n_inflector_kinds[kind.to_sym] = nil end end |