Skip to content
Permalink
Branch: master
Commits on Jul 10, 2019
  1. Update readme

    tlux committed Jul 10, 2019
  2. Improve implementations of all types to have similar cast behavior fo…

    tlux committed Jul 10, 2019
    …r maps
  3. Enhance readme

    tlux committed Jul 10, 2019
  4. Update file_size

    tlux committed Jul 10, 2019
Commits on Jun 27, 2019
  1. Improve readme and update description

    tlux committed Jun 27, 2019
  2. Add docs

    tlux committed Jun 27, 2019
  3. Update file_size

    tlux committed Jun 27, 2019
  4. Set file_size version to 2.0

    tlux committed Jun 27, 2019
Commits on Jun 7, 2019
  1. Bump version

    tlux committed Jun 7, 2019
  2. Fix tests and update FileSize to next branch

    tlux committed Jun 7, 2019
Commits on Jun 2, 2019
  1. Change namespace

    tlux committed Jun 2, 2019
Commits on Jun 1, 2019
  1. Refactor Bit and Byte cast with float value function

    tlux committed Jun 1, 2019
  2. Improve integration test

    tlux committed Jun 1, 2019
  3. Add BitWithUnit and ByteWithUnit

    tlux committed May 31, 2019
    diff --git a/lib/file_size_ecto/bit.ex b/lib/file_size_ecto/bit.ex
    new file mode 100644
    index 0000000..0ce5551
    --- /dev/null
    +++ b/lib/file_size_ecto/bit.ex
    @@ -0,0 +1,17 @@
    +defmodule FileSizeEcto.Bit do
    +  @behaviour Ecto.Type
    +
    +  alias FileSizeEcto.WithoutUnit
    +
    +  @impl true
    +  def type, do: :integer
    +
    +  @impl true
    +  def cast(term), do: WithoutUnit.cast(term, :bit)
    +
    +  @impl true
    +  def load(term), do: WithoutUnit.load(term, :bit)
    +
    +  @impl true
    +  def dump(term), do: WithoutUnit.dump(term, :bit)
    +end
    diff --git a/lib/file_size_ecto/byte.ex b/lib/file_size_ecto/byte.ex
    new file mode 100644
    index 0000000..bf5b5e4
    --- /dev/null
    +++ b/lib/file_size_ecto/byte.ex
    @@ -0,0 +1,17 @@
    +defmodule FileSizeEcto.Byte do
    +  @behaviour Ecto.Type
    +
    +  alias FileSizeEcto.WithoutUnit
    +
    +  @impl true
    +  def type, do: :integer
    +
    +  @impl true
    +  def cast(term), do: WithoutUnit.cast(term, :b)
    +
    +  @impl true
    +  def load(term), do: WithoutUnit.load(term, :b)
    +
    +  @impl true
    +  def dump(term), do: WithoutUnit.dump(term, :b)
    +end
    diff --git a/lib/file_size_ecto/byte_based.ex b/lib/file_size_ecto/byte_based.ex
    new file mode 100644
    index 0000000..5f7f92c
    --- /dev/null
    +++ b/lib/file_size_ecto/byte_based.ex
    @@ -0,0 +1,17 @@
    +defmodule FileSizeEcto.ByteBased do
    +  @behaviour Ecto.Type
    +
    +  alias FileSizeEcto.WithUnit
    +
    +  @impl true
    +  def type, do: :map
    +
    +  @impl true
    +  def cast(term), do: WithUnit.cast(term)
    +
    +  @impl true
    +  def load(term), do: WithUnit.load(term)
    +
    +  @impl true
    +  def dump(term), do: WithUnit.dump(term)
    +end
    diff --git a/lib/file_size_ecto/type.ex b/lib/file_size_ecto/type.ex
    deleted file mode 100644
    index a09a7f2..0000000
    --- a/lib/file_size_ecto/type.ex
    +++ /dev/null
    @@ -1,71 +0,0 @@
    -defmodule FileSizeEcto.Type do
    -  alias FileSize.Bit
    -  alias FileSize.Byte
    -  alias FileSize.Convertible
    -
    -  defmacro __using__(unit: unit) do
    -    quote bind_quoted: [unit: unit] do
    -      @behaviour Ecto.Type
    -
    -      @impl true
    -      def type, do: :integer
    -
    -      @impl true
    -      def cast(term) do
    -        unquote(__MODULE__).cast(unquote(unit), term)
    -      end
    -
    -      @impl true
    -      def dump(term) do
    -        unquote(__MODULE__).dump(unquote(unit), term)
    -      end
    -
    -      @impl true
    -      def load(term) do
    -        unquote(__MODULE__).load(unquote(unit), term)
    -      end
    -    end
    -  end
    -
    -  @SPEC cast(FileSize.unit(), term) ::
    -          {:ok, FileSize.t()} | {:error, Keyword.t()} | :error
    -  def cast(unit, term)
    -
    -  def cast(unit, value) when is_integer(value) do
    -    {:ok, FileSize.new(value, unit)}
    -  end
    -
    -  def cast(unit, value) when is_float(value) do
    -    cast(unit, trunc(value))
    -  end
    -
    -  def cast(unit, str) when is_binary(str) do
    -    case FileSize.parse(str) do
    -      {:ok, size} -> {:ok, FileSize.convert(size, unit)}
    -      {:error, _} -> :error
    -    end
    -  end
    -
    -  def cast(unit, %type{} = size) when type in [Bit, Byte] do
    -    {:ok, FileSize.convert(size, unit)}
    -  end
    -
    -  def cast(_unit, _term), do: :error
    -
    -  @SPEC dump(FileSize.unit(), term) :: {:ok, integer} | :error
    -  def dump(unit, term) do
    -    case cast(unit, term) do
    -      {:ok, size} -> {:ok, Convertible.normalized_value(size)}
    -      _ -> :error
    -    end
    -  end
    -
    -  @SPEC load(FileSize.unit(), term) :: {:ok, FileSize.t()} | :error
    -  def load(unit, term)
    -
    -  def load(unit, value) when is_integer(value) do
    -    {:ok, FileSize.new(value, unit)}
    -  end
    -
    -  def load(_unit, _term), do: :error
    -end
    diff --git a/lib/file_size_ecto/type/bit.ex b/lib/file_size_ecto/type/bit.ex
    deleted file mode 100644
    index 242b3b3..0000000
    --- a/lib/file_size_ecto/type/bit.ex
    +++ /dev/null
    @@ -1,3 +0,0 @@
    -defmodule FileSizeEcto.Type.Bit do
    -  use FileSizeEcto.Type, unit: :bit
    -end
    diff --git a/lib/file_size_ecto/type/byte.ex b/lib/file_size_ecto/type/byte.ex
    deleted file mode 100644
    index c121b6e..0000000
    --- a/lib/file_size_ecto/type/byte.ex
    +++ /dev/null
    @@ -1,3 +0,0 @@
    -defmodule FileSizeEcto.Type.Byte do
    -  use FileSizeEcto.Type, unit: :b
    -end
    diff --git a/lib/file_size_ecto/type/byte_with_unit.ex b/lib/file_size_ecto/type/byte_with_unit.ex
    deleted file mode 100644
    index 21951e9..0000000
    --- a/lib/file_size_ecto/type/byte_with_unit.ex
    +++ /dev/null
    @@ -1,49 +0,0 @@
    -defmodule FileSizeEcto.Type.ByteWithUnit do
    -  @behaviour Ecto.Type
    -
    -  alias FileSize.Convertible
    -  alias FileSize.InvalidUnitError
    -  alias FileSizeEcto.Type.Byte, as: ByteType
    -
    -  @impl true
    -  def type, do: :map
    -
    -  @impl true
    -  def cast(term)
    -
    -  def cast(%{"bytes" => _, "unit" => _} = term) do
    -    load(term)
    -  end
    -
    -  def cast(term) do
    -    ByteType.cast(term)
    -  end
    -
    -  @impl true
    -  def dump(term) do
    -    case cast(term) do
    -      {:ok, size} ->
    -        {:ok, %{
    -          "bytes" => Convertible.normalized_value(size),
    -          "unit" => to_string(size.unit)
    -        }}
    -
    -      _ ->
    -        :error
    -    end
    -  end
    -
    -  @impl true
    -  def load(term)
    -
    -  def load(%{"bytes" => bytes, "unit" => unit_str})
    -  when is_integer(bytes) and is_binary(unit_strs) do
    -    unit = String.to_existing_atom(unit_str)
    -    {:ok, FileSize.from_bytes(bytes, unit)}
    -  rescue
    -    error in [ArgumentError, InvalidUnitError] ->
    -      :error
    -  end
    -
    -  def load(_term), do: :error
    -end
    diff --git a/lib/file_size_ecto/with_unit.ex b/lib/file_size_ecto/with_unit.ex
    new file mode 100644
    index 0000000..bdb32fa
    --- /dev/null
    +++ b/lib/file_size_ecto/with_unit.ex
    @@ -0,0 +1,46 @@
    +defmodule FileSizeEcto.WithUnit do
    +  @moduledoc false
    +
    +  alias FileSize.Convertible
    +  alias FileSize.InvalidUnitError
    +  alias FileSizeEcto.WithoutUnit
    +
    +  @SPEC cast(term, FileSize.unit()) :: {:ok, FileSize.t()} | :error
    +  def cast(term, unit)
    +
    +  def cast(%{value: value, unit: unit}) do
    +    {:ok, FileSize.new(value, unit)}
    +  end
    +
    +  def cast(term, unit) do
    +    WithoutUnit.cast(term, unit)
    +  end
    +
    +  @SPEC dump(term, FileSize.unit()) :: {:ok, map} | :error
    +  def dump(term, unit) do
    +    case cast(term, unit) do
    +      {:ok, size} ->
    +        {:ok,
    +         %{
    +           "bytes" => Convertible.normalized_value(size),
    +           "unit" => to_string(size.unit)
    +         }}
    +
    +      _ ->
    +        :error
    +    end
    +  end
    +
    +  @SPEC load(FileSize.unit(), term) :: {:ok, FileSize.t()} | :error
    +  def load(term, unit)
    +
    +  def load(map, unit) when is_map(map) do
    +    unit = String.to_existing_atom(unit_str)
    +    {:ok, FileSize.from_bytes(bytes, unit)}
    +  rescue
    +    _error in [ArgumentError, InvalidUnitError] ->
    +      :error
    +  end
    +
    +  def load(_term, _unit), do: :error
    +end
    diff --git a/lib/file_size_ecto/without_unit.ex b/lib/file_size_ecto/without_unit.ex
    new file mode 100644
    index 0000000..079314c
    --- /dev/null
    +++ b/lib/file_size_ecto/without_unit.ex
    @@ -0,0 +1,48 @@
    +defmodule FileSizeEcto.WithoutUnit do
    +  @moduledoc false
    +
    +  alias FileSize.Bit
    +  alias FileSize.Byte
    +  alias FileSize.Convertible
    +
    +  @SPEC cast(term, FileSize.unit()) :: {:ok, FileSize.t()} | :error
    +  def cast(term, unit)
    +
    +  def cast(value, unit) when is_integer(value) do
    +    {:ok, FileSize.new(value, unit)}
    +  end
    +
    +  def cast(value, unit) when is_float(value) do
    +    value |> trunc() |> cast(unit)
    +  end
    +
    +  def cast(str, unit) when is_binary(str) do
    +    case FileSize.parse(str) do
    +      {:ok, size} -> {:ok, FileSize.convert(size, unit)}
    +      {:error, _} -> :error
    +    end
    +  end
    +
    +  def cast(%type{} = size, unit) when type in [Bit, Byte] do
    +    {:ok, FileSize.convert(size, unit)}
    +  end
    +
    +  def cast(_term, _unit), do: :error
    +
    +  @SPEC dump(term, FileSize.unit()) :: {:ok, integer} | :error
    +  def dump(term, unit) do
    +    case cast(term, unit) do
    +      {:ok, size} -> {:ok, Convertible.normalized_value(size)}
    +      _ -> :error
    +    end
    +  end
    +
    +  @SPEC load(term, FileSize.unit()) :: {:ok, FileSize.t()} | :error
    +  def load(term, unit)
    +
    +  def load(value, unit) when is_integer(value) do
    +    {:ok, FileSize.new(value, unit)}
    +  end
    +
    +  def load(_term, _unit), do: :error
    +end
    diff --git a/test/file_size_ecto/type/bit_test.exs b/test/file_size_ecto/bit_test.exs
    similarity index 97%
    rename from test/file_size_ecto/type/bit_test.exs
    rename to test/file_size_ecto/bit_test.exs
    index 40a3b9c..ab71d16 100644
    --- a/test/file_size_ecto/type/bit_test.exs
    +++ b/test/file_size_ecto/bit_test.exs
    @@ -1,9 +1,9 @@
    -defmodule FileSizeEcto.Type.BitTest do
    +defmodule FileSizeEcto.BitTest do
       use ExUnit.Case
    
       import FileSize.Sigil
    
    -  alias FileSizeEcto.Type.Bit
    +  alias FileSizeEcto.Bit
    
       describe "type/0" do
         test "is integer" do
    diff --git a/test/file_size_ecto/byte_based_test.exs b/test/file_size_ecto/byte_based_test.exs
    new file mode 100644
    index 0000000..1bce7a7
    --- /dev/null
    +++ b/test/file_size_ecto/byte_based_test.exs
    @@ -0,0 +1,78 @@
    +defmodule FileSizeEcto.ByteBaseTest do
    +  use ExUnit.Case
    +
    +  import FileSize.Sigil
    +
    +  alias FileSizeEcto.ByteBased
    +
    +  describe "type/0" do
    +    test "is map" do
    +      assert ByteBased.type() == :map
    +    end
    +  end
    +
    +  describe "cast/1" do
    +    test "cast string" do
    +      assert ByteBased.cast("16 GB") == {:ok, ~F(16 GB)}
    +      assert ByteBased.cast("8 GiB") == {:ok, ~F(8 GiB)}
    +      assert ByteBased.cast("12 B") == {:ok, ~F(12 B)}
    +      assert ByteBased.cast("20 bit") == {:ok, ~F(2 B)}
    +    end
    +
    +    test "cast integer" do
    +      bytes = 1_234_567
    +
    +      assert ByteBased.cast(bytes) == {:ok, FileSize.new(bytes, :b)}
    +    end
    +
    +    test "cast float" do
    +      assert ByteBased.cast(12345.67) == {:ok, ~F(12345 B)}
    +    end
    +
    +    test "cast FileSize.Byte" do
    +      assert ByteBased.cast(~F(4 B)) == {:ok, ~F(4 B)}
    +      assert ByteBased.cast(~F(4 kB)) == {:ok, ~F(4000 B)}
    +      assert ByteBased.cast(~F(4 KiB)) == {:ok, ~F(4096 B)}
    +    end
    +
    +    test "cast FileSize.Bit" do
    +      assert ByteBased.cast(~F(16 bit)) == {:ok, ~F(2 B)}
    +      assert ByteBased.cast(~F(16 kbit)) == {:ok, ~F(2000 B)}
    +      assert ByteBased.cast(~F(16 Kibit)) == {:ok, ~F(2048 B)}
    +    end
    +
    +    test "cast map" do
    +      assert ByteBased.cast(%{value: 16, unit: :b}) == {:ok, ~F(16 B)}
    +      assert ByteBased.cast(%{value: 16, unit: :gb}) == {:ok, ~F(16 GB)}
    +      assert ByteBased.cast(%{value: 16, unit: :gib}) == {:ok, ~F(16 GiB)}
    +      assert ByteBased.cast(%{value: 16, unit: :b}) == :error
    +      assert ByteBased.cast(%{value: 16, unit: :kbit}) == :error
    +    end
    +
    +    test "error" do
    +      assert ByteBased.cast("") == :error
    +      assert ByteBased.cast("16 in") == :error
    +      assert ByteBased.cast(:invalid) == :error
    +    end
    +  end
    +
    +  describe "dump/1" do
    +    test "dump string"
    +
    +    test "dump integer"
    +
    +    test "dump float"
    +
    +    test "dump FileSize.Byte"
    +
    +    test "dump FileSize.Bit"
    +
    +    test "error"
    +  end
    +
    +  describe "load/1" do
    +    test "load map"
    +
    +    test "error"
    +  end
    +end
    diff --git a/test/file_size_ecto/type/byte_test.exs b/test/file_size_ecto/byte_test.exs
    similarity index 97%
    rename from test/file_size_ecto/type/byte_test.exs
    rename to test/file_size_ecto/byte_test.exs
    index de2d671..9170b76 100644
    --- a/test/file_size_ecto/type/byte_test.exs
    +++ b/test/file_size_ecto/byte_test.exs
    @@ -1,9 +1,9 @@
    -defmodule FileSizeEcto.Type.ByteTest do
    +defmodule FileSizeEcto.ByteTest do
       use ExUnit.Case
    
       import FileSize.Sigil
    
    -  alias FileSizeEcto.Type.Byte
    +  alias FileSizeEcto.Byte
    
       describe "type/0" do
         test "is integer" do
    diff --git a/test/file_size_ecto/changeset_integration_test.exs b/test/file_size_ecto/changeset_integration_test.exs
    index c1b0264..fd03a70 100644
    --- a/test/file_size_ecto/changeset_integration_test.exs
    +++ b/test/file_size_ecto/changeset_integration_test.exs
    @@ -1,27 +1,27 @@
    -defmodule FileSizeEcto.ChangesetIntegrationTest do
    -  use ExUnit.Case
    -
    -  import Ecto.Changeset
    -
    -  describe "Ecto.Changeset.cast/2" do
    -    test "valid when value present" do
    -      changeset = cast(%TestSchema{}, %{byte_size: "16 MB"}, [:byte_size])
    -
    -      assert Enum.empty?(changeset.errors)
    -      assert get_change(changeset, :byte_size) == FileSize.new(16_000_000, :b)
    -    end
    -
    -    test "valid when value blank" do
    -      changeset = cast(%TestSchema{}, %{byte_size: ""}, [:byte_size])
    -
    -      assert Enum.empty?(changeset.errors)
    -      assert get_change(changeset, :byte_size) == nil
    -    end
    -
    -    test "invalid format" do
    -      changeset = cast(%TestSchema{}, %{byte_size: "invalid"}, [:byte_size])
    -
    -      assert changeset.errors != []
    -    end
    -  end
    -end
    +defmodule FileSizeEcto.ChangesetIntegrationTest do
    +  use ExUnit.Case
    +
    +  import Ecto.Changeset
    +
    +  describe "Ecto.Changeset.cast/2" do
    +    test "valid when value present" do
    +      changeset = cast(%TestSchema{}, %{byte_size: "16 MB"}, [:byte_size])
    +
    +      assert Enum.empty?(changeset.errors)
    +      assert get_change(changeset, :byte_size) == FileSize.new(16_000_000, :b)
    +    end
    +
    +    test "valid when value blank" do
    +      changeset = cast(%TestSchema{}, %{byte_size: ""}, [:byte_size])
    +
    +      assert Enum.empty?(changeset.errors)
    +      assert get_change(changeset, :byte_size) == nil
    +    end
    +
    +    test "invalid format" do
    +      changeset = cast(%TestSchema{}, %{byte_size: "invalid"}, [:byte_size])
    +
    +      assert changeset.errors != []
    +    end
    +  end
    +end
    diff --git a/test/file_size_ecto/type_test.exs b/test/file_size_ecto/type_test.exs
    deleted file mode 100644
    index 47178b4..0000000
    --- a/test/file_size_ecto/type_test.exs
    +++ /dev/null
    @@ -1,15 +0,0 @@
    -defmodule FileSizeEcto.TypeTest do
    -  use ExUnit.Case
    -
    -  describe "cast/2" do
    -    # TODO
    -  end
    -
    -  describe "dump/2" do
    -    # TODO
    -  end
    -
    -  describe "load/2" do
    -    # TODO
    -  end
    -end
    diff --git a/test/support/test_schema.ex b/test/support/test_schema.ex
    index 247b87e..30b51ea 100644
    --- a/test/support/test_schema.ex
    +++ b/test/support/test_schema.ex
    @@ -1,11 +1,11 @@
    -defmodule TestSchema do
    -  @moduledoc false
    -
    -  use Ecto.Schema
    -
    -  embedded_schema do
    -    field :bit_size, FileSizeEcto.Type.Bit
    -    field :byte_size, FileSizeEcto.Type.Byte
    -    field :byte_size_with_unit, FileSizeEcto.Type.ByteWithUnit
    -  end
    -end
    +defmodule TestSchema do
    +  @moduledoc false
    +
    +  use Ecto.Schema
    +
    +  embedded_schema do
    +    field(:bit_size, FileSizeEcto.Bit)
    +    field(:byte_size, FileSizeEcto.Byte)
    +    field(:byte_size_with_unit, FileSizeEcto.ByteBased)
    +  end
    +end
Commits on May 30, 2019
  1. Add ByteWithUnit type

    tlux committed May 30, 2019
  2. Refactor FileSizeEcto.Type

    tlux committed May 30, 2019
  3. Add test schema and integration test

    tlux committed May 30, 2019
  4. Change namespace of types

    tlux committed May 30, 2019
  5. Add ecto type

    tlux committed May 30, 2019
  6. Add tests and implementation for Bit type

    tlux committed May 30, 2019
  7. Update readme

    tlux committed May 30, 2019
  8. Add LICENSE

    tlux committed May 30, 2019
  9. Add Byte type

    tlux committed May 30, 2019
  10. Setup project

    tlux committed May 30, 2019
You can’t perform that action at this time.