Skip to content

Instantly share code, notes, and snippets.

@sshaw
sshaw / convert-phone-number.rb
Last active February 26, 2017 02:53
Convert a Vanity Phone Number With Letters to One With Numbers Only
# https://gist.github.com/sshaw/29d6f7379771e3b4596e228b626bcf9a
def convert(chr)
chr = chr.upcase
# subtract "A"
n = (chr.ord - 65) / 3
# account for #7 & #9 which have 4 chars
n -= 1 if chr == "S".freeze || chr == "V".freeze || chr >= "Y".freeze
(n + 2).to_s
end
@sshaw
sshaw / template.rb
Created November 17, 2016 04:16
Class for rendering ERB templates using "local variable style" in templates
require "erb"
class Template
def initialize(template)
@__template = ::ERB.new(template, nil, "-")
end
def render(__vars = {})
__b = binding
__names = []
@sshaw
sshaw / to_pluralizer.rb
Last active September 10, 2016 23:37
String#to_pluralizer: Create a pluralization Proc from a String
require "active_support/core_ext/string/inflections"
class String
def to_pluralizer
->(*a) { pluralize(*a) }
end
end
__END__
CUSTOMER = "customer".to_pluralizer
@sshaw
sshaw / refills_flash.rb
Last active July 27, 2016 23:17
Ruby helper to render Bourbon/Neat Refills style flash messages
# Helper to render Bourbon/Neat Refills style flash messages
# https://gist.github.com/sshaw/bc27941c20f0adbfa5b5c9c57da095d7
module RefillsFlash
FLASH_KEYS = [:success, :notice, :error, :alert]
def flash_messages
FLASH_KEYS.inject("") do |html, name|
if flash[name]
html << content_tag(:div, :class => "flash-#{name}") {
content_tag :span, flash[name]
@sshaw
sshaw / erb_test_fixture.rb
Last active March 9, 2024 02:32
Ruby module that allows you use ERB in your test/spec fixtures and easily load them
require "erb"
require "shellwords"
#
# Use ERB in your test fixtures and easily load them.
#
# By: Skye Shaw (https://github.com/sshaw)
# Date: 2016-06-30
# Source: https://gist.github.com/sshaw/f9bad743bb53d2439501d03fb6056a4c
#
@sshaw
sshaw / query_source.rb
Last active June 27, 2016 01:14
Make debugging a bit easier by determining the source of ActiveRecord queries. The source of queries will be output via logger.debug().
#
# Make debugging a bit easier by determining the source of ActiveRecord queries.
# The source of queries will be output via logger.debug().
#
# By: Skye Shaw (https://github.com/sshaw)
# Date: 2014-10-24
# Source: https://gist.github.com/sshaw/05d8bdf99e4175816db858323fb2169c
#
# === Usage
#
@sshaw
sshaw / page_number.gemspec
Last active September 14, 2018 10:29
Ruby utility methods for pagination page and per page that make sure you'll always have a valid number. Use them your controllers or model or anywhere where you process page info. Moved to https://github.com/sshaw/page_number.git
Gem::Specification.new do |s|
s.name = "page_number"
s.version = "0.0.2"
s.date = "2016-09-19"
s.summary = "Utility methods for pagination page and per page that make sure you'll always have a valid number."
s.description =<<-DESC
Utility methods for pagination page and per page that make sure you'll always have a valid number.
Use them your controllers or model or anywhere where you process page info.
DESC
s.authors = ["Skye Shaw"]
@sshaw
sshaw / iterm2_image.rb
Last active July 28, 2016 20:48
Ruby class & program to take an image and output it using an iTerm2 escape sequence. Supports all of the iTerm2 image protocol.
require "base64"
#
# Take an image and output it using an iTerm2 escape sequence.
# Supports all of the iTerm2 image protocol: https://iterm2.com/documentation-images.html
#
# https://gist.github.com/sshaw/3687e4acdc2e1e9080d3dff04474e4e5
module ITerm2
class Image
@sshaw
sshaw / greeting.liquid
Last active October 3, 2019 09:37
"Random" Greeting in a Shopify Liquid Template
{% assign name = "sshaw" %}
{% assign greetings = "Hi %s!|Oi %s!|¡Hola %s!" | split: "|" %}
{% assign index = "now" | date: "%s" | modulo: greetings.size %}
{{ greetings[index] | replace_first: '%s', name }}
@sshaw
sshaw / request-bin.el
Last active April 30, 2016 04:33
Interactive Emacs function to create a RequestBin on http://requestb.in/
;;; request-bin.el --- Create a RequestBin on http://requestb.in/
;; Author: Skye Shaw <skye.shaw@gmail.com>
;; Version: 0.0.1
;; Keywords: tools
;; URL: https://gist.github.com/19d1e0d85eadd555c178ae1754cc6211
;; This file is NOT part of GNU Emacs.
;;; License: