This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Infix: | |
def __init__(self, function): | |
self.function = function | |
def __rxor__(self, other): | |
return Infix(lambda x, self=self, other=other: self.function(other, x)) | |
def __xor__(self, other): | |
return self.function(other) | |
def __call__(self, value1, value2): | |
return self.function(value1, value2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://medium.com/learn-elixir/disassemble-elixir-code-1bca5fe15dd1 | |
f = './_build/dev/lib/test/ebin/Elixir.Test.beam' | |
result = :beam_lib.chunks(f,[:abstract_code]) | |
{:ok,{_,[{:abstract_code,{_,ac}}]}} = result | |
IO.puts :erl_prettypr.format(:erl_syntax.form_list(ac)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.Report do | |
@report_sql "SELECT * FROM …" # your raw SQL query here | |
def with_stream(callback) do | |
MyApp.Repo.transaction(fn -> | |
stream = Ecto.Adapters.SQL.stream(MyApp.Repo, @report_sql, []) | |
callback.(stream) | |
end) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.Report do | |
import Ecto.Query | |
def with_stream(callback) do | |
MyApp.Repo.transaction(fn -> | |
query = from MyApp.SomeSchema |> where(…) # your Ecto.Query here | |
stream = MyApp.Repo.stream(query) | |
callback.(stream) | |
end) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.ReportController do | |
use MyAppWeb, :controller | |
def index(conn, _params) do | |
conn = | |
conn | |
|> put_resp_content_type("text/csv") | |
|> put_resp_header("content-disposition", ~s[attachment; filename="report.csv"]) | |
|> send_chunked(:ok) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def authorize(operation, txn_key, action, opts \\ []) do | |
user = opts[:user] || operation.context.user | |
ExOperation.Operation.step(operation, {:authorize, txn_key}, fn | |
%{^txn_key => struct} -> | |
policy = (opts[:policy] || struct.__struct__) |> Module.concat(Policy) | |
with :ok <- Bodyguard.authorize(policy, action, user, struct: struct) do | |
{:ok, :authorized} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# mix.exs | |
{:ex_operation, “~> 0.4”} | |
# config/config.exs | |
config :ex_operation, repo: MyApp.Repo | |
# lib/myapp/operation.ex | |
defmodule MyApp.Operation do | |
defmacro __using__(opts) do | |
quote do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
operation | |
|> step(:foo, &foo/1) | |
|> defer(fn op, txn -> | |
case txn.foo.some_property do | |
:one -> | |
op |> step(:bar, &bar/1) | |
:two -> | |
op |> step(:baz, &baz/1) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
operation | |
|> step(:foo, &foo/1) | |
|> after_commit(fn txn -> | |
run_some_async_job(txn.foo) | |
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
operation | |
|> step(:foo, &foo/1) | |
|> suboperation(OtherOperation, &%{foo_id: &1.foo.id}, id: :sub) | |
|> step(:bar, &{:ok, &1.sub.result}) |