Skip to content

Commit

Permalink
🚀 Load country data on compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianSzturo committed Feb 2, 2015
1 parent 370da34 commit 0ff3f8c
Showing 1 changed file with 35 additions and 26 deletions.
61 changes: 35 additions & 26 deletions lib/countries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,41 @@ defmodule Countries do
country[attribute] == value_as_char_list
end)
end

defp countries do
# Lets load all country data from our yaml files
codes = List.first(:yamerl.decode_file(data_path "countries.yaml"))
country_data =
Enum.reduce(codes, [], fn (code, countries) ->
countries ++ :yamerl.decode_file(data_path "countries/#{code}.yaml")
end)

# :yamerl returns a really terrible data structure
# [[{'name', 'Germany'}, {'code', 'DE'}], [{'nam:e', 'Austria'}, {'code', 'AT'}]]
# We need to transform that to maps:
# [%{name: 'Germany', code: "DE"}, %{name: 'Austria', code: "AT"}]
countries =
Enum.reduce(country_data, [], fn (country_data, countries) ->
country =
Enum.reduce(country_data, %Country{}, fn({key, value}, country) ->
key_as_atom = String.to_atom(to_string key)
Map.put(country, key_as_atom, value)
end)

List.insert_at(countries, 0, country)
end)
end

defp data_path(path) do

#-- Load countries from yaml files once on compile time ---

# Ensure :yamerl is running
Application.ensure_all_started(:yamerl)

data_path = fn(path) ->
Path.join("data", path) |> Path.expand(__DIR__)
end

# Lets load all country data from our yaml files
codes = List.first(:yamerl.decode_file data_path.("/countries.yaml"))
country_data =
Enum.reduce(codes, [], fn (code, countries) ->
countries ++ :yamerl.decode_file data_path.("countries/#{code}.yaml")
end)

# :yamerl returns a really terrible data structure
# [[{'name', 'Germany'}, {'code', 'DE'}], [{'nam:e', 'Austria'}, {'code', 'AT'}]]
# We need to transform that to maps:
# [%{name: 'Germany', code: "DE"}, %{name: 'Austria', code: "AT"}]
countries =
Enum.reduce(country_data, [], fn (country_data, countries) ->
country =
Enum.reduce(country_data, %Country{}, fn({attribute, value}, country) ->
attribute_as_atom = String.to_atom(to_string attribute)
Map.put(country, attribute_as_atom, value)
end)

List.insert_at(countries, 0, country)
end)

defp countries do
# Maps and Structs need to be escaped before unquoting
unquote Macro.escape(countries)
end

end

0 comments on commit 0ff3f8c

Please sign in to comment.