diff --git a/lib/dateformat/dateformat.ex b/lib/format/datetime/dateformat.ex similarity index 94% rename from lib/dateformat/dateformat.ex rename to lib/format/datetime/dateformat.ex index be8f8b64..990b55e0 100644 --- a/lib/dateformat/dateformat.ex +++ b/lib/format/datetime/dateformat.ex @@ -1,4 +1,4 @@ -defmodule Timex.DateFormat do +defmodule Timex.Format.DateTime.DateFormat do @moduledoc """ Date formatting and parsing. @@ -10,10 +10,10 @@ defmodule Timex.DateFormat do module. One can also implement custom formatters for use with this module. """ alias Timex.DateTime - alias Timex.DateFormat.Formatters.Formatter - alias Timex.DateFormat.Formatters.StrftimeFormatter - alias Timex.Parsers.DateFormat.Parser - alias Timex.Parsers.DateFormat.StrftimeParser + alias Timex.Format.DateTime.Formatter + alias Timex.Format.DateTime.Formatters.StrftimeFormatter + alias Timex.Parse.DateTime.Parser + alias Timex.Parse.DateTime.Parsers.StrftimeParser @doc """ Converts date values to strings according to the given template (aka format string). diff --git a/lib/parsers/dateformat/directive.ex b/lib/format/datetime/directive.ex similarity index 98% rename from lib/parsers/dateformat/directive.ex rename to lib/format/datetime/directive.ex index ebf5b40f..c63477a6 100644 --- a/lib/parsers/dateformat/directive.ex +++ b/lib/format/datetime/directive.ex @@ -1,12 +1,12 @@ -defmodule Timex.Parsers.DateFormat.Directive do +defmodule Timex.Format.DateTime.Directive do @moduledoc """ This module defines parsing directives for all date/time tokens timex knows about. It is composed of a Directive struct, containing the rules for parsing a given token, and a `get/1` function, which fetches a directive for a given token value, i.e. `:year4`. """ - alias Timex.DateFormat.Formats - alias Timex.Parsers.DateFormat.Directive + alias Timex.Format.DateTime.Formats + alias Timex.Format.DateTime.Directive require Formats require Formats.Regex diff --git a/lib/dateformat/formats.ex b/lib/format/datetime/formats.ex similarity index 97% rename from lib/dateformat/formats.ex rename to lib/format/datetime/formats.ex index 1e3b013f..cbec13ac 100644 --- a/lib/dateformat/formats.ex +++ b/lib/format/datetime/formats.ex @@ -1,4 +1,4 @@ -defmodule Timex.DateFormat.Formats do +defmodule Timex.Format.DateTime.Formats do @moduledoc """ This module defines all known (by timex) common date/time formats, in macro form. @@ -10,8 +10,8 @@ defmodule Timex.DateFormat.Formats do These formats are consumed by the datetime string parsers, by first tokenizing the chosen format, then parsing the datetime string using those tokens. """ - alias Timex.Parsers.DateFormat.Tokenizers.Default - alias Timex.Parsers.DateFormat.Tokenizers.Strftime + alias Timex.Format.DateTime.Tokenizers.Default + alias Timex.Format.DateTime.Tokenizers.Strftime # For now, all preformatted strings will be tokenized using the Default tokenizer. @tokenizer {:tokenizer, Default} diff --git a/lib/dateformat/formatter.ex b/lib/format/datetime/formatter.ex similarity index 95% rename from lib/dateformat/formatter.ex rename to lib/format/datetime/formatter.ex index 0f8f141e..92d10374 100644 --- a/lib/dateformat/formatter.ex +++ b/lib/format/datetime/formatter.ex @@ -1,12 +1,12 @@ -defmodule Timex.DateFormat.Formatters.Formatter do +defmodule Timex.Format.DateTime.Formatter do use Behaviour alias Timex.Date alias Timex.Time alias Timex.DateTime - alias Timex.DateFormat.FormatError - alias Timex.DateFormat.Formatters.DefaultFormatter - alias Timex.Parsers.DateFormat.Directive + alias Timex.Format.FormatError + alias Timex.Format.DateTime.Formatters.DefaultFormatter + alias Timex.Format.DateTime.Directive defcallback tokenize(format_string :: String.t) :: {:ok, [%Directive{}]} | {:error, term} defcallback format(date :: %DateTime{}, format_string :: String.t) :: {:ok, String.t} | {:error, term} @@ -15,9 +15,9 @@ defmodule Timex.DateFormat.Formatters.Formatter do @doc false defmacro __using__(_opts) do quote do - @behaviour Timex.DateFormat.Formatters.Formatter + @behaviour Timex.Format.DateTime.Formatter - import Timex.DateFormat.Formatters.Formatter, only: [format_token: 2] + import Timex.Format.DateTime.Formatter, only: [format_token: 2] end end diff --git a/lib/dateformat/formatters/default.ex b/lib/format/datetime/formatters/default.ex similarity index 97% rename from lib/dateformat/formatters/default.ex rename to lib/format/datetime/formatters/default.ex index 86fb4971..6f111f53 100644 --- a/lib/dateformat/formatters/default.ex +++ b/lib/format/datetime/formatters/default.ex @@ -1,4 +1,4 @@ -defmodule Timex.DateFormat.Formatters.DefaultFormatter do +defmodule Timex.Format.DateTime.Formatters.DefaultFormatter do @moduledoc """ Date formatting language used by default by the `DateFormat` module. @@ -107,13 +107,13 @@ defmodule Timex.DateFormat.Formatters.DefaultFormatter do * `{kitchen}` - e.g. `3:25PM` """ - use Timex.DateFormat.Formatters.Formatter + use Timex.Format.DateTime.Formatter alias Timex.DateTime alias Timex.Timezone - alias Timex.DateFormat.FormatError - alias Timex.Parsers.DateFormat.Directive - alias Timex.Parsers.DateFormat.Tokenizers.Default, as: Tokenizer + alias Timex.Format.FormatError + alias Timex.Format.DateTime.Directive + alias Timex.Format.DateTime.Tokenizers.Default, as: Tokenizer @spec tokenize(String.t) :: {:ok, [%Directive{}]} | {:error, term} defdelegate tokenize(format_string), to: Tokenizer diff --git a/lib/dateformat/formatters/strftime.ex b/lib/format/datetime/formatters/strftime.ex similarity index 93% rename from lib/dateformat/formatters/strftime.ex rename to lib/format/datetime/formatters/strftime.ex index 232b6153..1c662031 100644 --- a/lib/dateformat/formatters/strftime.ex +++ b/lib/format/datetime/formatters/strftime.ex @@ -1,4 +1,4 @@ -defmodule Timex.DateFormat.Formatters.StrftimeFormatter do +defmodule Timex.Format.DateTime.Formatters.StrftimeFormatter do @moduledoc """ Date formatting language defined by the `strftime` function from the Standard C Library. @@ -95,12 +95,12 @@ defmodule Timex.DateFormat.Formatters.StrftimeFormatter do * `%v` - same as `%e-%b-%Y` """ - use Timex.DateFormat.Formatters.Formatter + use Timex.Format.DateTime.Formatter alias Timex.DateTime - alias Timex.Parsers.DateFormat.Directive - alias Timex.DateFormat.Formatters.DefaultFormatter - alias Timex.Parsers.DateFormat.Tokenizers.Strftime, as: Tokenizer + alias Timex.Format.DateTime.Directive + alias Timex.Format.DateTime.Formatters.DefaultFormatter + alias Timex.Format.DateTime.Tokenizers.Strftime, as: Tokenizer @spec tokenize(String.t) :: {:ok, [%Directive{}]} | {:error, term} defdelegate tokenize(format_string), to: Tokenizer @@ -117,4 +117,4 @@ defmodule Timex.DateFormat.Formatters.StrftimeFormatter do def format(%DateTime{} = date, format_string) do DefaultFormatter.format(date, format_string, Tokenizer) end -end \ No newline at end of file +end diff --git a/lib/parsers/dateformat/tokenizers/default.ex b/lib/format/datetime/tokenizers/default.ex similarity index 96% rename from lib/parsers/dateformat/tokenizers/default.ex rename to lib/format/datetime/tokenizers/default.ex index 7cb11850..ab237889 100644 --- a/lib/parsers/dateformat/tokenizers/default.ex +++ b/lib/format/datetime/tokenizers/default.ex @@ -1,10 +1,10 @@ -defmodule Timex.Parsers.DateFormat.Tokenizers.Default do +defmodule Timex.Format.DateTime.Tokenizers.Default do @moduledoc """ Responsible for tokenizing date/time format strings which use the Default formatter. """ - alias Timex.Parsers.DateFormat.ParserState, as: State - alias Timex.Parsers.DateFormat.Directive, as: Directive + alias Timex.Parse.DateTime.ParserState, as: State + alias Timex.Format.DateTime.Directive, as: Directive # These are all the default formatter's directives @directives [ @@ -100,7 +100,7 @@ defmodule Timex.Parsers.DateFormat.Tokenizers.Default do case get_directive(token) do :invalid -> {:error, "Invalid token beginning at column #{state.start_index}!"} {_, %Directive{} = directive} -> - state = %{state | + state = %{state | :col => state.col + 1, :padding => 0, :token => "", @@ -133,4 +133,4 @@ defmodule Timex.Parsers.DateFormat.Tokenizers.Default do defp get_directive(dir) do List.keyfind(@directives, dir, 0) || :invalid end -end \ No newline at end of file +end diff --git a/lib/parsers/dateformat/tokenizers/strftime.ex b/lib/format/datetime/tokenizers/strftime.ex similarity index 97% rename from lib/parsers/dateformat/tokenizers/strftime.ex rename to lib/format/datetime/tokenizers/strftime.ex index 12f70600..79ef0f8c 100644 --- a/lib/parsers/dateformat/tokenizers/strftime.ex +++ b/lib/format/datetime/tokenizers/strftime.ex @@ -1,10 +1,10 @@ -defmodule Timex.Parsers.DateFormat.Tokenizers.Strftime do +defmodule Timex.Format.DateTime.Tokenizers.Strftime do @moduledoc """ Responsible for tokenizing date/time format strings which use the strftime formatter. """ - alias Timex.Parsers.DateFormat.ParserState, as: State - alias Timex.Parsers.DateFormat.Directive, as: Directive + alias Timex.Parse.DateTime.ParserState, as: State + alias Timex.Format.DateTime.Directive, as: Directive # These are all the strftime formatter's directives @directives [ @@ -177,4 +177,4 @@ defmodule Timex.Parsers.DateFormat.Tokenizers.Strftime do defp non_empty(""), do: false defp non_empty(_), do: true -end \ No newline at end of file +end diff --git a/lib/dateformat/format_error.ex b/lib/format/format_error.ex similarity index 71% rename from lib/dateformat/format_error.ex rename to lib/format/format_error.ex index dc3fd1f6..adecda9b 100644 --- a/lib/dateformat/format_error.ex +++ b/lib/format/format_error.ex @@ -1,12 +1,12 @@ -defmodule Timex.DateFormat.FormatError do +defmodule Timex.Format.FormatError do @moduledoc """ Used for errors encountered during date formatting. """ - alias Timex.DateFormat.FormatError + alias Timex.Format.FormatError defexception message: "Invalid format!" def exception([message: message]) do %FormatError{message: message} end -end \ No newline at end of file +end diff --git a/lib/time/time_formatter.ex b/lib/format/time/time_formatter.ex similarity index 98% rename from lib/time/time_formatter.ex rename to lib/format/time/time_formatter.ex index 5fe153d0..7d560095 100644 --- a/lib/time/time_formatter.ex +++ b/lib/format/time/time_formatter.ex @@ -1,4 +1,4 @@ -defmodule Timex.TimeFormatter do +defmodule Timex.Format.Time.TimeFormatter do @moduledoc """ Handles formatting timestamp values as human readable strings. For formatting timestamps as points in time rather than intervals, diff --git a/lib/parsers/dateformat/parser.ex b/lib/parse/datetime/parser.ex similarity index 96% rename from lib/parsers/dateformat/parser.ex rename to lib/parse/datetime/parser.ex index daf72b51..caa014e1 100644 --- a/lib/parsers/dateformat/parser.ex +++ b/lib/parse/datetime/parser.ex @@ -1,4 +1,4 @@ -defmodule Timex.Parsers.DateFormat.Parser do +defmodule Timex.Parse.DateTime.Parser do @moduledoc """ This is the base plugin behavior for all Timex date/time string parsers. """ @@ -8,8 +8,10 @@ defmodule Timex.Parsers.DateFormat.Parser do alias Timex.Time alias Timex.DateTime alias Timex.Timezone - alias Timex.Parsers.ParseError - alias Timex.Parsers.DateFormat.Directive + alias Timex.Parse.ParseError + alias Timex.Format.DateTime.Directive + alias Timex.Parse.DateTime.Parsers.DefaultParser + alias Timex.Parse.DateTime.Parser # Tokenizes the format string defcallback tokenize(format_string :: binary) :: [%Directive{}] | {:error, term} @@ -21,9 +23,9 @@ defmodule Timex.Parsers.DateFormat.Parser do @doc false defmacro __using__(_opts) do quote do - @behaviour Timex.Parsers.DateFormat.Parser + @behaviour Timex.Parse.DateTime.Parser - import Timex.Parsers.DateFormat.Parser + import Timex.Parse.DateTime.Parser @whitespace [32, ?\t, ?\n, ?\r] @numerics ?0..?9 |> Enum.to_list @@ -48,7 +50,7 @@ defmodule Timex.Parsers.DateFormat.Parser do @spec parse(binary, binary) :: {:ok, %DateTime{}} | {:error, term} def parse(date_string, format_string) when is_binary(date_string) and is_binary(format_string), - do: parse(date_string, format_string, Timex.Parsers.DateFormat.DefaultParser) + do: parse(date_string, format_string, DefaultParser) @doc """ Parses a date/time string using the provided parser. Must implement the @@ -60,7 +62,7 @@ defmodule Timex.Parsers.DateFormat.Parser do %Date{year: 2014, month: 7, day: 29, hour: 0, minute: 20, second: 41, ms: 196, tz: %Timezone{name: "CST"}} """ - @spec parse(binary, binary, Timex.Parsers.DateFormat.Parser) :: {:ok, %DateTime{}} | {:error, term} + @spec parse(binary, binary, Parser) :: {:ok, %DateTime{}} | {:error, term} def parse(date_string, format_string, parser) when is_binary(date_string) and is_binary(format_string) do @@ -83,8 +85,8 @@ defmodule Timex.Parsers.DateFormat.Parser do @doc """ Same as `parse/2` and `parse/3`, but raises on error. """ - @spec parse!(String.t, String.t, Timex.Parsers.DateFormat.Parser | nil) :: %DateTime{} | no_return - def parse!(date_string, format_string, parser \\ Timex.Parsers.DateFormat.DefaultParser) + @spec parse!(String.t, String.t, Parser | nil) :: %DateTime{} | no_return + def parse!(date_string, format_string, parser \\ DefaultParser) when is_binary(date_string) and is_binary(format_string) do case parse(date_string, format_string, parser) do diff --git a/lib/parsers/dateformat/parser_state.ex b/lib/parse/datetime/parser_state.ex similarity index 88% rename from lib/parsers/dateformat/parser_state.ex rename to lib/parse/datetime/parser_state.ex index a6c0dbce..f6b33d2b 100644 --- a/lib/parsers/dateformat/parser_state.ex +++ b/lib/parse/datetime/parser_state.ex @@ -1,4 +1,4 @@ -defmodule Timex.Parsers.DateFormat.ParserState do +defmodule Timex.Parse.DateTime.ParserState do @derive Access defstruct col: 0, # The current column number start_index: 0, # The last index of a starting token @@ -6,4 +6,4 @@ defmodule Timex.Parsers.DateFormat.ParserState do pad_type: :zero, # The character to use for padding, :zero or :space token: "", # The current state of the parsed token tokens: [] # A keyword list of tokens -end \ No newline at end of file +end diff --git a/lib/parsers/dateformat/default.ex b/lib/parse/datetime/parsers/default.ex similarity index 96% rename from lib/parsers/dateformat/default.ex rename to lib/parse/datetime/parsers/default.ex index 57cfe02c..86661ea2 100644 --- a/lib/parsers/dateformat/default.ex +++ b/lib/parse/datetime/parsers/default.ex @@ -1,20 +1,20 @@ -defmodule Timex.Parsers.DateFormat.DefaultParser do +defmodule Timex.Parse.DateTime.Parsers.DefaultParser do @moduledoc """ This module is responsible for parsing date strings using the default timex formatting syntax. See `Timex.DateFormat.Formatters.DefaultFormatter` for more info. """ - use Timex.Parsers.DateFormat.Parser + use Timex.Parse.DateTime.Parser - alias Timex.Parsers.DateFormat.Directive + alias Timex.Format.DateTime.Directive alias Timex.DateTime alias Timex.TimezoneInfo @doc """ The tokenizer used by this parser. """ - defdelegate tokenize(format_string), to: Timex.Parsers.DateFormat.Tokenizers.Default + defdelegate tokenize(format_string), to: Timex.Format.DateTime.Tokenizers.Default @doc """ Constructs a DateTime from the parsed tokens @@ -36,7 +36,7 @@ defmodule Timex.Parsers.DateFormat.DefaultParser do end defp apply_directives([], %DateTime{} = date), do: {:ok, date} defp apply_directives([{token, value}|tokens], %DateTime{} = date) do - case Timex.Parsers.DateFormat.Parser.update_date(date, token, value) do + case Timex.Parse.DateTime.Parser.update_date(date, token, value) do {:error, _} = error -> error updated -> apply_directives(tokens, updated) end diff --git a/lib/parsers/dateformat/strftime.ex b/lib/parse/datetime/parsers/strftime.ex similarity index 58% rename from lib/parsers/dateformat/strftime.ex rename to lib/parse/datetime/parsers/strftime.ex index b4536edd..9af0619c 100644 --- a/lib/parsers/dateformat/strftime.ex +++ b/lib/parse/datetime/parsers/strftime.ex @@ -1,24 +1,24 @@ -defmodule Timex.Parsers.DateFormat.StrftimeParser do +defmodule Timex.Parse.DateTime.Parsers.StrftimeParser do @moduledoc """ This module is responsible for parsing date strings using the strftime formatting syntax. See `Timex.DateFormat.Formatters.StrftimeFormatter` for more info. """ - use Timex.Parsers.DateFormat.Parser + use Timex.Parse.DateTime.Parser @doc """ The tokenizer used by this parser. """ - defdelegate tokenize(format_string), to: Timex.Parsers.DateFormat.Tokenizers.Strftime + defdelegate tokenize(format_string), to: Timex.Format.DateTime.Tokenizers.Strftime @doc """ Extracts the value for a given directive. """ - defdelegate parse_directive(date_string, directive), to: Timex.Parsers.DateFormat.DefaultParser + defdelegate parse_directive(date_string, directive), to: Timex.Parse.DateTime.Parsers.DefaultParser @doc """ Constructs a DateTime from the parsed tokens """ - defdelegate apply_directives(tokens), to: Timex.Parsers.DateFormat.DefaultParser -end \ No newline at end of file + defdelegate apply_directives(tokens), to: Timex.Parse.DateTime.Parsers.DefaultParser +end diff --git a/lib/parsers/parse_error.ex b/lib/parse/parse_error.ex similarity index 71% rename from lib/parsers/parse_error.ex rename to lib/parse/parse_error.ex index bfd75839..55885d32 100644 --- a/lib/parsers/parse_error.ex +++ b/lib/parse/parse_error.ex @@ -1,12 +1,12 @@ -defmodule Timex.Parsers.ParseError do +defmodule Timex.Parse.ParseError do @moduledoc """ Used for errors encountered during date parsing. """ - alias Timex.Parsers.ParseError + alias Timex.Parse.ParseError defexception message: "Invalid input!" def exception(message) do %ParseError{message: message} end -end \ No newline at end of file +end diff --git a/lib/parsers/zoneinfo/zoneinfo.ex b/lib/parse/zoneinfo/parser.ex similarity index 96% rename from lib/parsers/zoneinfo/zoneinfo.ex rename to lib/parse/zoneinfo/parser.ex index ab74f2db..9fbf06e5 100644 --- a/lib/parsers/zoneinfo/zoneinfo.ex +++ b/lib/parse/zoneinfo/parser.ex @@ -1,4 +1,4 @@ -defmodule Timex.Parsers.ZoneInfo do +defmodule Timex.Parse.ZoneInfo.Parser do @moduledoc """ This module is responsible for parsing binary zoneinfo files, such as those found in /usr/local/zoneinfo. @@ -9,10 +9,10 @@ defmodule Timex.Parsers.ZoneInfo do defmodule Zone do @moduledoc """ Represents the data retreived from a binary tzfile. - For details on the tzfile format, see: + For details on the tzfile format, see: http://www.cstdbill.com/tzdb/tzfile-format.html - http://linux.about.com/library/cmd/blcmdl5_tzfile.htm + http://linux.about.com/library/cmd/blcmdl5_tzfile.htm https://github.com/eggert/tz/blob/master/tzfile.h """ @derive Access @@ -25,7 +25,7 @@ defmodule Timex.Parsers.ZoneInfo do defmodule Header do @moduledoc false - + # Six big-endian 4-8 byte integers @derive Access defstruct utc_count: 0, # count of UTC/local indicators @@ -130,7 +130,7 @@ defmodule Timex.Parsers.ZoneInfo do txs = indices |> Enum.map(&(Enum.at(txinfos, &1))) |> Enum.zip(tzfile.transitions) - |> Enum.map(fn {info, time} -> + |> Enum.map(fn {info, time} -> put_in(info, [:starts_at], time) end) do_parse_abbreviations(rest, header, %{tzfile | :transitions => txs}) @@ -139,7 +139,7 @@ defmodule Timex.Parsers.ZoneInfo do defp do_parse_abbreviations(data, %Header{abbrev_length: len} = header, tzfile) do {abbrevs, rest} = parse_array(data, len, &parse_char/1) txinfos = Enum.map(tzfile.transitions, fn tx -> - abbrev = abbrevs + abbrev = abbrevs |> Enum.drop(tx.abbrev_index) |> Enum.take_while(fn c -> c > 0 end) %{tx | :abbreviation => "#{abbrev}"} @@ -186,4 +186,4 @@ defmodule Timex.Parsers.ZoneInfo do defp parse_int(<>), do: {val, rest} defp parse_char(<>), do: {val, rest} defp parse_uchar(<>), do: {val, rest} -end \ No newline at end of file +end diff --git a/lib/timex.ex b/lib/timex.ex index f26bd260..830ba504 100644 --- a/lib/timex.ex +++ b/lib/timex.ex @@ -3,14 +3,12 @@ defmodule Timex do quote do alias Timex.DateTime alias Timex.Date - alias Timex.DateFormat alias Timex.Time alias Timex.TimezoneInfo alias Timex.Timezone - alias Timex.Date.Convert, as: DateConvert - alias Timex.TimeFormatter, as: TimeFormat - alias Timex.DateFormat.Formatters, as: TimexFormatters - alias Timex.Parsers.DateFormat, as: TimexParsers + alias Timex.Format.DateTime.DateFormat + alias Timex.Date.Convert, as: DateConvert + alias Timex.Format.Time.TimeFormatter, as: TimeFormat end end @moduledoc File.read!("README.md") diff --git a/lib/timezone/timezone_local.ex b/lib/timezone/timezone_local.ex index 51d76ee2..dbead4e5 100644 --- a/lib/timezone/timezone_local.ex +++ b/lib/timezone/timezone_local.ex @@ -27,8 +27,8 @@ defmodule Timex.Timezone.Local do alias Timex.DateTime, as: DateTime alias Timex.Date, as: Date alias Timex.Timezone.Database, as: ZoneDatabase - alias Timex.Parsers.ZoneInfo, as: ZoneParser - alias Timex.Parsers.ZoneInfo.TransitionInfo + alias Timex.Parse.ZoneInfo.Parser, as: ZoneParser + alias Timex.Parse.ZoneInfo.Parser.TransitionInfo @_ETC_TIMEZONE "/etc/timezone" @_ETC_SYS_CLOCK "/etc/sysconfig/clock" diff --git a/lib/utils/utils.ex b/lib/utils/utils.ex index 2e9dc926..44c0a321 100644 --- a/lib/utils/utils.ex +++ b/lib/utils/utils.ex @@ -32,35 +32,35 @@ defmodule Timex.Utils do """ @spec get_plugins(atom) :: [] | [atom] def get_plugins(plugin_type) when is_atom(plugin_type) do - type_str = plugin_type |> Atom.to_char_list - namespace = type_str |> Enum.reverse |> Enum.drop_while(fn c -> c != ?. end) |> Enum.reverse - suffix = (type_str -- namespace) |> List.to_string - re = ~r"#{namespace |> List.to_string |> String.replace(".", "\\.")}[\w\d]+#{suffix}" - available_modules |> Enum.reduce([], &load_plugin(re, &1, &2)) - end - - defp load_plugin(re, module_name, modules) do - case Regex.match?(re, module_name) do - true -> do_load_plugin(module_name, modules) - false -> modules + case Process.get(:timex_plugins) do + nil -> + plugins = available_modules(plugin_type) |> Enum.reduce([], &load_plugin/2) + Process.put(:timex_plugins, plugins) + plugins + plugins -> + plugins end end - defp do_load_plugin(module, modules) when is_binary(module) do - do_load_plugin(module |> String.to_atom, modules) - end - defp do_load_plugin(module, modules) when is_atom(module) do + defp load_plugin(module, modules) do if Code.ensure_loaded?(module), do: [module | modules], else: modules end - defp available_modules do + defp available_modules(plugin_type) do apps_path = Mix.Project.build_path |> Path.join("lib") apps = apps_path |> File.ls! apps |> Enum.map(&(Path.join([apps_path, &1, "ebin"]))) - |> Enum.filter(&File.exists?/1) - |> Enum.map(&File.ls!/1) - |> List.flatten - |> Enum.map(&String.replace(&1, ".beam", "")) + |> Enum.map(fn app_path -> app_path |> File.ls! |> Enum.map(&(Path.join(app_path, &1))) end) + |> Enum.flat_map(&(&1)) + |> Enum.filter(&(String.ends_with?(&1, ".beam"))) + |> Enum.map(fn path -> + {:ok, {module, chunks}} = :beam_lib.chunks('#{path}', [:attributes]) + {module, get_in(chunks, [:attributes, :behaviour])} + end) + |> Enum.filter(fn {_module, behaviours} -> + is_list(behaviours) && plugin_type in behaviours + end) + |> Enum.map(fn {module, _} -> module end) end end diff --git a/test/format_default_test.exs b/test/format_default_test.exs index bdb69d94..c5558883 100644 --- a/test/format_default_test.exs +++ b/test/format_default_test.exs @@ -2,7 +2,7 @@ defmodule DateFormatTest.FormatDefault do use ExUnit.Case, async: true use Timex - alias Timex.DateFormat.Formatters.DefaultFormatter + alias Timex.Format.DateTime.Formatters.DefaultFormatter test :format_year do date = Date.from({2013,8,18}) diff --git a/test/validate_default_test.exs b/test/validate_default_test.exs index 6af18e5e..03facccd 100644 --- a/test/validate_default_test.exs +++ b/test/validate_default_test.exs @@ -1,5 +1,6 @@ defmodule DateFormatTest.ValidateDefault do use ExUnit.Case + use Timex test :validate do assert {:error, "Format string cannot be nil or empty!"} = validate "" @@ -17,6 +18,6 @@ defmodule DateFormatTest.ValidateDefault do end defp validate(fmt) do - Timex.DateFormat.validate(fmt) + DateFormat.validate(fmt) end -end \ No newline at end of file +end