Skip to content

Instantly share code, notes, and snippets.

View emdeeeks's full-sized avatar

Gareth Griffiths emdeeeks

View GitHub Profile
@brianwisti
brianwisti / vimrc.rb
Created November 26, 2013 19:05
Terrible idea: vimrc in Ruby
# A terrible idea
# Spent a couple minutes experimenting with the idea of using Ruby to initialize
# my Vim session instead of something reliable, like a .vimrc.
#
# Why? Because I love terrible ideas.
# BTW - only works for the simplest of options right now.
class VimSession
include VIM
def initialize &options
@lleger
lleger / subscription.rb
Created October 28, 2013 04:27
**Simple Stripe Subscription Concern** To use, simply drop this in `app/models/concerns`, include it in the model you want to search (`include Subscription`) and declare the `plan_for_stripe` and `description_for_stripe` helper methods. Use `save_with_payment` in place of `save` when creating a new resource with billing details; similarly, use `…
module Subscription
extend ActiveSupport::Concern
included do
attr_accessor :billing_card_token
end
def save_with_payment
if valid?
customer = ::Stripe::Customer.create(email: email,
@nthj
nthj / expirable.rb
Created October 24, 2013 06:03
Expirable Concern for Rails
# Expirable is a module that lets you easily cache
# groups of records, either across an entire record:
# cache(Snippet) => cache(Snippet.cache_key)
# or, across a scope:
# cache(page.blocks) => cache(page.blocks.cache_key)
#
# Why not just use `maximum(:updated_at)`?
# Because it requires 2 queries: the total count,
# and the updated_at timestamp
@RudolfHattenkofer
RudolfHattenkofer / resourceable.rb
Created October 7, 2013 07:52
Resourceable module for Rails ActiveRecord-Model controllers
module Resourceable
protected
def create_response( saved, resource, format, name, url = false )
if saved
format.html { redirect_to url ? url : resource, notice: name + ' was successfully created.' }
format.json { render action: 'show', status: :created, location: resource }
else
render_error( format, 'new' )
end
@richardplatel
richardplatel / fsh.rb
Created June 18, 2013 03:43
Fake shell in Ruby
require 'fileutils'
class Fsh
def cmd(c)
# parse a command line and call appropriate function, returning output
c.match /^(\w+)\s*(.*)/ # only handles one argument
cmd = $1
arg = $2
case cmd
@nathankleyn
nathankleyn / ruby-events testing
Created April 30, 2013 09:38 — forked from mqu/ruby-events testing
Updating event fire method to fire event on the @piece. This Gist is for https://github.com/nathankleyn/ruby_events/issues/3.
#!/usr/bin/ruby
# coding: utf-8
# author : Marc Quinton, march 2013, licence : http://fr.wikipedia.org/wiki/WTFPL
libdir = 'lib'
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
require 'pp'
@romansklenar
romansklenar / README.md
Last active July 11, 2023 23:12
Rails concerns

What is it?

Collection of concerns for your Rails application

Installation

Copy to your app/models/concerns directory

@davetapley
davetapley / range_overlaps.rb
Created December 5, 2012 00:58
Extending Range with overlaps?
class Range
def overlaps?(other)
(self.first <= other.last) and (other.first <= self.last)
end
end
@acook
acook / keypress.rb
Created December 2, 2012 18:42
Read keypresses from user in terminal, including arrow keys using pure Ruby. This has since been folded into a much more robust gem called Remedy. https://rubygems.org/gems/remedy & https://github.com/acook/remedy
require 'io/console'
# Reads keypresses from the user including 2 and 3 escape character sequences.
def read_char
STDIN.echo = false
STDIN.raw!
input = STDIN.getc.chr
if input == "\e" then
input << STDIN.read_nonblock(3) rescue nil
@acook
acook / termsize.rb
Created December 2, 2012 17:31
Getting the terminal size in Ruby
# via http://www.megasolutions.net/ruby/Getting-the-size-of-the-terminal-in-a-portable-way-26006.aspx
TIOCGWINSZ = 0x40087468
def get_winsize
str = [0, 0, 0, 0].pack('SSSS')
if STDIN.ioctl(TIOCGWINSZ, str) >= 0
rows, cols, xpixels, ypixels = str.unpack("SSSS")
p rows, cols, xpixels, ypixels
else
puts "Unable to get window size"