module Card::Set::I18nScope

Public Instance Methods

mod_name(backtrace) click to toggle source

extract the mod name from the path of a set’s tmp file

# File lib/card/set/i18n_scope.rb, line 16
def mod_name backtrace
  parts = path_parts backtrace
  mod_from_parts parts, path_set_index(parts)
end
scope(backtrace) click to toggle source

return scope for I18n

# File lib/card/set/i18n_scope.rb, line 7
def scope backtrace
  return "lib" unless (parts = path_parts backtrace)

  index = path_set_index parts
  mod = mod_from_parts parts, index
  mod || "lib"
end

Private Instance Methods

find_set_path(backtrace) click to toggle source

extract mod and set from tmp path @example

a tmp path looks like ~/mydeck/tmp/set/mod002-core/all/event.rb/
this method returns ["core", "all", "event"]

def tmp_set_path_parts backtrace

path_parts = find_tmp_set_path(backtrace).split(File::SEPARATOR)
res = path_parts[tmp_path_mod_index(path_parts)..-1]
res[0] = mod_name_from_tmp_dir res.first
res[-1] = res.last.split(".").first
res

end

def find_tmp_set_path backtrace

path = backtrace.find { |line| line.include? "tmp/set/" }
raise Error, "couldn't find set path in backtrace: #{backtrace}" unless path

path

end

# File lib/card/set/i18n_scope.rb, line 83
def find_set_path backtrace
  re = %r{(?<!card)/set/}
  backtrace.find { |line| line.match?(re) }.tap do |path|
    return nil unless path
  end
end
mod_from_parts(parts, set_index) click to toggle source
# File lib/card/set/i18n_scope.rb, line 28
def mod_from_parts parts, set_index
  if tmp_files?
    mod_without_tmp_prefix parts[set_index + 1]
  else
    mod_without_version_suffix parts[set_index - 1]
  end
end
mod_name_from_tmp_dir(dir) click to toggle source

# index of the mod part in the tmp path def tmp_path_mod_index parts

unless (set_index = parts.index("set")) &&
       parts.size >= set_index + 2
  raise Error, "not a valid set path: #{path}"
end

set_index + 1

end

# File lib/card/set/i18n_scope.rb, line 100
def mod_name_from_tmp_dir dir
  match = dir.match(/^mod\d+-(?<mod_name>.+)$/)
  match[:mod_name]
end
mod_without_tmp_prefix(mod) click to toggle source
# File lib/card/set/i18n_scope.rb, line 40
def mod_without_tmp_prefix mod
  mod.gsub(/^[^-]*-/, "")
end
mod_without_version_suffix(mod) click to toggle source
# File lib/card/set/i18n_scope.rb, line 36
def mod_without_version_suffix mod
  mod.gsub(/-[\d.]+$/, "")
end
path_parts(backtrace) click to toggle source
# File lib/card/set/i18n_scope.rb, line 44
def path_parts backtrace
  return unless (path = find_set_path backtrace)

  parts = path.split File::SEPARATOR
  parts[-1] = parts.last.split(".").first
  parts
end
path_set_index(parts) click to toggle source

index of the mod part in the path

# File lib/card/set/i18n_scope.rb, line 106
def path_set_index parts
  unless (set_index = parts.index("set")) && parts.size >= set_index + 2
    raise Error, "not a valid set path: #{path}"
  end

  set_index
end
set_from_parts(parts, index) click to toggle source
# File lib/card/set/i18n_scope.rb, line 23
def set_from_parts parts, index
  start_index = index + (tmp_files? ? 2 : 1)
  parts[start_index..-1].join "."
end