Skip to content

Instantly share code, notes, and snippets.

View chrisbloom7's full-sized avatar

Chris Bloom chrisbloom7

View GitHub Profile
@chrisbloom7
chrisbloom7 / random_password.rb
Created April 24, 2012 05:47
Generate a random password containing at least one number and one special character
# Generate a password that is 8 - 20 characters in length, and which contains at least one number and one special character
# Requires Ruby >= 1.9
def random_password
specials = ((32..47).to_a + (58..64).to_a + (91..96).to_a + (123..126).to_a).pack('U*').chars.to_a
numbers = (0..9).to_a
alpha = ('a'..'z').to_a + ('A'..'Z').to_a
%w{i I l L 1 O o 0}.each{ |ambiguous_character|
alpha.delete ambiguous_character
}
characters = (alpha + specials + numbers)
@chrisbloom7
chrisbloom7 / order_test_steps.rb
Created March 20, 2012 21:15
A cucumber/capybara step to test the order of elements in a list
Then /^I should see each item listed in alphabetical order in the items section$/ do
@items.each_with_index do |item, index|
find(:xpath, "//*[@id='items']/ul[@class='items']/li[#{index+1}]").inspect.should eq(find("li#item-#{item.friendly_id}").inspect)
@item = item
step "I should see the item listed in the items section"
end
end
Then /^I should( not)? see the item listed in the items section$/ do |negation|
assertion = negation ? :should_not : :should
@chrisbloom7
chrisbloom7 / .bash_aliases
Last active June 23, 2018 00:37
A portion of my alias entries and some other useful snippets for Bash
#!/bin/sh
# GENERAL COMMANDS
alias l='ls -AHhlp'
alias c='clear'
alias cx='chmod +x'
alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"
alias reload='source ~/.bash_profile'
alias release='xattr -d com.apple.quarantine'
alias flushdns='sudo discoveryutil udnsflushcaches'
@chrisbloom7
chrisbloom7 / exception_flow.rb
Created February 3, 2012 19:36
Testing the process flow of exception handling blocks in Ruby
# A contrived example
def a (dividend, divisor)
puts "-- Entering!"
if dividend >= 5
puts "-- Dividend >= 5!"
begin
puts "-- Beginning!"
puts "-- #{dividend.to_i / divisor.to_i}"
rescue => e
puts "-- Exception! #{e}"
@chrisbloom7
chrisbloom7 / preload_sti_models.rb
Created January 31, 2012 22:17
Setting up STI subclass routes programmatically
if Rails.env.development?
# Make sure we preload the parent and children class in development
# since classes aren't pre-cached. Otherwise we get an error when
# accessing a child class before we access the parent.
%w[kase coaching_kase training_kase alpha_kase].each do |c|
require_dependency File.join("app","models","#{c}.rb")
end
end
@chrisbloom7
chrisbloom7 / cucumber.yml
Created December 23, 2011 14:48 — forked from oliverbarnes/env.rb
Cucumber setup with spork and database_cleaner
<%
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip"
%>
default: <%= std_opts %> features
wip: --drb -tags @wip:3 --wip features
autotest: --drb --color --format progress --strict
rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip
@chrisbloom7
chrisbloom7 / excercise_custom_factory_girl_steps.feature
Created December 23, 2011 09:16 — forked from blaix/factory_girl_steps.rb
A modified version of the cucumber steps included in factory_girl, which will set an instance variable based on the factory/factories being generated. Drop this file into the features/support folder and don't require the original factory_girl/step_definit
# Assumes you have a factory named :customer defined with attributes first_name and last_name
Feature: Testing customized factory_girl step definitions
As an open source advocate
I
Want to make sure that my customized step definitions don't throw any errors
Scenario: Generating factories from a table
Given the following customers exist:
| first_name | last_name |
| Bob | Smith |
@chrisbloom7
chrisbloom7 / tail_recursive.sh
Created October 25, 2011 16:45
Bash shell command to recursively monitor a directory for new and changed files
#!/bin/sh
# Recursively `tail` new and changed files
# See https://gist.github.com/1313404#file_watch_recursive.sh for usage
watch -d -n 5 'tail `find . -type f \( ! -iname ".*" ! -ipath "*.svn*" \) -newerct "'"15 minutes ago"'" -print`'
@chrisbloom7
chrisbloom7 / rspec-syntax-cheat-sheet.rb
Created July 22, 2011 00:53 — forked from dnagir/rspec-syntax-cheat-sheet.rb
RSpec 2 syntax cheat sheet by example
# RSpec 2.0 syntax Cheet Sheet by http://ApproachE.com
# defining spec within a module will automatically pick Player::MovieList as a 'subject' (see below)
module Player
describe MovieList, "with optional description" do
it "is pending example, so that you can write ones quickly"
it "is already working example that we want to suspend from failing temporarily" do
pending("working on another feature that temporarily breaks this one")
@chrisbloom7
chrisbloom7 / hash_comparison_spec.rb
Created June 29, 2011 05:54
An RSpec2 spec that demonstrates the issue reported at https://github.com/rspec/rspec-expectations/issues/81 regarding comparison of arrays of multiple hashes with different keys and/or values when using the =~ operator. See failure reports #15 and #18 in
require 'spec_helper'
describe "hash comparison tests" do
context "comparing single hashes with same keys, different values" do
let(:me) { { :foo => 'bar' } }
let(:me2) { { :foo => 'baz' } }
it "should fail gracefully when using ==" do
me.should == me2
end