class TwitterCldr::Resources::Loader

Constants

VALID_EXTS

Public Instance Methods

absolute_resource_path(path) click to toggle source
# File lib/twitter_cldr/resources/loader.rb, line 27
def absolute_resource_path(path)
  File.join(TwitterCldr::RESOURCES_DIR, path)
end
get_locale_resource(locale, resource_name) click to toggle source
# File lib/twitter_cldr/resources/loader.rb, line 31
def get_locale_resource(locale, resource_name)
  get_resource(*locale_resource_path(locale, resource_name))
end
get_resource(*path) click to toggle source
# File lib/twitter_cldr/resources/loader.rb, line 15
def get_resource(*path)
  resources_cache[resource_file_path(path)]
end
locale_resource_exists?(locale, resource_name) click to toggle source
# File lib/twitter_cldr/resources/loader.rb, line 23
def locale_resource_exists?(locale, resource_name)
  resource_exists?(*locale_resource_path(locale, resource_name))
end
locale_resource_loaded?(locale, resource_name) click to toggle source
# File lib/twitter_cldr/resources/loader.rb, line 39
def locale_resource_loaded?(locale, resource_name)
  resource_loaded?(*locale_resource_path(locale, resource_name))
end
preload_all_resources() click to toggle source
# File lib/twitter_cldr/resources/loader.rb, line 71
def preload_all_resources
  TwitterCldr.supported_locales.each do |locale|
    preload_resources_for_locale(locale, :all)
  end
  nil
end
preload_resource_for_locales(resource, *locales) click to toggle source
# File lib/twitter_cldr/resources/loader.rb, line 57
def preload_resource_for_locales(resource, *locales)
  locales.each do |locale|
    preload_resources_for_locale(locale, resource)
  end
  nil
end
preload_resources_for_all_locales(*resources) click to toggle source
# File lib/twitter_cldr/resources/loader.rb, line 64
def preload_resources_for_all_locales(*resources)
  TwitterCldr.supported_locales.each do |locale|
    preload_resources_for_locale(locale, *resources)
  end
  nil
end
preload_resources_for_locale(locale, *resources) click to toggle source
# File lib/twitter_cldr/resources/loader.rb, line 49
def preload_resources_for_locale(locale, *resources)
  if resources.size > 0
    resources = resource_types_for(locale) if resources.first == :all
    resources.each { |res| get_locale_resource(locale, res) }
  end
  nil
end
resource_exists?(*path) click to toggle source
# File lib/twitter_cldr/resources/loader.rb, line 19
def resource_exists?(*path)
  File.exist?(absolute_resource_path(resource_file_path(path)))
end
resource_file_path(path) click to toggle source
# File lib/twitter_cldr/resources/loader.rb, line 78
def resource_file_path(path)
  file = File.join(*path.map(&:to_s))
  file << '.yml' unless VALID_EXTS.include?(File.extname(file))
  file
end
resource_loaded?(*path) click to toggle source
# File lib/twitter_cldr/resources/loader.rb, line 35
def resource_loaded?(*path)
  resources_cache.include?(resource_file_path(path))
end
resource_types_for(locale) click to toggle source
# File lib/twitter_cldr/resources/loader.rb, line 43
def resource_types_for(locale)
  Dir.glob(File.join(RESOURCES_DIR, 'locales', locale.to_s, '*')).map do |file|
    File.basename(file).chomp(File.extname(file)).to_sym
  end
end

Private Instance Methods

custom_resource_exists?(custom_path) click to toggle source
# File lib/twitter_cldr/resources/loader.rb, line 126
def custom_resource_exists?(custom_path)
  File.exist?(
    File.join(TwitterCldr::RESOURCES_DIR, custom_path)
  )
end
load_marshalled_resource(path, _merge_custom = :unused) click to toggle source
# File lib/twitter_cldr/resources/loader.rb, line 118
def load_marshalled_resource(path, _merge_custom = :unused)
  Marshal.load(read_resource_file(path))
end
load_raw_resource(path, _merge_custom = :unused) click to toggle source
# File lib/twitter_cldr/resources/loader.rb, line 122
def load_raw_resource(path, _merge_custom = :unused)
  read_resource_file(path)
end
load_resource(path, merge_custom = true) click to toggle source
# File lib/twitter_cldr/resources/loader.rb, line 96
def load_resource(path, merge_custom = true)
  case File.extname(path)
    when '.yml'
      load_yaml_resource(path, merge_custom)
    when '.dump'
      load_marshalled_resource(path, merge_custom)
    else
      load_raw_resource(path, merge_custom)
  end
end
load_yaml_resource(path, merge_custom = true) click to toggle source
# File lib/twitter_cldr/resources/loader.rb, line 107
def load_yaml_resource(path, merge_custom = true)
  base = YAML.load(read_resource_file(path))
  custom_path = File.join("custom", path)

  if merge_custom && custom_resource_exists?(custom_path) && !TwitterCldr.disable_custom_locale_resources
    TwitterCldr::Utils.deep_merge!(base, load_resource(custom_path, false))
  end

  base
end
locale_resource_path(locale, resource_name) click to toggle source
# File lib/twitter_cldr/resources/loader.rb, line 86
def locale_resource_path(locale, resource_name)
  [:locales, TwitterCldr.convert_locale(locale), resource_name]
end
read_resource_file(path) click to toggle source
# File lib/twitter_cldr/resources/loader.rb, line 132
def read_resource_file(path)
  file_path = absolute_resource_path(path)

  if File.file?(file_path)
    File.open(file_path, "r:UTF-8", &:read)
  else
    raise ResourceLoadError,
      "Resource '#{path}' not found."
  end
end
resources_cache() click to toggle source
# File lib/twitter_cldr/resources/loader.rb, line 90
def resources_cache
  @resources_cache ||= Hash.new do |hash, path|
    hash[path] = load_resource(path)
  end
end