Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/edubkendo/kitsune
An Elixir library for transforming the representation of data
https://github.com/edubkendo/kitsune
Last synced: about 11 hours ago
JSON representation
An Elixir library for transforming the representation of data
- Host: GitHub
- URL: https://github.com/edubkendo/kitsune
- Owner: edubkendo
- License: mit
- Created: 2014-12-21T22:22:25.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2014-12-23T10:38:00.000Z (almost 10 years ago)
- Last Synced: 2024-11-01T09:34:36.226Z (7 days ago)
- Language: CSS
- Size: 234 KB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- freaking_awesome_elixir - CSS - An Elixir library for transforming the representation of data. (Text and Numbers)
- fucking-awesome-elixir - kitsune - An Elixir library for transforming the representation of data. (Text and Numbers)
- awesome-elixir - kitsune - An Elixir library for transforming the representation of data. (Text and Numbers)
README
Kitsune
=======>A kitsune is a shapeshifter, and usually when it reaches the age of 100 years, it learn the ability to take on a human form. Thus, they have to be a fox for a hundred years before it can shapeshift from a fox to a human and back again. It is also said that a kitsune can duplicate other human beings, in other words shapeshift into the look-a-likes of different people.
-- http://www.mythicalcreaturesguide.com/page/Kitsune
Kitsune is an Elixir library for transforming the representation of data inspired by [Representable](https://github.com/apotonick/representable/).
## Examples
### Properties
```elixir
defmodule Person do
defstruct name: nil, age: nil
enddefmodule PersonRepresenter do
use Kitsune.JSONproperty :name
property :age
endperson = %Person{name: "Nikki", age: 18}
PersonRepresenter.to_json(person)
#=> "{\"name\":\"Nikki\",\"age\":18}"json = "{\"name\":\"Nikki\",\"age\":18}"
PersonRepresenter.from_json(json, Person)
#=> %Person{ name: "Nikki", age: 18 }
```If your property doesn't match the name of the key in the Struct, use `as`.
```elixir
defmodule Song do
defstruct name: nil
enddefmodule SongRepresenter do
use Kitsune.JSONproperty :title, as: :name
endsong = %Song{name: "Gin and Juice"}
SongRepresenter.to_json(song)
#=> "{\"title\":\"Gin and Juice\"}"json = "{\"title\":\"Gin and Juice\"}"
SongRepresenter.from_json(json, Song)
#=> %Song{name: "Gin and Juice"}```
### CollectionsYou can use collections to create collections of representations inside representations.
```elixir
defmodule Album do
defstruct name: nil, songs: []
enddefmodule AlbumRepresenter do
use Kitsune.JSONproperty :name
collection :songs, extend: SongRepresenter, from: Song
endalbum = %Album{name: "Doggystyle", songs: [song, %Song{name: "Lodi Dodi"}]}
AlbumRepresenter.to_json(album)
#=> "{\"name\":\"Doggystyle\",\"songs\":[{\"title\":\"Gin and Juice\"},{\"title\":\"Lodi Dodi\"}]}"json = "{\"name\":\"Doggystyle\",\"songs\":[{\"title\":\"Gin and Juice\"},{\"title\":\"Lodi Dodi\"}]}"
AlbumRepresenter.from_json(json, Album)
#=> %Album{name: "Doggystyle", songs: [%Song{name: "Gin and Juice"}, %Song{name: "Lodi Dodi"}]}```
## Other
- [Docs](http://hexdocs.pm/kitsune/)