Skip to content

Instantly share code, notes, and snippets.

View cheald's full-sized avatar

Chris Heald cheald

View GitHub Profile
## Decorating
chris@luna /var/www/rails3-follow git:(master*) ruby-1.9.3 % rails r bench.rb
{:count=>39, :heap_used=>2169, :heap_length=>2444, :heap_increment=>0, :heap_live_num=>498236, :heap_free_num=>378418, :heap_final_num=>9986}
user system total real
Extend 0.820000 0.040000 0.860000 ( 0.868023)
{:count=>39, :heap_used=>6147, :heap_length=>7027, :heap_increment=>880, :heap_live_num=>498236, :heap_free_num=>2004679, :heap_final_num=>0}
## Extending
chris@luna /var/www/rails3-follow git:(master*) ruby-1.9.3 % rails r bench.rb
{:count=>39, :heap_used=>2169, :heap_length=>2444, :heap_increment=>0, :heap_live_num=>498514, :heap_free_num=>378128, :heap_final_num=>9986}
module ActiveSupport
module FastSafeBuffer
[
:capitalize, :chomp, :chop, :delete, :downcase, :gsub, :lstrip,
:next, :reverse, :rstrip, :slice, :squeeze, :strip, :sub,
:succ, :swapcase, :tr, :tr_s, :upcase, :prepend
].each do |unsafe_method|
if String.new.respond_to? unsafe_method
module_eval <<-EVAL
def #{unsafe_method}!(*)
require 'active_support/core_ext/array'
class FactoryGirl
attr_accessor :options
def self.create(model, options = {})
new(model, options)
end
def initialize(model, options)
def let(name, &block)
define_method(name) do
__memoized.fetch(name) {|k| __memoized[k] = instance_eval(&block) }
end
end
require 'active_support/core_ext/array'
class FactoryGirl
attr_accessor :options
def self.create(model, options = {})
new(model, options)
end
def initialize(model, options)
describe "let behavior" do
foo = "original recipe"
let(:foo) { "extra crispy" }
specify { foo.should == "extra crispy" }
end
class Foo
def value
"Value"
end
def eval_stuff
instance_eval &(Proc.new { puts value })
instance_eval &(Proc.new {|foo| puts foo.value })
instance_eval &(lambda {|x| puts x.value })
instance_eval &(lambda {|x| puts value })
% rspec spec.rb --format doc
Foo
when logged in as a user
should == "TEST VALUE"
Finished in 0.00047 seconds
1 example, 0 failures
module AuthenticationHelpers
def authenticated_as(roles, options=nil)
options = instance_eval(&options) if options.is_a? Proc
Array(roles).each do |role|
context "when logged in as a #{role}" do
before(:all) { @user = FactoryGirl.create role, *Array(options) }
after(:all) { DatabaseCleaner.clean }
before(:each) { sign_in @user }
# Renders the given content or block in a CDATA section if necessary. If CDATA is not necessary, the raw data is returend.
# @param data [String] The data to render. Nil if passing a block.
# @yield Template fragment to encode as CDATA if necessary.
def cdata(data = nil)
data = data.dup if data.present? and data.frozen?
if data
data.strip!
data.gsub!(/\]\]>/, "]]&gt;")
if match = data.match(/([<>&]|[^\x20-\x7f])/)
data = "\n%s" % data if data.match(/\n/)