Skip to content

Instantly share code, notes, and snippets.

@sshaw
sshaw / gist:386070
Created May 1, 2010 05:29
Rails current_page? Style Referer Function
# Usage:
# referer? :controller => 'beezies', :action => 'holla', :type => 'popozuda'
# referer? %r|/\d+/edit|
def referer?(options)
referer = request.headers["Referer"]
return (options.blank? ? true : false) if referer.blank?
if options.is_a?(Regexp)
referer =~ options
@sshaw
sshaw / Makefile.PL
Created March 27, 2011 08:32
Makefile.PL postamble() - Create README.pod for Perl packages uploaded to GitHub
# Create README.pod for a repo's GitHub page. Unlike CPAN, GitHub won't
# display the module's POD, it looks for a README.*
use Config;
sub MY::postamble
{
my $self = shift;
return if -r 'README' or ! -r $self->{VERSION_FROM};
return<<END_MAKE;
README.pod: $self->{VERSION_FROM}
\@$Config{bin}/perldoc -uT $self->{VERSION_FROM} > README.pod
@sshaw
sshaw / random_record.rb
Created July 15, 2011 02:51
Rails 3 - select random record(s)
module RandomRecord
def random
order(random_function_name)
end
private
def random_function_name
if !defined? @__random_function_name
@__random_function_name = "rand()"
begin
@sshaw
sshaw / form_builder_with_cancel.rb
Created July 26, 2011 08:21
FormBuilderWithCancel < Formtastic::SemanticFormBuilder
# - f.buttons do
# = f.commit_button
# = f.cancel
# Need to make this v2 like by using module...
class FormBuilderWithCancel < Formtastic::SemanticFormBuilder
def cancel(*args)
options = args.extract_options!
options[:class] ||= "cancel"
options[:label] ||= (args.shift || "Cancel")
@sshaw
sshaw / gist:1190047
Last active March 20, 2016 03:03
Ruby String class extensions for checking file permissions
class String
MODES={"r" => :readable?, "w" => :writable?, "x" => :executable?}
def permission?(file)
self.split(//).inject(true) { |can, m| can && MODES.include?(m) && File.send(MODES[m], file) }
end
end
"rw".permission?("Makefile.PL")
@sshaw
sshaw / gist:1342663
Last active March 20, 2016 03:02
Ruby Pathname extensions to check for parent and subdirectory: Pathname.under? and Pathanme.above?
class Pathname
def under?(path)
parent, child = [ Pathname.new(path), self ].inject([]) { |a, p| a << p.expand_path.to_s.split(File::SEPARATOR) }
parent.drop_while { |p| p == child.shift }.none? && child.any?
end
def above?(path)
Pathname.new(path).under? self
end
end
@sshaw
sshaw / gist:1410732
Created November 30, 2011 20:43
Harris Timecode
class HarrisTimecode
attr_accessor :hours, :minutes, :seconds, :frames, :encoded
def initialize(encoded)
raise ArgumentError, "Encoded duration invalid: #{encoded}" unless encoded.respond_to?(:to_i)
hex = "%08x" % encoded.to_i
@hours, @minutes, @seconds, @frames = [0,2,4,6].map { |n| hex[n, 2] }
@encoded = encoded
end
def to_s
@sshaw
sshaw / gist:1604295
Created January 13, 2012 02:33
Custom Registry Settings
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced]
"HideFileExt"=dword:00000000
; Show hidden and system files
"Hidden"=dword:00000001
"ShowSuperHidden"=dword:00000001
"SuperHidden"=dword:00000001
; In Explorer, do not open folders in new window
"SeparateProcess"=dword:00000000
@sshaw
sshaw / Makefile.PL
Created January 16, 2012 20:23
Use test libs without all the `use lib` calls
# Include test helpers and/or libs
sub MY::test
{
package MY;
my $make = shift->SUPER::test(@_);
$make .= 'FULLPERLRUN = $(FULLPERL) -I t -I t/lib';
return $make;
}
@sshaw
sshaw / 00run.t
Created January 21, 2012 00:55
Test::Class Class Filter
use Test::Class::Load 't/lib';
#
# I feel that there's a better way to do this.
# Also if no tests run then no plan is emitted, shouldn't Test::Class deal with this?
#
# Regexes are not validated.
#
if($ENV{TEST_CLASS}) {