Skip to content

Instantly share code, notes, and snippets.

View RomanTurner's full-sized avatar
🤫

Roman RomanTurner

🤫
View GitHub Profile
@RomanTurner
RomanTurner / event_base.rb
Created April 7, 2024 23:44
Ruby Simple Event as State Machine
module Event
class Base < ActiveRecord::Base
self.table_name = 'events'
# Callbacks to enforce state transitions logic
before_update :validate_status_transition
enum status: {
enqueued: 0,
processing: 1,
@RomanTurner
RomanTurner / response_values.rb
Last active February 19, 2024 03:44
structs to model values in our domain
module PaymentProcessor
module Response
class ResponseStruct < Struct
def self.build(args)
return if args.blank?
# this filters the fields from the response that we don't need or want.
# but it also keeps the kwargs strict.
new(args.slice(*members)
end
@RomanTurner
RomanTurner / pattern_matching.rb
Last active February 1, 2024 16:14
Ruby Pattern Matching
class Response
attr_reader :status, :data
def initialize(status, data)
@status = status
@data = data
end
def deconstruct
by_status
@RomanTurner
RomanTurner / haml_components.rb
Created November 18, 2023 05:04
View Components- With Haml 😅
require "haml"
module Hamlet
def self.included(base)
base.extend ClassMethods
end
def render
engine = Haml::Template.new { self.class.get_element }
engine.render(self)
@RomanTurner
RomanTurner / vite_rephlex.rb
Created November 9, 2023 01:36
Phlex + ViteRuby
# frozen_string_literal: true
require 'phlex'
require 'vite_ruby'
module ViteRephlex
module PhlexHelpers
class OohBabyILikeItRaw < Phlex::HTML
def initialize(tag:)
@tag = tag
@RomanTurner
RomanTurner / accessor.js
Created April 27, 2023 15:45
Testing speed of lookups
// // Generate a large sorted array of random integers
const arraySize = 1500;
const amountOfRecipes = 1000;
const sortedArray = Array.from({ length: arraySize }, (_, i) => i).sort(
(a, b) => a - b
);
const jsonArray = JSON.stringify(sortedArray);
function binarySearch(arr, val) {
@RomanTurner
RomanTurner / tailwind_phlex.rb
Last active July 16, 2023 23:17
POC Tailwind + Phlex component.
class StyledComponent < Phlex::HTML
STYLES = {
container: {
xs: "mx-auto max-w-7xl px-6 py-24",
sm: "sm:py-32",
lg: "lg:px-8"
},
get_started: {
@RomanTurner
RomanTurner / roda_phlex.rb
Last active July 16, 2023 16:21
A plugin for Roda for using Phlex as the rendering library of choice.
class Roda
module RodaPlugins
module Phlex
def self.configure(app, opts)
app.opts[:class_name] = app.name
app.opts[:phlex] = opts
end
module InstanceMethods
# Render a component with the default layout.
@RomanTurner
RomanTurner / vite_rephlex.rb
Last active January 30, 2023 15:12
Tag helpers for ViteRuby using the Phlex component gem.
module ViteRephlex
module PhlexHelpers
class OohBabyILikeItRaw < Phlex::HTML
def initialize(tag:)
@tag = tag
end
def template
@tag.is_a?(Array) ? @tag.each { |t| unsafe_raw(t) } : unsafe_raw(@tag)
end
end
@RomanTurner
RomanTurner / error_serializer.rb
Last active July 13, 2022 04:37
Error Serializer
class ErrorSerializer
attr_accessor :request, :params, :errors
def initialize(request, params, errors = [], exception = nil)
@request = request
@params = params
@errors = errors
@exception = exception
end