Skip to content

Instantly share code, notes, and snippets.

@rmosolgo
rmosolgo / graphql_state_machine.rb
Created June 2, 2016 13:32
Return an error message in a GraphQL field with graphql-ruby
# This object returns the _new_ state and any error message during the transition.
# So in the response, you could check for the presence of an error message.
# (In the case of an error, presumably the state would not change.)
StateMachineTransitionType = GraphQL::ObjectType.define do
name "StateMachineTransition"
field :error_message, types.String
field :state, types.String # Could also define StateMachineStateEnum
end
MutationType = GraphQL::ObjectType.define do

Keybase proof

I hereby claim:

  • I am rmosolgo on github.
  • I am rmosolgo (https://keybase.io/rmosolgo) on keybase.
  • I have a public key whose fingerprint is 77C7 1234 2A6A C5E7 CF1F 2F1E 949B E1E2 5F48 E7AE

To claim this, I am signing this object:

@rmosolgo
rmosolgo / func_bench.rb
Last active October 3, 2024 05:03
Calling a pure function in Ruby (benchmark)
require "benchmark/ips"
# Let's say you want pure functions in Ruby.
# Which form of "packaging" is the fastest?
pure_func_proc = -> (a, b) { a + b }
pure_func_lambda = lambda { |a, b| a + b }
def pure_func_global(a, b)
a + b
printerName:DYMO LabelWriter 450 Turbo
printParamsXml:<LabelWriterPrintParams><Copies>1</Copies><TwinTurboRoll>Auto</TwinTurboRoll></LabelWriterPrintParams>
labelXml:<?xml version="1.0" encoding="utf-8"?>
<DieCutLabel Version="8.0" Units="twips">
<PaperOrientation>Landscape</PaperOrientation>
<Id>LargeShipping</Id>
<PaperName>30256 Shipping</PaperName>
<DrawCommands>
<RoundRectangle X="0" Y="0" Width="3331" Height="5715" Rx="270" Ry="270"/>
</DrawCommands>
@rmosolgo
rmosolgo / etag_middleware.rb
Created February 1, 2016 16:00
ETag idea for GraphQL + Ruby
# An untested idea to use objects' `update_at` properties to determine
# whether the query response has changed since a given time.
#
# Requires some setup, you must pass the `updated_at` from the incoming ETag into the query:
#
# ```
# # The `context` object itself is read-only, so use a nested hash, which may be modified during query execution:
# etags = {updated_at: last_etag}
#
# result = MySchema.execute(query_string, context: {etags: etags})
@rmosolgo
rmosolgo / passbook.txt
Last active October 8, 2015 18:50
Passbook flow
_____ ___ __ __ ____ _____ _____ __ __
||_// ||=|| (( (( ||=) (( )) (( )) ||<<
|| || || \_)) \_)) ||_)) \\_// \\_// || \\
_____ _____
|- == | .-,( ),-. |-----|
|Web | .-( )-. | iOS |
| App| ( Apple Server ) | |
| | '-( ).-' |-----|
'-----' '-.( ).-' `--°--'
@rmosolgo
rmosolgo / key-saving.js
Last active August 29, 2015 14:24
save key-by-key
// - Request saves on a key-by-key basis
//
// - After the operation, record whether keys are now persisted
// or whether they errored out
//
// - Store keys in a workflow:
// _requestedKeys -> requested, but not in flight or successfully saved
// _pendingKeys -> keys whose save is in flight now
// _successKeys -> keys whose save was successful
// _errorKeys -> keys whose save errored
@rmosolgo
rmosolgo / loader.js
Created January 18, 2015 16:01
Use the native loading indicator with turbolinks
var frame;
var frameSrc = "/sleep" // some local endpoint that just `sleep 2` or something
function addFrame() {
frame = document.createElement("iframe")
frame.style.display = "none"
document.head.appendChild(frame)
frame.src = frameSrc
}
@rmosolgo
rmosolgo / components_new_view.coffee
Created August 10, 2014 18:46
Batman.js and mass file upload with drag and drop
class Funzies.ComponentsNewView extends Batman.View
constructor: ->
super
@set 'pendingComponents', new Batman.Set
@set 'defaultKind', null
@set 'newFiles', null
@observe 'newFiles', (nv, ov) ->
if nv?.length
@_componentsFromFiles(nv)
@rmosolgo
rmosolgo / tasks_controller.coffee
Created July 20, 2014 21:40
Initialize a batman.js model with a hasMany child
class MyApp.TasksController extends MyApp.ApplicationController
new: ->
task = new MyApp.Task
task.get('task_entries').build(item: new MyApp.Item)
@set('task', task)
# /tasks/new will be rendered implicitly. MyApp.TasksNewView will be used to render that template, if it is defined.