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
PERMITTED_YAML_CLASSES =
[Range, Regexp, Symbol, Time].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_yaml(yaml, permitted_classes: []) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/twitter_cldr/resources/loader.rb', line 16

def load_yaml(yaml, permitted_classes: [])
  if Psych::VERSION >= '4'
    YAML.safe_load(yaml, permitted_classes: permitted_classes)
  else
    YAML.safe_load(yaml, permitted_classes)
  end
end

Instance Method Details

#absolute_resource_path(path) ⇒ Object



40
41
42
# File 'lib/twitter_cldr/resources/loader.rb', line 40

def absolute_resource_path(path)
  File.join(TwitterCldr::RESOURCES_DIR, path)
end

#get_locale_resource(locale, resource_name) ⇒ Object



44
45
46
# File 'lib/twitter_cldr/resources/loader.rb', line 44

def get_locale_resource(locale, resource_name)
  get_resource(*locale_resource_path(locale, resource_name))
end

#get_resource(*path) ⇒ Object



28
29
30
# File 'lib/twitter_cldr/resources/loader.rb', line 28

def get_resource(*path)
  resources_cache[resource_file_path(path)]
end

#locale_resource_exists?(locale, resource_name) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/twitter_cldr/resources/loader.rb', line 36

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)


52
53
54
# File 'lib/twitter_cldr/resources/loader.rb', line 52

def locale_resource_loaded?(locale, resource_name)
  resource_loaded?(*locale_resource_path(locale, resource_name))
end

#preload_all_resourcesObject



84
85
86
87
88
89
# File 'lib/twitter_cldr/resources/loader.rb', line 84

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



70
71
72
73
74
75
# File 'lib/twitter_cldr/resources/loader.rb', line 70

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



77
78
79
80
81
82
# File 'lib/twitter_cldr/resources/loader.rb', line 77

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



62
63
64
65
66
67
68
# File 'lib/twitter_cldr/resources/loader.rb', line 62

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)


32
33
34
# File 'lib/twitter_cldr/resources/loader.rb', line 32

def resource_exists?(*path)
  File.exist?(absolute_resource_path(resource_file_path(path)))
end

#resource_file_path(path) ⇒ Object



91
92
93
94
95
# File 'lib/twitter_cldr/resources/loader.rb', line 91

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)


48
49
50
# File 'lib/twitter_cldr/resources/loader.rb', line 48

def resource_loaded?(*path)
  resources_cache.include?(resource_file_path(path))
end

#resource_types_for(locale) ⇒ Object



56
57
58
59
60
# File 'lib/twitter_cldr/resources/loader.rb', line 56

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