Code Monkey home page Code Monkey logo

ecto_interval's Introduction

EctoInterval

Interval type for Ecto, currently only implemented for PostgreSQL, PR's to support others are welcome.

Installation

Available in Hex, the package can be installed by adding ecto_interval to your list of dependencies in mix.exs:

    {:ecto_interval, "~> 0.2.4"},

Usage

Just use EctoInterval as a field type, like:

    field :renewal_period, EctoInterval, default: %{months: 0, days: 0, secs: 0}

Contributors

  • Special thanks to @ibarchenkov for updating to latest implementation!

ecto_interval's People

Contributors

fchabouis avatar nathanl avatar overminddl1 avatar tomciopp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ecto_interval's Issues

Outdated dependencies?

Hi,

It seems that the dependencies are too old to allow this module to be installed:

Failed to use "phoenix_html" (version 2.13.3) because
  ecto_interval (version 0.2.1) requires ~> 2.12.0
  phoenix_ecto (version 4.0.0) requires ~> 2.9
  scrivener_html (version 1.8.1) requires ~> 2.2
  mix.lock specifies 2.13.3
Failed to use "postgrex" because
  ecto_interval (version 0.2.1) requires ~> 0.14.0
  ecto_sql (version 3.1.6) requires ~> 0.14.0 or ~> 0.15.0
  mix.lock specifies 0.15.0

Outdated Dependencies

Looks like #3 is happening again:

Failed to use "phoenix_html" (versions 3.0.0 to 3.0.3) because
  ecto_interval (version 0.2.3) requires ~> 2.12

Having trouble querying an interval

I'm n00b to Elixir, so this could very easily be my mistake, but I'm having trouble using this library.

Gvien the following schema:

defmodule HsmElixir.Attendance.Record do
  use Ecto.Schema
  import Ecto.Changeset

  @schema_prefix "hsm_public"
  @primary_key {:id, Ecto.UUID, autogenerate: true}

  schema "attendance_record" do
    field :in, :utc_datetime
    field :notes, :string
    field :out, :utc_datetime
    field :type, :string
    field :duration, EctoInterval, virtual: true
    belongs_to :user, HsmElixir.Accounts.User, type: Ecto.UUID
  end

  @doc false
  def changeset(record, attrs) do
    record
    |> cast(attrs, [:in, :out, :notes, :type])
    |> validate_required([:in, :type])
  end
end

And using it thusly:

  def record_query() do
    from r in Record,
      as: :attendance_record,
      select_merge: %{
        in: r.in,
        out: r.out,
        type: r.type,
        user_id: r.user_id,
        duration: fragment("hsm_public.attendance_record_duration(?)", r)
      }
  end

  def list_records do
    Repo.all(record_query())
  end

I get the following error:

Screenshot from 2019-11-06 13-05-41

Better string formatting

At the moment string formatting isn't always the most helpful:

iex(34)> to_string(%Interval{months: 1, days: 2, secs: 3665})
"Every 1 months 2 days 3665 seconds"

I've come up with a function that uses Timex.Duration to give a better representation:

iex(37)> format_interval(%Interval{months: 1, days: 2, secs: 3665})
"1 month 2 days 1 hour 1 minute 5 seconds"

I appreciate that we don't want ecto_interval to depend on Timex but if there's interest maybe I can come up with another function that doesn't depend on it and submit a PR.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.