Class: TwitterCldr::Resources::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_cldr/resources/loader.rb

Constant Summary collapse

VALID_EXTS =
%w(.yml .dump).freeze

Instance Method Summary collapse

Instance Method Details

#absolute_resource_path(path) ⇒ Object


27
28
29
# 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) ⇒ Object


31
32
33
# 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) ⇒ Object


15
16
17
# 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) ⇒ Boolean

Returns:

  • (Boolean)

23
24
25
# 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) ⇒ Boolean

Returns:

  • (Boolean)

39
40
41
# 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_resourcesObject


71
72
73
74
75
76
# 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) ⇒ Object


57
58
59
60
61
62
# 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) ⇒ Object


64
65
66
67
68
69
# 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) ⇒ Object


49
50
51
52
53
54
55
# 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) ⇒ Boolean

Returns:

  • (Boolean)

19
20
21
# 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) ⇒ Object


78
79
80
81
82
# 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) ⇒ Boolean

Returns:

  • (Boolean)

35
36
37
# 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) ⇒ Object


43
44
45
46
47
# 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