Skip to content

Instantly share code, notes, and snippets.

View nathankleyn's full-sized avatar
👨‍💻
Creating!

Nathan Kleyn nathankleyn

👨‍💻
Creating!
View GitHub Profile
@nathankleyn
nathankleyn / gd-shared.js
Created December 12, 2012 12:06
This is an example of how to handle the /gd/shared event and count the number of shares.
gd.init().then(function() {
var shares = 0;
gd.event('/gd/shared').subscribe(function() {
shares += 1;
// Do whatever else you would like here...
});
});
@nathankleyn
nathankleyn / delegate_writer.patch
Created February 13, 2012 21:05
A writer addition for Rails' delagate feature.
diff --git a/.gitignore b/.gitignore
index 2dfdf96..79d884c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -19,3 +19,5 @@ railties/test/initializer/root/log
railties/doc
railties/guides/output
railties/tmp
+.DS_Store
+nbproject
@nathankleyn
nathankleyn / gist_11_functional_ruby.rb
Created February 6, 2012 11:51
Gist #11 For "Functional Programming Techniques With Ruby: Part I"
class CssBlock
attr_reader :selector, :properties
def initialize(selector, properties = {})
@selector = selector.dup.freeze
@properties = properties.dup.freeze
end
def set(key, value = nil)
new_properties = if key.is_a?(Hash)
@nathankleyn
nathankleyn / gist_10_functional_ruby.rb
Created February 6, 2012 11:50
Gist #10 For "Functional Programming Techniques With Ruby: Part I"
CssBlock.new("#some_id .class a").set("color", "#FFF").set({ "color" => "#000", "text-decoration" => "underline"}) # => "#some_id .class a { color: #000; text-decoration: underline }"
@nathankleyn
nathankleyn / gist_9_functional_ruby.rb
Created February 6, 2012 11:49
Gist #9 For "Functional Programming Techniques With Ruby: Part I"
class CssBlock
# ...
def to_s
serialised_properties = self.properties.inject([]) do |acc, (k, v)|
acc + ["#{k}: #{v}"]
end
"#{self.selector} { #{serialised_properties.join("; ") } }"
@nathankleyn
nathankleyn / gist_8_functional_ruby.rb
Created February 6, 2012 11:49
Gist #8 For "Functional Programming Techniques With Ruby: Part I"
class CssBlock
# ...
def set(key, value = nil)
new_properties = if key.is_a?(Hash)
key
elsif !value.nil?
{
key => value
@nathankleyn
nathankleyn / gist_7_functional_ruby.rb
Created February 6, 2012 11:47
Gist #7 For "Functional Programming Techniques With Ruby: Part I"
class CssBlock
attr_reader :selector, :properties
def initialize(selector, properties = {})
@selector = selector.dup.freeze
@properties = properties.dup.freeze
end
end
@nathankleyn
nathankleyn / gist_6_functional_ruby.rb
Created February 6, 2012 11:46
Gist #6 For "Functional Programming Techniques With Ruby: Part I"
class CssBlock
# We'll add some methods in here.
end
@nathankleyn
nathankleyn / gist_5_functional_ruby.rb
Created February 6, 2012 11:46
Gist #5 For "Functional Programming Techniques With Ruby: Part I"
def module_split(module_path, separator = "::")
modules = module_path.split(separator)
modules.length.downto(1).map { |n| modules.first(n).join(separator) }
end
module_split("W::X::Y::Z")
@nathankleyn
nathankleyn / gist_4_functional_ruby.rb
Created February 6, 2012 11:46
Gist #4 For "Functional Programming Techniques With Ruby: Part I"
y = x + 1
∴ x = y - 1