Skip to content

Commit

Permalink
Accepting string locale codes on Holidefs functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinst committed Feb 23, 2018
1 parent 3aee8cd commit 878a722
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

## Unreleased
## 0.2.1

### Added

- Now `Holidefs` functions accepts string locale codes too

## 0.2.0

Expand Down
13 changes: 7 additions & 6 deletions lib/holidefs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Holidefs do
alias Holidefs.Options

@type error_reasons :: :no_def | :invalid_date
@type locale_code :: atom | binary

@locales %{
at: "Austria",
Expand Down Expand Up @@ -83,8 +84,8 @@ defmodule Holidefs do
If succeed returns a `{:ok, holidays}` tuple, otherwise
returns a `{:error, reason}` tuple.
"""
@spec on(atom, Date.t()) :: {:ok, [Holidefs.Holiday.t()]} | {:error, String.t()}
@spec on(atom, Date.t(), Holidefs.Options.t()) ::
@spec on(locale_code, Date.t()) :: {:ok, [Holidefs.Holiday.t()]} | {:error, String.t()}
@spec on(locale_code, Date.t(), Holidefs.Options.t()) ::
{:ok, [Holidefs.Holiday.t()]} | {:error, error_reasons}
def on(locale, date, opts \\ []) do
locale
Expand All @@ -98,8 +99,8 @@ defmodule Holidefs do
If succeed returns a `{:ok, holidays}` tuple, otherwise
returns a `{:error, reason}` tuple
"""
@spec year(atom, integer) :: {:ok, [Holidefs.Holiday.t()]} | {:error, String.t()}
@spec year(atom, integer, Holidefs.Options.t()) ::
@spec year(locale_code, integer) :: {:ok, [Holidefs.Holiday.t()]} | {:error, String.t()}
@spec year(locale_code, integer, Holidefs.Options.t()) ::
{:ok, [Holidefs.Holiday.t()]} | {:error, error_reasons}
def year(locale, year, opts \\ []) do
locale
Expand All @@ -119,7 +120,7 @@ defmodule Holidefs do
@spec all_year_holidays(
[Holidefs.Definition.Rule.t()],
integer,
atom,
locale_code,
Holidefs.Options.t() | list
) :: [Holidefs.Holiday.t()]
defp all_year_holidays(
Expand All @@ -145,7 +146,7 @@ defmodule Holidefs do
If succeed returns a `{:ok, holidays}` tuple, otherwise
returns a `{:error, reason}` tuple
"""
@spec between(atom, Date.t(), Date.t(), Holidefs.Options.t()) ::
@spec between(locale_code, Date.t(), Date.t(), Holidefs.Options.t()) ::
{:ok, [Holidefs.Holiday.t()]} | {:error, error_reasons}
def between(locale, start, finish, opts \\ []) do
locale
Expand Down
22 changes: 20 additions & 2 deletions lib/holidefs/definition/store.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,31 @@ defmodule Holidefs.Definition.Store do
@doc """
Returns the definitions for the given locale
"""
@spec get_definition(atom) :: Holidefs.Definition.t() | nil
@spec get_definition(Holidefs.locale_code()) :: Holidefs.Definition.t() | nil
def get_definition(locale) do
Agent.get(__MODULE__, fn %{definitions: definitions} ->
Enum.find(definitions, &(&1.code == locale))
Enum.find(definitions, &(&1.code == get_locale_atom(locale)))
end)
end

defp get_locale_atom(locale) when is_binary(locale) do
if locale in locale_keys() do
String.to_existing_atom(locale)
else
locale
end
end

defp get_locale_atom(locale) when is_atom(locale) do
locale
end

defp locale_keys do
Holidefs.locales()
|> Map.keys()
|> Enum.map(&Atom.to_string/1)
end

defp load_all do
%{
definitions:
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Holidefs.Mixfile do
def project do
[
app: :holidefs,
version: "0.2.0",
version: "0.2.1",
elixir: "~> 1.5",
description: "Definition-based national holidays",
source_url: @github_url,
Expand Down
2 changes: 2 additions & 0 deletions test/holidefs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ defmodule HolidefsTest do
raw_date: ~D[2017-11-15]
}
]

assert {:ok, ^holidays} = Holidefs.between("br", ~D[2017-11-03], ~D[2017-12-24])
end

require Logger
Expand Down

0 comments on commit 878a722

Please sign in to comment.