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

Instance Method Details

#i18n_inflector_kindsHash

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.

Returns:



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

Note:

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.

Examples:

Registering an inflection method for the kind gender visible in a whole application

class ApplicationController < ActionController::Base
  inflection_method :gender
  []
end

Registering an inflection method for the strict kind @gender visible in a whole application

class ApplicationController < ActionController::Base
  inflection_method :@gender
  []
end

Registering a custom-named inflection method for the kind gender

class ApplicationController < ActionController::Base
  inflection_method :get_user_gender => :gender
  []
end

Registering a custom-named inflection methods for the kinds gender and tense

class ApplicationController < ActionController::Base
  inflection_method :get_user_gender => :gender, :get_tense => :tense
  []
end

Registering inflection methods for the kinds gender and tense

class ApplicationController < ActionController::Base
  inflection_method :gender, :tense
  []
end

Registering inflection methods for the kind gender and the strict kind @tense

class ApplicationController < ActionController::Base
  inflection_method :gender, :@tense
  []
end

Registering a custom-named inflection methods for the kinds gender and @gender

# in case of named patterns the method get_strict_gender and the
# strict kind @gender will have priority; the regular kind gender
# and its method would be used if there would be no strict variants
class ApplicationController < ActionController::Base
  inflection_method :get_gender => :gender, :get_strict_gender => :@gender
  []
end

Registering a method for the kind gender and the custom-named method for the kind @gender

class ApplicationController < ActionController::Base
  inflection_method :gender, :get_strict_gender => :@gender
  []
end

Registering a method for the kind gender visible in whole app and a variant for some controller

class ApplicationController < ActionController::Base
  inflection_method :gender
  []
end

# In this controller the method gender will be called
# to obtain inflection option's value for the kind gender
class UsersController < ApplicationController
end

# In this controller the method getit will be called
# to obtain inflection option's value for the kind gender
class OtherController < ApplicationController
  inflection_method :getit => :gender
end

# In this controller no method will be called
# to obtain inflection option's value for the kind gender
class FlowersController < ApplicationController
  no_inflection_method :getit
end

Parameters:

  • *args (Hash{Symbol => Symbol}, Array<Symbol>, Symbol, String)

    the methods and inflection kinds assigned to them

Raises:



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.options.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.

Parameters:

  • names (Array<Symbol>)

    the method names for which the assigned kinds should be marked as not supported in a current controller and all derivative controllers

Raises:



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.

Parameters:

  • kinds (String, Symbol, Array<Symbol>)

    the kind for which the method names should be marked as not supported in a current controller and all derivative controllers

Raises:



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