Skip to content

Instantly share code, notes, and snippets.

View cjbottaro's full-sized avatar

Christopher J. Bottaro cjbottaro

  • AcademicWorks, Inc
  • Austin, Tx
View GitHub Profile
@cjbottaro
cjbottaro / gist:1720115
Created February 1, 2012 23:22
DCI with ActiveSupport callbacks
require "active_support"
module RoleA
def self.extended(object)
object.singleton_class.class_eval do
puts "blah"
puts object.inspect
include ActiveSupport::Callbacks
define_callbacks :save
@cjbottaro
cjbottaro / gist:1488185
Created December 16, 2011 21:56
convert to utf-8 using iconv fallback when String#encode doesn't work
require "iconv"
def sanitize_utf8(string)
string = string.encode("UTF-8", :invalid => :replace, :undef => :replace)
begin
string.blank? # Assume you're using ActiveSupport
rescue ArgumentError => e
if e.message == "invalid byte sequence in UTF-8"
Thread.current["iconv"] ||= Iconv.new('UTF-8//IGNORE', 'UTF-8')
string = Thread.current["iconv"].iconv(string)
@cjbottaro
cjbottaro / gist:1361613
Created November 13, 2011 04:51
god segfault
$ sudo RAILS_ENV=staging bundle exec god -c config/god/resque.rb -D
I [2011-11-13 04:46:05] INFO: Loading config/god/resque.rb
I [2011-11-13 04:46:05] INFO: Syslog enabled.
I [2011-11-13 04:46:05] INFO: Using pid file directory: /home/sender/sender_staging/current/tmp/pids
I [2011-11-13 04:46:05] INFO: Started on drbunix:///tmp/god.17165.sock
I [2011-11-13 04:46:05] INFO: resque-00 move 'unmonitored' to 'init'
I [2011-11-13 04:46:05] INFO: resque-00 moved 'unmonitored' to 'init'
I [2011-11-13 04:46:05] INFO: resque-00 [trigger] process is running (ProcessRunning)
I [2011-11-13 04:46:05] INFO: resque-00 move 'init' to 'up'
I [2011-11-13 04:46:05] INFO: resque-00 registered 'proc_exit' event for pid 8972
@cjbottaro
cjbottaro / gist:1326226
Created October 30, 2011 18:22
No dice either
module User::Ascents
def self.extended(object)
object.singleton_class.class_eval do
has_many :fun_ascents, :class_name => "Ascent"
end
end
end
@cjbottaro
cjbottaro / gist:1326224
Created October 30, 2011 18:21
Wish this worked
module User::Ascents
extend ActiveSupport::Concern
included do
has_many :fun_ascents, :class_name => "Ascent"
end
def self.extended(object)
object.singleton_class.send(:include, self)
end
@cjbottaro
cjbottaro / gist:1205075
Created September 8, 2011 23:20
class vs instance vars in CoffeeScript
class Blah
@counter = 0
@incr: ->
return @counter += 1
constructor:
@counter = 0
@cjbottaro
cjbottaro / gist:1167520
Created August 24, 2011 07:56
sleep in EM reactor
EM.run do
t = Time.now
Fiber.new{ sleep(1) }.resume
Fiber.new{ sleep(1) }.resume
puts Time.now - t
# 2 seconds have elapsed. No concurrency is achieved.
EM.stop
end
@cjbottaro
cjbottaro / gist:1152006
Created August 17, 2011 16:55
cron job
#!/bin/bash
RVM_BIN=/usr/local/rvm/bin/rvm
EDITOR_UI=/home/onespot/editor-ui/current
$RVM_BIN 1.9.2@editor-ui ruby $EDITOR_UI/bin/openx_report.rb -e production > $EDITOR_UI/log/openx_report.log 2>&1
@cjbottaro
cjbottaro / gist:1128108
Created August 5, 2011 17:54
unDRY rspec code
describe Object do
context "extented with MyModule" do
before(:all){ subject.extend(MyModule) }
it "should define #some_method" do
subject.should respond_to(:some_method)
end
end
end
describe Class do
@cjbottaro
cjbottaro / gist:1128091
Created August 5, 2011 17:50
DRY'ed rspec using shared_context with a block
shared_context "for_extending" do
context "extended with MyModule" do
before(:all){ subject.extend(MyModule) }
it "should define #some_method" do
subject.should respond_to(:some_method)
end
yield
end
end