Skip to content

Instantly share code, notes, and snippets.

View jaredcwhite's full-sized avatar
🌲

Jared White jaredcwhite

🌲
View GitHub Profile
@jaredcwhite
jaredcwhite / sourced_locally.rb
Last active November 30, 2020 19:53
Monkeypatch to find Ruby methods defined within local source tree
module InstanceMethodsSource
refine Object do
def self.instance_methods_sourced_locally
meths = instance_methods
meths_hash = Hash.new
meths.each do |meth|
soc = instance_method(meth).source_location
if !soc.nil? && soc[0].start_with?(Dir.pwd)
meths_hash[meth] = soc
end
@jaredcwhite
jaredcwhite / nav.html
Created December 14, 2020 20:05
Markup with custom elements, inline styles + CSS variables
<nav style="margin: var(--gap-3) 0">
<navbar-inner>
<sl-flex nowrap align-items="center" spaced="between">
<sl-flex-item size="6/10 xs:2/10 sm:3/10 md:4/10 lg:5/10">
<a href="/" style="background: var(--primary-color)">
<img alt="Whitefusion" src="/images/whitefusion-w-transparent.png" width="60">
</a>
</sl-flex-item>
<sl-flex-item>
<a href="/spec">Tech Specs</a>
@jaredcwhite
jaredcwhite / README.md
Last active April 21, 2024 14:28
Use Shoelace Form Components with Hotwire Turbo

This is a Stimulus controller which will allow a Shoelace Form component to be processed and submitted by Turbo.

Just wrap your <sl-form> control with a form tag:

<%= form_with url: "/test-submit" do %>
  <sl-form data-controller="shoelace-form">
    controls go here
  </sl-form>
&lt;% end %&gt;
@jaredcwhite
jaredcwhite / test_punning.rb
Created January 7, 2021 01:04
Custom object destructuring in Ruby 3
class TestPunning
attr_accessor :x, :y
def deconstruct_keys(keys)
values = {}
keys.each do |key|
values[key] = send(key)
end
values
end
@jaredcwhite
jaredcwhite / hash_for_locals.rb
Created January 7, 2021 01:34
Get a hash where key names match local variable values
class Binding
def hash_for_locals(*keys)
values = {}
keys.each do |key|
values[key] = local_variable_get(key)
end
values
end
end
@jaredcwhite
jaredcwhite / set_ivars.rb
Last active January 7, 2021 01:51
Import keyword argument values into instance variables
def set_ivars_from_keys(local_binding, *keys)
keys.each do |key|
instance_variable_set :"@#{key}", local_binding.local_variable_get(key)
end
end
def test(a:, b:)
set_ivars_from_keys binding, :a
end
@jaredcwhite
jaredcwhite / semantic-markup.html
Last active January 8, 2021 05:01
Tailwind HTML Gzip sizes
<!-- 538 bytes gzipped -->
<!-- (and that's before cleaning up all the *unnecessary divs* !! -->
<div class="toast">
<div class="toast-inset">
<div class="toast-rounded">
<div class="toast-padding">
<div class="toast-flex">
<div class="toast-content__icon">
<svg class="text-green-400" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
@jaredcwhite
jaredcwhite / datauri_builder.rb
Created January 21, 2021 21:20
Embed PNG images as data URIs right in your Bridgetown website
# Use in Liquid or ERB templates like this:
#
# Liquid: <img src="{% datauri myimage.png %}">
# ERB: <img src="<%= datauri "myimage.png" %>">
class DatauriBuilder < SiteBuilder
def build
liquid_tag "datauri", :datauri
helper "datauri", :datauri
end
@jaredcwhite
jaredcwhite / settings.json
Created February 22, 2021 18:39
File Watcher setup for the Bridgetown repo
{
"filewatcher.commands": [
{
"match": "test/test_.*\\.rb",
"isAsync": true,
"cmd": "/bin/zsh --login -c \"cd ${fileDirname}; cd ..; SKIP_COV=true script/test ${file}\" && echo 'Test run completed.'",
"event": "onFileChange"
}
]
}
@jaredcwhite
jaredcwhite / node-hi-ruby.rb
Last active April 6, 2021 21:38
Easiest way to run a Node script from Ruby?!
require "json"
def execjs(js)
IO.popen(["node", "--input-type=module"], "r+") do |pipe|
pipe.puts js
pipe.close_write
pipe.read
end
end