Skip to content

Instantly share code, notes, and snippets.

View thiagomajesk's full-sized avatar
💜
Happily doing some Elixir wizardry

Thiago Majesk Goulart thiagomajesk

💜
Happily doing some Elixir wizardry
  • Brazil
View GitHub Profile
@thiagomajesk
thiagomajesk / constant.ex
Created April 30, 2024 22:31
Macro-based constants in Elixir
defmodule Constant do
@moduledoc """
Allows generating dynamic functions for the given list of values.
Accepts an enumerable with the of keys and values to be generated as functions.
"""
defmacro __using__(constants) do
[define_constants(constants), define_helpers()]
end
defp define_constants(constants) when is_list(constants) do
@thiagomajesk
thiagomajesk / purge_html_css.exs
Last active August 8, 2022 20:43
Finds unused CSS classes in HTML files using Elixir
# (!) This is a first-effort/ naive script to find unused CSS in HTML files.
# This should (hopefully) work as the opposite of what you get from PURGE CSS.
# Configure the variables bellow and execute the file with `elixir purge_html_css.exs`.
# Configures the default location to look for the outputed CSS.
# Ideally this file should not be transformed to preserve new lines.
outputed_css = "priv/static/css/app.css"
# Configures the default pattern to look for files that may contain CSS to purge.
# Currently inculdes .eex and .heex files. Change this if necessary.
@thiagomajesk
thiagomajesk / hooks.js
Created April 11, 2022 03:49
LiveView Tips and Tricks
/*
* Resets a input to a value when a form is updated by LiveView.
* Add phx-hook="FormReset" to the form and phx-reset="" to the input you want to reset.
* The attribute phx-reset must contain the value that the input value will be reseted to.
*/
Hooks.FormReset = {
updated() {
let input = this.el.querySelector('[phx-reset]:not(.invalid-feedback)')
let value = input.getAttribute('phx-reset')
input.value = value
@thiagomajesk
thiagomajesk / counter_cache.ex
Created February 17, 2022 23:59
Elixir's Ecto Counter Cache Manager
defmodule CounterCache do
@moduledoc """
This module provides a way of generating embedded schemas that contain the definition of cached fields.
A module is automatically created for each and contains the definition values passed in the list.
By accessing the generated module, those fields can be queried from the schema that contains them.
# Usage
- Add `use CounterCache` to your module.
- Invoke `counter_cache_field` inside your schema definition:
@thiagomajesk
thiagomajesk / benchmark.exs
Last active July 7, 2020 18:50
XML parsing benchmark
# Dependencies: {:saxy, "~> 1.2"}, {:sax_map, "~> 0.2"}, {:benchee, "~> 1.0"}
#
# Samples
#
simple = """
<?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<catalog>
@thiagomajesk
thiagomajesk / application.ts
Last active July 3, 2019 20:24
Knockout & Typescript & Webpack bootstrap code
// Component definition (holds information about template and view model)
class ClickCounterComponent extends Component {
constructor(name: string, viewModel: ViewModelClass) {
super(name, viewModel, template)
}
}
// Actual view model (This separation allows us to reuse view models for components)
class ClickCounterViewModel extends ViewModel {
public count: ko.Observable<number> = ko.observable<number>(0);
@thiagomajesk
thiagomajesk / AutoFacModule.cs
Last active August 30, 2023 16:47
Generic Handlers & Commands for MediatR (+ AutoFac config)
public class GenericHandlersModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<CreateCommandHandler<Foo, CreateFooCommand>>().As<IRequestHandler<CreateFooCommand, bool>>();
builder.RegisterType<DeleteCommandHandler<Foo, DeleteFooCommand>>().As<IRequestHandler<DeleteFooCommand, bool>>();
builder.RegisterType<ListQueryHandler<Foo, ListFooQuery>>().As<IRequestHandler<ListFooQuery, IEnumerable<Foo>>>();
builder.RegisterType<UpdateCommandHandler<Foo, UpdateFooCommand>>().As<IRequestHandler<UpdateFooCommand, bool>>();
}
}
@thiagomajesk
thiagomajesk / AutoMapperModule.cs
Created January 25, 2018 13:05
AutoMapper configuration for AutoFac
public class AutoMapperModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.Register(c => new MapperConfiguration(config => { config.AddProfiles(GetType().Assembly); })).AsSelf().SingleInstance();
builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve)).As<IMapper>().InstancePerLifetimeScope();
}
}
@thiagomajesk
thiagomajesk / ModelStateHelper.cs
Created September 30, 2017 15:07
ASP.NET Core PRG TempData Helpers
public class ModelStateTransferValue
{
public string Key { get; set; }
public string AttemptedValue { get; set; }
public object RawValue { get; set; }
public ICollection<string> ErrorMessages { get; set; } = new List<string>();
}
public static class ModelStateHelper
{
@thiagomajesk
thiagomajesk / brunch-config.js
Last active August 8, 2017 17:30
ASP.NET Core Brunch Config (Babel + Bootstrap 4 + LESS + Font Awesome)
module.exports = {
paths: {
public: "wwwroot",
watched: ["wwwroot/javascripts", "wwwroot/stylesheets"]
},
npm: {
styles: {
"bootstrap": ["dist/css/bootstrap.css"],
"font-awesome": ["css/font-awesome.css"],
"animate.css": ["animate.css"]