Skip to content

Instantly share code, notes, and snippets.

View jgaskins's full-sized avatar

Jamie Gaskins jgaskins

View GitHub Profile
@jgaskins
jgaskins / render_promise.rb
Created June 28, 2018 21:03
Rendering a promise with Clearwater
require 'clearwater/black_box_node'
require 'clearwater/component'
require 'clearwater/application'
class RenderPromise
include Clearwater::BlackBoxNode
DEFAULT = proc { Clearwater::Component.span }
Wrapper = Struct.new(:render).include(Clearwater::Component)
@jgaskins
jgaskins / app.rb
Last active August 15, 2020 02:34
API app with Roda
require 'authentication'
class MyApp < Roda
include Authentication
plugin :json
route do |r|
r.on('docs') { r.run Docs }
@jgaskins
jgaskins / gist:8230fbc0cd644203245be1ba40b1375b
Created April 21, 2018 01:33
Optimized inheritance-tree walking for Opal Class#===
Opal.is_a = function(object, self) {
if(!object.$$class) { return false }
var klass = object.$$class;
var modules = klass.$$inc, index;
while(klass !== Opal.BasicObject) {
if(klass === self) { return true }
for(index = 0; index < modules.length; index++) {
if(modules[index] === self) { return true }
@jgaskins
jgaskins / component.jsx
Created February 3, 2018 20:07
JSX vs Nested calls
// Interpolation requires curly braces, which can make things looks much uglier
const ListJSX = ({ things }) => (
<ul>
{
things.map(thing => (
<li>{ thing.name }</li>
))
}
</ul>
)
@jgaskins
jgaskins / thing.html
Created February 2, 2018 15:02
Stimulus vs jQuery
<div class="hello" data-controller="hello">
<input id="hello-name" data-target="hello.name" type="text" />
<button id="hello-greet" data-action="click->hello#greet">Greet</button>
<span id="hello-output" data-target="hello.output"></span>
</div>
@jgaskins
jgaskins / ams_vs_primalize.rb
Created February 1, 2018 21:46
Primalize vs ActiveModel::Serializer
require 'active_model'
require 'benchmark/ips'
require 'active_model_serializers'
require 'primalize'
require 'pp'
class Model
include ActiveModel::Model
def read_attribute_for_serialization key
@jgaskins
jgaskins / tree_shake.rb
Last active November 21, 2019 03:45
Tree-shaking for compiled output from the Opal compiler
#!/usr/bin/env ruby
require 'set'
def get_stubs code
code
.scan(/\.add_stubs\(.*\)/)
.map { |call| call.scan(/"\$?(\w+)"/) }
.flatten
.to_set
end
@jgaskins
jgaskins / class.rb
Last active February 16, 2019 13:28
Class declarations in Ruby with lexical scopes vs closures
require 'primalize'
# Because TicketOwnerSerializer is a constant, it is available
# anywhere in the app. We could nest it inside of TicketSerializer,
# but it has to exist before TicketSerializer's attributes declaration,
# so it would push that down.
class TicketOwnerSerializer < Primalize::Single
attributes(
id: string,
name: string,
@jgaskins
jgaskins / renderable.rb
Last active February 16, 2019 13:30
"Functional" components in Clearwater
require 'clearwater/component'
# Renderable is just a component factory that makes heavy
# use of Ruby metaprogramming
module Renderable
include Clearwater::Component
# Return a Struct-like component class. It isn't an Enumerable like Structs
# are, but arguments to the factory methods become methods on instances of
# the component.
@jgaskins
jgaskins / auditable.rb
Created September 30, 2017 17:39
Data provenance with Neo4j.rb
module Auditable
def audit model_name: :audits, method_name: :audit
define_method method_name do |info={}|
from = changed_attributes.to_h
to = from.each_key.each_with_object({}) do |key, hash|
hash[key] = self[key]
end
@__current_audit = {
from: from,