Skip to content

Instantly share code, notes, and snippets.

View jgaskins's full-sized avatar

Jamie Gaskins jgaskins

View GitHub Profile
@jgaskins
jgaskins / mappers.rb
Last active September 17, 2015 04:17
Perpetuity associations
require 'perpetuity/postgres'
require 'time' # Gets around a bug I found while writing this.
require 'pp'
module AttributeAssignment
def initialize attributes={}
attributes.each do |attr, value|
instance_variable_set "@#{attr}", value
end
end
@jgaskins
jgaskins / router.rb
Created October 4, 2015 07:03
Brainstorm for Clearwater routing callbacks
class Router
def navigate_to path
current_targets = targets_for_path(current_path)
new_targets = targets_for_path(path)
(current_targets - new_targets).each do |from_target|
if from_target.respond_to?(:will_transition_from)
from_target.will_transition_from
end
end
(new_targets - current_targets).each do |to_target|
@jgaskins
jgaskins / foo.rb
Created October 5, 2015 03:51
Widgets with Clearwater
require 'clearwater/component'
require 'google_map_container'
class Foo
include Clearwater::Component
def render
GoogleMapContainer.new(
height: 500,
width: 500,
@jgaskins
jgaskins / google_map_renderer.rb
Last active October 8, 2015 02:39
Using virtual-dom hooks to render a Google Map in Clearwater
require 'browser/animation_frame'
require 'hook'
class GoogleMapRenderer
include Hook
def initialize(latitude: 0, longitude: 0, zoom: 13)
@latitude = latitude
@longitude = longitude
@zoom = zoom
@jgaskins
jgaskins / benchmark.rb
Created October 20, 2015 02:40
Opal vs plain JS performance comparison
require 'opal'
def fib n
return n if n < 2
fib(n - 1) + fib(n - 2)
end
%x{
var fib = function(n) {
@jgaskins
jgaskins / model.rb
Created October 29, 2015 22:24
Immutable models in Ruby
require 'set'
class Model
def self.attributes *attrs
@attributes ||= Set.new
attr_reader *attrs
@attributes += attrs
end
def initialize attributes={}
@jgaskins
jgaskins / dispatcher_driver.rb
Created November 1, 2015 16:26
GrandCentral deep nesting w/ cache invalidation
require 'clearwater/component'
require 'clearwater/cached_render'
module Dispatcher
class Driver
include Clearwater::Component
include Clearwater::CachedRender
attr_reader :driver, :orders
@jgaskins
jgaskins / application.rb
Created November 1, 2015 22:50
Clearwater todo-list app with cached rendering
require 'opal'
require 'clearwater'
require 'clearwater/cached_render'
require 'grand_central'
class Todo
attr_reader :id, :description, :timestamp
def initialize attributes={}
@id = attributes.fetch(:id) { rand(1 << 31) }
@jgaskins
jgaskins / component.rb
Created December 8, 2015 22:30
Clearwater inline styles
class MyComponent
include Clearwater::Component
attr_reader :market, :detail_view
def initialize(market, detail_view: nil)
@market = market
@detail_view = detail_view
end
@jgaskins
jgaskins / route.rb
Created December 15, 2015 03:10
Graceful degradation from pushState routing to fragment-based routing
class Router
def initialize
if `window.location.pushState === undefined`
@location = FragmentLocation.new
if !has_fragment?
@location.copy_path_to_fragment!
end
else
@location = PushStateLocation.new