Skip to content

Instantly share code, notes, and snippets.

@darui00kara
darui00kara / Omake.elm
Last active April 10, 2022 13:56
Elm json encode example
-- おまけ用のメモ
> body = Http.jsonBody (UserEncoder.user User.new)
StringBody "application/json" "{\"id\":0,\"name\":\"\",\"email\":\"\"}"
: Http.Body
> import Model.UserDecoder as UserDecoder
> Http.post "http://localhost:4000/exmamples" body UserDecoder.user
Request { method = "POST", headers = [], url = "http://localhost:4000/exmamples", body = StringBody "application/json" "{\"id\":0,\"name\":\"\",\"email\":\"\"}", expect = { responseType = "text", responseToResult = <function> }, timeout = Nothing, withCredentials = False }
: Http.Request Model.User.Schema
@darui00kara
darui00kara / macro_example.ex
Last active April 12, 2017 13:39
[elixir]map and ast toy
defmodule MacroExample do
alias User
alias Post
defmacro get_struct_info(
{:%, _, [{_, _, struct_name}, {:%{}, _, values}]}) do
quote do
{unquote(struct_name), unquote(values)}
end
@darui00kara
darui00kara / example.ex
Created December 10, 2016 06:55
Model for many-to-many and follow functions
defmodule SampleApp.User do
use SampleApp.Web, :model
alias SampleApp.Helpers.Encryption
schema "users" do
field :name, :string
field :email, :string
field :password, :string, virtual: true
field :password_digest, :string
<%= form_for @changeset, @action, fn f -> %>
<%= if f.errors != [] do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below:</p>
<ul>
<%= for {attr, message} <- f.errors do %>
<li><%= humanize(attr) %> <%= message %></li>
<% end %>
</ul>
</div>
<h2>Show user</h2>
<ul>
<li>
<strong>Name:</strong>
<%= @user.name %>
</li>
<li>
<h2>Listing users</h2>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Password</th>
<th></th>
defmodule EctoModelsSample.User do
use EctoModelsSample.Web, :model
use Ecto.Model.Callbacks
before_insert :set_password_digest
schema "users" do
field :name, :string
field :email, :string
field :password_digest, :string
defmodule SafetyboxSample do
def encrypt(password) do
Safetybox.encrypt(password)
end
def authentication(_userid, _user_name, password) do
encrypt_password = "" # DBからユーザデータを取得する処理が入る
Safetybox.is_decrypted(password, encrypt_password)
end
end
defp deps do
[ { :safetybox, "~> 0.1" } ]
end
defmodule TrotSample.Router do
use Trot.Router
use Trot.Template
get "/", do: 200
get "/hello" do
"Hello Trot!!"
end