Skip to content

Instantly share code, notes, and snippets.

Avatar

Adam Cuppy acuppy

View GitHub Profile
@acuppy
acuppy / Hash required key assertions
Created Dec 28, 2010
Hash extension to add addition assertions
View Hash required key assertions
class Hash
# Confirms that a key is set in the Hash
def assert_required_keys(*required_keys)
missing_keys = []
[required_keys].flatten.each { |k| missing_keys << k unless keys.has_key?(k) }
raise(ArgumentError, "Missing required key(s): #{missing_keys.join(", ")}") unless missing_keys.empty?
end
# Confirms that a key is present and that is has value
@acuppy
acuppy / Unless Helpers
Created Jan 24, 2011
Adds the link_to_remote_if/unless helpers to a Rails application
View Unless Helpers
module ActionView
module Helpers
module PrototypeHelper
def link_to_remote_if(condition, name, options = {}, html_options = nil)
condition ? link_to_remote(name, options, html_options) : name
end
def link_to_remote_unless(condition, name, options = {}, html_options = nil)
!condition ? link_to_remote(name, options, html_options) : name
end
View backup.rake
## rake file lib/tasks/backup.rake
desc "Backup Everything Specified in config/backup.yml"
task :backup => [ "backup:db", "backup:push"]
namespace :backup do
RAILS_APPDIR = RAILS_ROOT.sub("/config/..","")
def interesting_tables
@acuppy
acuppy / string.map_concat.coffee
Last active Dec 20, 2015
String#mapConcat: splits a string by character; iterates through each character; runs a callback on each character; and concat the callback return into a new string
View string.map_concat.coffee
String.prototype.mapConcat = (args...) ->
separator = args.slice(0,1) if $.type(args[0]) == 'string' or $.type(args[0]) == 'regexp'
separator or= ''
chars = @.split(separator)
rtn = []
for char, i in chars
rtn.push(args[0].call(@, char, i) or char)
rtn.join('')
@acuppy
acuppy / string.array_replace.coffee
Last active Dec 20, 2015
String#arrayReplace: similar to String#replace, but allows you to pass multiple array sets for replacement.
View string.array_replace.coffee
String.prototype.arrayReplace = (args...) ->
rtn = @
for set in args
rtn = rtn.replace(new RegExp(set[0], 'gi'), set[1])
rtn
@acuppy
acuppy / url_patterns.rb
Created Jul 21, 2013
Remote URL (ex: http://example.com) and Local URL (ex: C:\\path\to\file or /path/to/file.txt)
View url_patterns.rb
REMOTE_URL_PATTERN = /\A(?:(?:http|https):\/\/[a-z0-9]+(?:[\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(?:\:[0-9]{1,5})?).*\z/ix
LOCAL_URL_PATTERN = /\A(?:[a-zA-Z]{1,2}\:\\\\?).*\z/
@acuppy
acuppy / document_types.rb
Created Jul 21, 2013
All major document mime types (Microsoft Office, Apple iWork, Adobe PDF) as a Ruby Module
View document_types.rb
module DocumentFileTypes
module Microsoft
WORD = %w(
application/msword
application/vnd.openxmlformats-officedocument.wordprocessingml.document
application/vnd.openxmlformats-officedocument.wordprocessingml.template
application/vnd.ms-word.document.macroEnabled.12
application/vnd.ms-word.template.macroEnabled.12
)
@acuppy
acuppy / paginate_array.rb
Created Jul 21, 2013
Array#paginate: adds pagination functionality using the WillPaginate gem to a Ruby Array
View paginate_array.rb
require 'will_paginate'
class Array
def paginate(all = nil, options = {})
options[:page] = (options[:page].to_i == 0) ? 1 : options[:page].to_i
options[:per_page] = (options[:per_page].to_i == 0) ? 30 : options[:per_page].to_i
pagination_array = WillPaginate::Collection.new(options[:page], options[:per_page], self.size)
start_index = pagination_array.offset
end_index = start_index + (options[:per_page] - 1)
array_to_concat = self[start_index..end_index]
@acuppy
acuppy / aspell_spell_checker.rb
Last active Dec 20, 2015
Simple abstraction layer to interact with the UNIX aspell library via Ruby
View aspell_spell_checker.rb
module Spelling
require 'net/https'
require 'uri'
require 'rexml/document'
ASPELL_WORD_DATA_REGEX = Regexp.new(/\&\s\w+\s\d+\s\d+(.*)$/)
ASPELL_PATH = "aspell"
#
# @param String: spell_check_text - represents the source string
@acuppy
acuppy / object_true_false.rb
Last active Dec 20, 2015
Object#true? and Object#false? boolean assertions
View object_true_false.rb
class Object
def true?
self.respond_to?( :to_i ) ? !self.to_i.zero? : true
end
def false?
!true?
end
end