Skip to content

Instantly share code, notes, and snippets.

View stephaneliu's full-sized avatar

Stephane Liu stephaneliu

  • Hawaii
  • 12:56 (UTC -10:00)
View GitHub Profile
class Event < ApplicationRecord
# Before
def cateorized?
event.category.present? ? event.category.name != 'none' : false
end
# Refactor - simplify with Null test with AND operator
def categorized?
event.category && event.category != 'none'
end
# Before
class CustomQuery
def levels
@levels ||= { 'a' => :low, 'b' => :med, 'c' => :high }
end
def current(level)
levels.each_with_index { |avail_level, index| return avail_level[index] if level == avail_level[index] }
:low
end
@stephaneliu
stephaneliu / DO
Last active August 29, 2015 14:08
Error
#!/usr/bin/env ruby
require 'barge'
require 'delegate'
class DigitalOcean
attr_reader :client, :droplets, :sizes
def initialize
@client = Barge::Client.new(access_token: ENV['DO_KEY'])
end
- Get gist XXXXX and add to clipboard. >
:Gist -c XXXXX
<
@stephaneliu
stephaneliu / gist:895b4a222b4cbe5a13ce
Last active August 29, 2015 14:01
JK remap for readline
# .inputrc
set editing-mode vi
# http://nocruft.com/post/22528784313/avoiding-the-esc-key-in-vim-and-readline
$if mode=vi
set keymap vi-command
"ii": vi-insertion-mode
set keymap vi-insert
"jk": vi-movement-mode
@stephaneliu
stephaneliu / gist:7981017
Last active December 31, 2015 11:39
Ruby statistics
require 'descriptive-statistics'
class StdDev
attr_reader :stats
def initialize(dataset)
# comment
@stats = DescriptiveStatistics::Stats.new(dataset)
end
@stephaneliu
stephaneliu / gist:6735433
Last active December 24, 2015 03:09
Git pre-commit hook to alert committer that spec file contains focus
#!/usr/bin/env ruby
spec_hits = []
# Find the names of all the filenames in spec directory that have been (A)dded (C)opied or (M)odified
filenames = `git diff --cached --name-only --diff-filter=ACM`.split("\n")
filenames.each do |filename|
if filename.end_with? '_spec.rb'
results = `git diff --cached #{filename} | grep \^+\[\^+\] | grep focus`
module ActsAsDtg
def acts_as_dtg(*attributes)
attributes.each do |attribute|
dtg_attribute = "#{attribute}_dtg"
attr_reader dtg_attribute
define_method("#{dtg_attribute}=") do |val|
instance_variable_set("@#{dtg_attribute}", val)
puts "#{dtg_attribute} was set"
end
end
@stephaneliu
stephaneliu / pre-commit
Created December 2, 2012 03:47
Pre commit git hook to check for focus in spec directory
#!/usr/bin/env ruby
spec_hits = []
# Find the names of all the filenames in spec directory that have been (A)dded (C)opied or (M)odified
filenames = `git diff --cached --name-only --diff-filter=ACM spec`.split("\n")
filenames.each do |filename|
# Filter all the additions to this file, find if they contain `focus: true` or `:focus => true` and store these lines without the initial `+` and spaces
results = `git diff --cached #{filename} | grep \^+\[\^+\] | grep focus`