Skip to content

Instantly share code, notes, and snippets.

View hopsoft's full-sized avatar

Nate Hopkins hopsoft

View GitHub Profile
@hopsoft
hopsoft / blank_slate.rb
Created January 13, 2011 14:03
Blank Slate - Illustrates how to remove functionality from a class to create a blank slate
# demonstrate how to remove functionality
String.class_eval do
undef_method :length
end
"test".length # => NoMethodError: undefined method `length' for "test":String
# create a blank slate class
class BlankSlate
public_instance_methods.each do |method_name|
undef_method(method_name) unless method_name =~ /^__|^(public_methods|method_missing|respond_to\?)$/
@hopsoft
hopsoft / gcd.rb
Created January 13, 2011 21:55
Testing the GCD system in MacRuby
def async_example(arg, delay=1.0)
Dispatch::Queue.new('org.macruby.examples.gcd').async do
start = Time.now
sleep delay
puts "finished async_work for: #{arg} in #{(Time.now - start).round} secs"
end
end
async_example(:first, 5.0)
async_example(:second, 4.0)
@hopsoft
hopsoft / scope_gate.rb
Created January 14, 2011 05:19
Scope Gate - Illustrates the three ways to define a new scope
# demonstrate scoping in ruby
scope = "global scope"
puts(scope) # => global scope
class ExampleClass
# the globally scoped variable isn't defined in the classes' scope
defined?(scope) # => nil
scope = "class scope"
puts(scope) # => class scope
end
@hopsoft
hopsoft / gist:1122080
Created August 3, 2011 07:14
Rails Environment Based Behavior
# app/models/user.rb
class User < ActiveRecord::Base
end
# lib/user_logger.rb
module UserLogger
def self.included(mod)
mod.send :after_create, :log_creation
end
@hopsoft
hopsoft / monkey_patcher.rb
Created November 29, 2011 19:02
An attempt to define a standard for applying monkey patches
# An attempt to define a standard interface for monkey patching existing method definitions
# on existing Object instances, Classes, and Modules.
#
# This effort warrants a new monkey patching nomenclature.
# * Monkey Patch - a re-definition of an existing method that was patched via MonkeyPatcher
# * Patch - a re-definition of an existing method
#
# Lets get started with some usage examples.
# First lets add some helper methods to all objects.
#
@hopsoft
hopsoft / resourceful.rb
Created November 30, 2011 21:23
Resourceful Rails actions for free with a Sinatra like DSL for action callbacks
module Resourceful
def self.actions
[:index, :show, :new, :edit, :create, :update, :destroy]
end
def self.callbacks
[:before, :after, :respond_to]
end
@hopsoft
hopsoft / sprockets.rb
Created January 10, 2012 16:45
Rails + Backbone + Underscore templates
# sprockets.rb
# monkey patch to sprockets for evaluate_template
module Blisten
module Sprockets
module Context
# Evaluates a Backbone style HTML template.
# This is basically Sprockets::Context#evaluate
# with some special rules applied.
def evaluate_template(path)
@hopsoft
hopsoft / enum.rb
Created February 14, 2012 22:31
Example of a Ruby enum
module Enum
def self.included(mod)
mod.extend(ClassMethods)
end
module ClassMethods
def enum(*args)
raise "Enum already defined!" if @enum_hash
@enum_hash = {}
@hopsoft
hopsoft / minitest_helper.rb
Created February 24, 2012 04:08
Minitest with Ruby 1.9 & Rails 3.1 that actually works
# No need to add any new gem dependencies for this to work.
# test/minitest_helper.rb
ENV["RAILS_ENV"] = "test"
require "minitest/autorun"
require File.expand_path("../../config/environment", __FILE__)
require "active_support/testing/setup_and_teardown"
require "active_record/fixtures"
module Hopsoft
@hopsoft
hopsoft / has_seed_data.rb
Created March 2, 2012 17:39
Model extension to simplify access to seed data
# has_seed_data.rb
module HasSeedData
def self.included(model)
model.extend(ClassMethods)
model.send(:instance_variable_set, :@seeds, MyApp::Application.data_seeds[model.table_name.to_sym].seeds)
end
module ClassMethods
def seeds; @seeds; end