Skip to content

Instantly share code, notes, and snippets.

View feymartynov's full-sized avatar

Timofey Martynov feymartynov

  • Wildberries
  • Moscow
View GitHub Profile
@feymartynov
feymartynov / cat_operator.py
Created February 28, 2019 20:52
cat operator in python
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)
@feymartynov
feymartynov / iex.ex
Created February 19, 2019 13:31
Decompile Elixir into Erlang
# 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))
@feymartynov
feymartynov / report.ex
Created November 29, 2018 00:20
Phoenix CSV export article: raw SQL report
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
@feymartynov
feymartynov / report.ex
Last active August 6, 2019 20:03
Phoenix CSV export article: Ecto.Query report
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
@feymartynov
feymartynov / report_controller.ex
Last active November 29, 2018 00:58
Phoenix CSV export article: controller
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)
@feymartynov
feymartynov / dsl_extension.ex
Created November 24, 2018 01:10
ExOperation article: DSL extension
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
@feymartynov
feymartynov / setup.ex
Created November 24, 2018 01:02
ExOperation article: Setup
# 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
@feymartynov
feymartynov / defer.ex
Created November 24, 2018 00:57
ExOperation article: defer example
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
@feymartynov
feymartynov / after_commit.ex
Created November 24, 2018 00:48
ExOperation article: After commit example
operation
|> step(:foo, &foo/1)
|> after_commit(fn txn ->
run_some_async_job(txn.foo)
end)
@feymartynov
feymartynov / suboperation.ex
Created November 24, 2018 00:45
ExOperation article: Suboperation example
operation
|> step(:foo, &foo/1)
|> suboperation(OtherOperation, &%{foo_id: &1.foo.id}, id: :sub)
|> step(:bar, &{:ok, &1.sub.result})