Skip to content

Instantly share code, notes, and snippets.

@patrickgombert
patrickgombert / activeRecordDirtyPusher.rb
Created July 28, 2011 20:06
ActiveRecord::Dirty Pusher example
class Example < ActiveRecord::Base
after_save { push_updates if changed? }
private
def push_updates
if self.my_attribute_changed?
Pusher["channel-example"].trigger!("my-attribute-update", {:my_attribute => self.my_attribute})
end
@patrickgombert
patrickgombert / gist:1225336
Created September 18, 2011 18:11
Flawed Singleton
class MailingList < ActiveRecord::Base
has_many :subscribers
def self.instance
self.find_by_id(1)
end
end
@patrickgombert
patrickgombert / gist:1225346
Created September 18, 2011 18:19
Better Singleton
class MailingList < ActiveRecord::Base
has_many :subscribers
before_create :assert_singleton!
def self.instance
first || create!
end
private
@patrickgombert
patrickgombert / kidsruby_installer.sh
Created December 20, 2011 20:43
Kids Ruby Ubuntu Installer
#!/bin/bash
echo "Installing Kids Ruby!"
sudo apt-get install git
# rbenv
cd
git clone git://github.com/sstephenson/rbenv.git .rbenv
echo `export PATH="$HOME/.rbenv/bin:$PATH"` >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
@patrickgombert
patrickgombert / gist:1572284
Created January 6, 2012 20:34
CppUTest Install Error
make[1]: Entering directory `/home/patrickgombert/.cpputest/examples'
compiling MockDocumentationTest.cpp
ApplicationLib/MockDocumentationTest.cpp: In member function ‘virtual void TEST_MockDocumentation_returnValue_Test::testBody()’:
ApplicationLib/MockDocumentationTest.cpp:132:6: error: variable ‘value’ set but not used [-Werror=unused-but-set-variable]
cc1plus: all warnings being treated as errors
make[1]: *** [objs/ApplicationLib/MockDocumentationTest.o] Error 1
make[1]: Leaving directory `/home/patrickgombert/.cpputest/examples'
make: *** [examples] Error 2
@patrickgombert
patrickgombert / gist:1604213
Created January 13, 2012 01:54
Bad Javascript
var hider = function() {
$("#some_div").hide();
}
var shower = function() {
$("#some_div").show();
}
var total_column = function() {
var total = 0;
@patrickgombert
patrickgombert / gist:1604655
Created January 13, 2012 04:14
Better Javasript
var Visibility {
init : function() {
$("#some_button").bind('click', hider);
$("#some_other_button").bind('click', shower);
}
hider : function() {
$("#some_div").hide();
}
@patrickgombert
patrickgombert / gist:1640806
Created January 19, 2012 16:07
Clojure protocols, types, and records
; Here is our protocol
(defprotocol ThirdGradeMath
(add [this])
(sub [this])
(mul [this])
(div [this]))
;And our record
(defrecord MathRecord [x y]
;Let's implement our protocol
@patrickgombert
patrickgombert / gist:1710909
Created January 31, 2012 14:56
Clojure to Java interop
(ns test.wrapper
(:gen-class
:name test.Speak
:methods [
[hello [String] String]
]))
(defn -hello [this name]
(str "Hello, " name))
@patrickgombert
patrickgombert / gist:1711288
Created January 31, 2012 15:57
Clojure to Java interop with State
(ns test.wrapper
(:gen-class
:name test.Speak
:state name
:methods [
[hello [] String]
[setName [name] void]
]))
(defn -setName[this name]