Improve method privacy

This commit is contained in:
Adam Hoyle 2021-08-24 21:27:43 +01:00
parent 94f9c2b86f
commit 0a3b052cf6

View file

@ -7,49 +7,53 @@ class Communities
@local_chapters[locale] = local_chapter_for(locale) @local_chapters[locale] = local_chapter_for(locale)
end end
def self.local_chapter_for(locale) class << self
@local_chapters_index = load_local_chapters protected
locale_dict = locale_dict_for(locale)
localised_chapters = []
@local_chapters_index.each do |chapter|
id = chapter[:id]
name = locale_dict.dig(id, "name") || chapter[:name]
url = chapter[:url]
localised_chapters.push({ :id => id, :name => name, :url => url })
end
localised_chapters
end
def self.load_local_chapters def local_chapter_for(locale)
json_file = File.expand_path("node_modules/osm-community-index/dist/resources.json", Dir.pwd) @local_chapters_index = load_local_chapters
community_index = JSON.parse(File.read(json_file)) locale_dict = locale_dict_for(locale)
local_chapters = [] localised_chapters = []
community_index["resources"].each do |id, resource| @local_chapters_index.each do |chapter|
resource.each do |key, value| id = chapter[:id]
next unless key == "type" && value == "osm-lc" && id != "OSMF" name = locale_dict.dig(id, "name") || chapter[:name]
url = chapter[:url]
strings = resource["strings"] localised_chapters.push({ :id => id, :name => name, :url => url })
chapter_name = strings["community"] || strings["name"]
url = strings["url"]
local_chapters.push({ :id => id, :name => chapter_name, :url => url })
end end
localised_chapters
end end
local_chapters
end
def self.locale_dict_for(locale_in) def load_local_chapters
locale = locale_in.to_s.tr("-", "_") json_file = File.expand_path("node_modules/osm-community-index/dist/resources.json", Dir.pwd)
full_local_path = File.expand_path("node_modules/osm-community-index/i18n/#{locale}.yaml", Dir.pwd) community_index = JSON.parse(File.read(json_file))
locale_dict = {} local_chapters = []
if File.exist?(full_local_path) community_index["resources"].each do |id, resource|
locale_dict = YAML.safe_load(File.read(full_local_path))[locale] resource.each do |key, value|
else next unless key == "type" && value == "osm-lc" && id != "OSMF"
shortened_locale = locale.split("_").first
if shortened_locale != locale strings = resource["strings"]
shortened_local_path = File.expand_path("node_modules/osm-community-index/i18n/#{shortened_locale}.yaml", Dir.pwd) chapter_name = strings["community"] || strings["name"]
locale_dict = YAML.safe_load(File.read(shortened_local_path))[shortened_locale] if File.exist?(shortened_local_path) url = strings["url"]
local_chapters.push({ :id => id, :name => chapter_name, :url => url })
end
end end
local_chapters
end
def locale_dict_for(locale_in)
locale = locale_in.to_s.tr("-", "_")
full_local_path = File.expand_path("node_modules/osm-community-index/i18n/#{locale}.yaml", Dir.pwd)
locale_dict = {}
if File.exist?(full_local_path)
locale_dict = YAML.safe_load(File.read(full_local_path))[locale]
else
shortened_locale = locale.split("_").first
if shortened_locale != locale
shortened_local_path = File.expand_path("node_modules/osm-community-index/i18n/#{shortened_locale}.yaml", Dir.pwd)
locale_dict = YAML.safe_load(File.read(shortened_local_path))[shortened_locale] if File.exist?(shortened_local_path)
end
end
locale_dict
end end
locale_dict
end end
end end