All Projects → vic → indifferent

vic / indifferent

Licence: other
Elixir Indifferent access on maps/lists/tuples with custom key transforms.

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to indifferent

Codebattle
Codebattle game
Stars: ✭ 209 (+945%)
Mutual labels:  phoenix
glasnost
Glasnost is a GraphQL app server for Golos/Steem
Stars: ✭ 23 (+15%)
Mutual labels:  phoenix
howto
How to do things on the Internet
Stars: ✭ 78 (+290%)
Mutual labels:  phoenix
Ex Shop
Digital goods shop & blog created using Elixir (Phoenix framework)
Stars: ✭ 214 (+970%)
Mutual labels:  phoenix
Exchat
(Not maintaining) A Slack-like app by Elixir, Phoenix & React(redux)
Stars: ✭ 252 (+1160%)
Mutual labels:  phoenix
pokerex client
Elm client for PokerEx project
Stars: ✭ 39 (+95%)
Mutual labels:  phoenix
Realtime
Listen to your to PostgreSQL database in realtime via websockets. Built with Elixir.
Stars: ✭ 4,278 (+21290%)
Mutual labels:  phoenix
phoenix-hibernate-dialect
An Apache Phoenix Hibernate dialect
Stars: ✭ 20 (+0%)
Mutual labels:  phoenix
Conduit
RealWorld example backend implementing the CQRS/ES pattern in Elixir and Phoenix
Stars: ✭ 253 (+1165%)
Mutual labels:  phoenix
livebook
Automate code & data workflows with interactive Elixir notebooks
Stars: ✭ 3,402 (+16910%)
Mutual labels:  phoenix
Heroku Buildpack Phoenix Static
A Heroku buildpack for building Phoenix's static assets
Stars: ✭ 225 (+1025%)
Mutual labels:  phoenix
Params
Easy parameters validation/casting with Ecto.Schema, akin to Rails' strong parameters.
Stars: ✭ 239 (+1095%)
Mutual labels:  phoenix
keeper
Flexible and Simple authentication solution for Phoenix
Stars: ✭ 27 (+35%)
Mutual labels:  phoenix
Absinthe plug
Plug support for Absinthe, the GraphQL toolkit for Elixir
Stars: ✭ 209 (+945%)
Mutual labels:  phoenix
ticker-react
React Client for displaying Stock Quotes (IEX Trading)
Stars: ✭ 34 (+70%)
Mutual labels:  phoenix
Formex
A better form library for Phoenix
Stars: ✭ 206 (+930%)
Mutual labels:  phoenix
phoenix assets webpack
Asset Pipeline with Webpack on Phoenix
Stars: ✭ 52 (+160%)
Mutual labels:  phoenix
Sphinx
Authorization library for Phoenix web framework
Stars: ✭ 19 (-5%)
Mutual labels:  phoenix
ambry
Self-hosted audiobook streaming server
Stars: ✭ 38 (+90%)
Mutual labels:  phoenix
phoenix
Apache Phoenix / Hbase Spring Boot Microservices
Stars: ✭ 23 (+15%)
Mutual labels:  phoenix

Indifferent

Build Status help maintain this lib

This library provides a wrapper for indifferent access on maps, structs, lists and tuples.

You could use Indifferent to wrap a parameters hash or something without having to distingish on the type of keys (atoms or binaries) akin to the features of Rails' HashWithIndifferentAccess.

Installation

Available in Hex, the package can be installed as:

  1. Add indifferent to your list of dependencies in mix.exs:
  def deps do
    [{:indifferent, "~> 0.9"}]
  end
  1. Ensure indifferent is started before your application:
  def application do
    [applications: [:indifferent]]
  end

Usage

Be sure to look at the documentation for more examples.

Indifferent module

##
# Wrapping a map and accesing it with an atom key
iex> i = Indifferent.access(%{"a" => 1})
iex> i[:a]
1


##
# You can provide your custom list of key transformations
# and specify how are keys indifferents for your use case.
iex> i = Indifferent.access(%{"a" => 1},
...>   key_transforms: [fn x, _indifferent -> {:ok, String.downcase(x)} end])
iex> i["A"]
1

##
# Indifferent is compatible with Elixir's Access
iex> Kernel.get_in(%{"a" => 1}, [Indifferent.at(:a)])
1
iex> Kernel.get_in(Indifferent.access(%{"a" => 1}), [:a])
1

##
# Or you can auto wrap data by using Indifferent's version of
# `get_in/2`, `get_and_update_in/3`, `pop_in/2`
iex> Indifferent.get_and_update_in(%{"a" => %{"x" => 1}}, [:a, :x], fn x -> {x * 2, x * 4} end)
{2, %{"a" => %{"x" => 4}}}


##
# Indifferent `path/2` lets you use any syntax you want for
# accessing the nested value you need.
iex> %{"b" => %{"c" => %{"d" => %{"e" => 4}}}} |> Indifferent.path(b["c"][:d].e)
4

##
# And works on lists, tuples, keywords
iex> [9, %{"c" => {:ok, [e: 4]}}] |> Indifferent.path(1.c["1"].e)
4

##
# Accessing nil yields nil
iex> nil |> Indifferent.path(foo.bar)
nil

##
# Can access inside structs
iex> %User{name: "john"} |> Indifferent.path(name)
"john"

##
# And can be used with Kernel methods.
iex> Kernel.get_in(%{"a" => {0, 2}}, Indifferent.path(a["1"]))
2

##
# `path/2` can also take a Keyword of paths and returns a keyword of values
iex> %{"b" => [1, 2]} |> Indifferent.path(x: b[-1])
[x: 2]

##
# The `read/1` macro is a convenience that reads an indifferent path on the first value
iex> System.put_env("COLOR", "red")
iex> Indifferent.read(System.get_env.COLOR)
"red"

##
# Just like `path/2`, the `read/1` macro can also take a Keyword of named things to read
iex> System.put_env("COLOR", "red")
iex> Process.put(:color, "blue")
iex> Indifferent.read(a: System.get_env.COLOR, b: Process.get.color)
[a: "red", b: "blue"]

Indifferent sigils

The ~i and ~I sigils are shortcuts for the API previously described.

iex> import Indifferent.Sigils
Indifferent.Sigils

##
# `~i` is the sigil for `Indifferent.read/1`
iex> data = %{"a" => [b: {10, 20}]}
iex> ~i(data.a.b[1])
20

# When piped a value it will act just like `Indifferent.path/2`
iex> data = %{"a" => [b: {10, 20}]}
iex> data |> ~i(a.b[1])
20


##
# `~I` is the sigil version of `Indifferent.path/1`
# for use with Kernel access functions.

iex> data = %{"a" => [b: {10, 20}]}
iex> Kernel.get_in(data, ~I(a.b[1]))
20

iex> data = %{"a" => [b: {10, 20}]}
iex> Kernel.pop_in(data, ~I(a.b))
{{10, 20}, %{"a" => []}}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].