Skip to content

Instantly share code, notes, and snippets.

View mcmire's full-sized avatar
🦊

Elliot Winkler mcmire

🦊
View GitHub Profile
getClientLocation = ->
new Promise (resolve, reject) ->
success = (position) ->
resolve(position.coords)
error = (positionError) ->
error = new Error("Error fetching client location: " + positionError.message)
reject(error)
navigator.geolocation.getCurrentPosition(success, error)

CoffeeScript: Differences from JavaScript

Semicolons

Don't write them; they'll be inserted automatically.

Functions

Replace function () { ... } with ->.

@mcmire
mcmire / single_or_multiple.rb
Created May 28, 2015 16:59
SingleOrMultiple functor (Ruby)
require "attr_extras"
class SingleOrMultiple
rattr_initialize :value
def map(&block)
result =
if multiple_values?
value.map(&block)
else
@mcmire
mcmire / 00_README.md
Last active July 25, 2016 23:15
New table matchers

Table Matchers

This gist includes a matcher, have_row, that makes it possible to assert that a table has a certain row. It also comes with a helper class, Features::Table, that makes it possible to convert a table into an array of arrays or array of hashes, or if you quickly want to find a row based the content of a cell inside it, you can do that too.

@mcmire
mcmire / limited_exposure.rb
Last active August 29, 2015 14:04
Like DecentExposure, but less magical
# This is a module that provides two methods, `expose` and `let`, which
# allow you to cleanly and clearly define lazily-evaluated, memoized
# values that you can use anywhere in your controllers and views. It's
# very similar to the decent_exposure[1] gem, but it works more simply
# (and doesn't have a default action when you don't pass a block to
# #expose, to keep things very very simple).
#
# You will want to mix this into ApplicationController.
#
# == Rationale
@mcmire
mcmire / alternate_constructor_pattern.coffee
Last active August 29, 2015 13:57
Design patterns: JavaScript vs. Ruby
# If you wanted to make a constructor that did some extra stuff
# on the newly created object before giving it back to you
# you might be tempted to write it like you would in Ruby:
class Modal
@load: ->
modal = new this
modal.load
modal
@mcmire
mcmire / table_helpers.rb
Last active December 18, 2015 23:19
Helper for use in RSpec/Capybara feature tests to find a table on a page and convert it into an array of arrays, so that you can make an assertion on it. Similar to Cucumber's #tableize but is better about stringifying cell content.
module TableHelpers
VALID_FORMATS = [:html, :text]
# Find a table on the page and convert it to an array of arrays.
#
# selector_or_node - A String CSS selector to find the table, or a
# Nokogiri::XML::Node object (if you already have a
# reference to the table).
# options - Optional hash:
# columns - An Integer or Range of Integers. Lets you
@mcmire
mcmire / enumerable_matchers.rb
Created June 25, 2013 16:40
Enumerable RSpec matchers
module EnumerableMatchers
# RSpec already lets you do this:
#
# arr.should be_any {|item| item["foo"] == 2 }
#
# However, that's kind of unwieldy to say. So we add this matcher so we
# can say one of these alternatives instead:
#
# arr.should have_any {|item| item["foo"] == 2 }
# arr.should have_an {|item| item["foo"] == 2 }
@mcmire
mcmire / hash_matchers.rb
Last active May 9, 2017 13:08
Hash RSpec matchers
module HashMatchers
def contain(other)
Matcher.new(other)
end
class Matcher
def initialize(other)
@other = other
end
@mcmire
mcmire / 00_README.md
Created October 1, 2012 21:54
Running two instances of Spork for two separate projects using Guard

Instructions:

  • Take a look at the Guardfile here and copy and paste relevant parts.
  • Install spork to script/spork
  • Install drspec to script/drspec
  • Remove --drb from your .rspec file. Using Spork has to be opt-in in order to properly customize the DRb port.
  • Install .spork-port to .spork-port. Change the port to whatever you want.