Skip to content

Instantly share code, notes, and snippets.

defmodule Util.Tree do
def transform_keys(map, transform) when is_map(map) do
Map.new(
Enum.map(map, fn {k, v} ->
{transform.(k) |> IO.inspect(label: "transformed"), transform_keys(v, transform)}
end)
)
end
def transform_keys(list, transform) when is_list(list) do
@opsb
opsb / Cursor.elm
Last active February 14, 2022 00:26
elm-ui cursor
module Cursor exposing (cursor, Cursor(..))
import Element exposing (Element, Attribute, htmlAttribute)
import Html.Attributes
cursor : Cursor -> Attribute msg
cursor cursor_ =
htmlAttribute <| Html.Attributes.style "cursor" (cursorName cursor_)
@opsb
opsb / ecto_utils.ex
Last active March 1, 2018 16:23
Export elixir Ecto models as plain maps
defmodule Util.Ecto do
def export(model = %{__meta__: _meta, __struct__: _struct}) when is_map(model) do
stripped =
model
|> Map.from_struct()
|> Map.delete(:__meta__)
associations = model.__struct__.__schema__(:associations)
Enum.reduce(associations, stripped, fn assoc, model ->
@opsb
opsb / Function based
Created May 20, 2017 19:05
Possible view API styles
view model =
HolyGrail.view
|> HolyGrail.header
(HeaderCard.config
|> HeaderCard.leftGutter Avatar.small
|> HeaderCard.title "Using AI to Detect Emotions in text"
)
|> HolyGrail.body
(ScrollPaneCard.view
[ ChatDay.config
AppState (Main.elm delegates to one of these)
LoggedIn
Context.elm
Init.elm
Model.elm
Msg.elm
Subscriptions.elm
Update.elm
UrlUpdate.elm
View.elm
@opsb
opsb / actions.ex
Last active March 11, 2017 17:04
set_text_helper
defmodule Helpers.Actions do
def set_text(parent, %Query{} = query, text) when is_binary(text) do
parent |> find(query)
case Query.compile(query) do
{:css, selector} ->
parent
|> pipeline_script("document.querySelector('#{selector}').value = '#{text}'")
|> pipeline_script("document.querySelector('#{selector}').dispatchEvent(new Event('input'))")
{:xpath, xpath} ->
@opsb
opsb / index.html
Last active December 8, 2016 11:09
Flexbox spike
<html>
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.ns-autogrow/1.1.6/jquery.ns-autogrow.min.js"></script>
<script>
$(document).ready(function() {
$('.new-message textarea').autogrow({vertical: true, horizontal: false});
});
</script>
@opsb
opsb / OnEnter0.17.elm
Last active November 16, 2016 12:44
Replacing customDecoder for elm 0.18
onEnter : Msg -> Attribute Msg
onEnter msg =
let
filterKey ( code, shift ) =
if (isEnter code) && (not shift) then
Ok "triggering"
else
Err "not triggering"
decoder =
@opsb
opsb / Add metadata to a client
Last active October 31, 2016 08:44
Inject custom claims into auth0 jwt token using a rule
Go the settings page/section: Clients > ClientABC > Advanced Settings > advanced > Application Metadata
And add the metadata, e.g. Key: tenantId, Value: "tenant123"
import Html exposing (div, button, text)
import Html.App exposing (program)
import Html.Events exposing (onClick)
import Time
import Task
main =
program { init = init, view = view, update = update, subscriptions = always Sub.none }