class Object

Public Class Methods

_translate(*args) click to toggle source
# File lib/coaster/core_ext/object_translation.rb, line 5
def _translate(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  options = _translate_params.merge(options)
  options = options.to_hash.symbolize_keys!

  key = args.shift
  subkey = nil

  if key.is_a?(String)
    if key.start_with?('.')
      subkey = key
    else
      return I18n.t(key, *args, **options)
    end
  elsif key.is_a?(Symbol)
    subkey = ".#{key.to_s}"
  elsif key.nil?
    # do nothing
  else
    return I18n.t(key, *args, **options)
  end

  key_class = options.delete(:class) || self
  subkey = '.self' unless subkey
  key = key_class.name.gsub(/::/, '.')
  key = 'class.' + key + subkey

  unless options.key?(:original_throw)
    options[:original_throw] = options.delete(:throw)
  end
  options[:tkey] ||= key
  options.merge!(throw: true)
  result = catch(:exception) do
    I18n.t(key, *args, **options)
  end

  if result.is_a?(I18n::MissingTranslation)
    unless options.key?(:original_missing)
      options.merge!(original_missing: result)
    end

    if key_class.superclass == Object || key_class == Object
      return options[:description] if options[:description].present?
      case options[:fallback]
      when Proc then return options[:fallback].call(self)
      when Symbol then return self.send(options[:fallback])
      when String then return options[:fallback]
      end
      if Coaster.logger 
        Coaster.logger.info(options[:original_missing])
        Coaster.logger.debug(caller.join("\n"))
      end
      throw :exception, result if options[:original_throw]
      missing = options[:original_missing] || result
      msg = missing.message
      msg = msg.dup
      msg.instance_variable_set(:@missing, missing)
      msg.instance_variable_set(:@tkey, options[:tkey])
      msg
    else
      options[:class] = key_class.superclass
      _translate(subkey, *args, options)
    end
  else
    result = result.dup if result.frozen?
    result.instance_variable_set(:@translated, true)
    result.instance_variable_set(:@tkey, options[:tkey])
    result.instance_variable_set(:@missing, options[:original_missing])
    result
  end
end
_translate_params() click to toggle source
# File lib/coaster/core_ext/object_translation.rb, line 77
def _translate_params
  {}
end

Public Instance Methods

_translate(*args) click to toggle source

Foo::Bar.new._translate #=> return translation ‘class.Foo.Bar.self’ Foo::Bar.new._translate(‘.title’) #=> return translation ‘class.Foo.Bar.title’ Foo::Bar.new._translate(‘title’) #=> return translation ‘title’ Foo::Bar.new._translate(:force) #=> ignore ‘message’ even if message exists

# File lib/coaster/core_ext/object_translation.rb, line 87
def _translate(*args)
  options = (args.last.is_a?(Hash) ? args.pop : {}).with_indifferent_access
  key = args.shift || (respond_to?(:tkey) ? tkey : nil)
  options = _translate_params.merge(options)
  self.class._translate(key, *args, options)
end
_translate_params() click to toggle source
# File lib/coaster/core_ext/object_translation.rb, line 94
def _translate_params
  {}
end
require_more() click to toggle source
# File lib/coaster/core_ext/require_more.rb, line 1
def require_more
  required_file_path = caller[0].split(':', 2).first
  load_name = nil
  load_path_index = $LOAD_PATH.each_with_index do |load_path, ix|
    scanned = required_file_path.scan(/(#{load_path})#{File::SEPARATOR}(.*)/).first
    next false unless scanned
    load_name = scanned[1]
    break ix
  end

  return false unless load_path_index

  more_load_paths = $LOAD_PATH.drop(load_path_index + 1)
  more_load_paths.each do |load_path|
    path = File.join(load_path, load_name)
    if File.exist?(path)
      return require_dependency path
    end
  end

  raise LoadError, "cannot require more -- #{load_name}"
end