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



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

def load_yaml(yaml, permitted_classes: [])
  if RUBY_VERSION >= '2.6.0'
    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



38
39
40
# File 'lib/twitter_cldr/resources/loader.rb', line 38

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

#get_locale_resource(locale, resource_name) ⇒ Object



42
43
44
# File 'lib/twitter_cldr/resources/loader.rb', line 42

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

#get_resource(*path) ⇒ Object



26
27
28
# File 'lib/twitter_cldr/resources/loader.rb', line 26

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

#locale_resource_exists?(locale, resource_name) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/twitter_cldr/resources/loader.rb', line 34

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)


50
51
52
# File 'lib/twitter_cldr/resources/loader.rb', line 50

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

#preload_all_resourcesObject



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

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



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

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



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

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



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

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)


30
31
32
# File 'lib/twitter_cldr/resources/loader.rb', line 30

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

#resource_file_path(path) ⇒ Object



89
90
91
92
93
# File 'lib/twitter_cldr/resources/loader.rb', line 89

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)


46
47
48
# File 'lib/twitter_cldr/resources/loader.rb', line 46

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

#resource_types_for(locale) ⇒ Object



54
55
56
57
58
# File 'lib/twitter_cldr/resources/loader.rb', line 54

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