Skip to content

Instantly share code, notes, and snippets.

@adomokos
adomokos / observer_example.js
Created Jan 2, 2012
Observer in JavaScript
View observer_example.js
var observer = {
addSubscriber: function(callback) {
this.subscribers[this.subscribers.length] = callback;
},
removeSubscriber: function(callback) {
for (var i = 0; i < this.subscribers.length; i++) {
if (this.subscribers[i] === callback) {
delete (this.subscribers[i]);
}
@adomokos
adomokos / inheritance.js
Created Dec 22, 2011
A couple of ways to create JS objects
View inheritance.js
// From the book - Javascript: The Good Parts.
// Differential Inheritance
var myMammal = {
name: 'Herb the Mammal',
getName: function() {
return this.name;
},
says: function() {
return this.saying || '';
@adomokos
adomokos / stubbing_with_arguments_spec.rb
Created Dec 13, 2011
Stubbing with arguments - examples used in my "(More) Specific Stubbing with RSpec" blog post
View stubbing_with_arguments_spec.rb
class GivesCreditToPreferredCustomers
LOOK_BACK_PERIOD = 3
def self.for_large_orders(sales_amount, added_credit)
# the has_large_purchases scope now takes two arguments
preferred_customers = Customer.has_large_purchases(sales_amount, LOOK_BACK_PERIOD)
preferred_customers.each do |customer|
customer.add_credit added_credit
end
end
end
@adomokos
adomokos / .gitconfig
Created Dec 2, 2011
Wiring up p4merge
View .gitconfig
...
[merge]
summary = true
tool = p4
[mergetool "p4"]
cmd = /Applications/p4merge.app/Contents/Resources/launchp4merge "$PWD/$BASE" "$PWD/$LOCAL" "$PWD/$REMOTE" "$PWD/$MERGED"
keepBackup = false
trustExitCode = false
[mergetool]
keepBackup = false
@adomokos
adomokos / ScoreBoard.coffee
Created Nov 19, 2011
The TicTacToe game's ScoreBoard class is taking shape
View ScoreBoard.coffee
class App.ScoreBoard
permutations =
[['A_1', 'B_1', 'C_1'],
['A_2', 'B_2', 'C_2'],
['A_3', 'B_3', 'C_3'],
['A_1', 'A_2', 'A_3'],
['B_1', 'B_2', 'B_3'],
['C_1', 'C_2', 'C_3'],
@adomokos
adomokos / sort_with_other.rb
Created Oct 5, 2011
A special kind of sorting algorithm, where "Other" has to be the last item when sorted
View sort_with_other.rb
require 'ostruct'
module SortWithOther
def <=>(other)
if self.send(sort_field).downcase == "Other".downcase
1
elsif other.send(sort_field).downcase == "Other".downcase
-1
else
self.send(sort_field) <=> other.send(sort_field)
@adomokos
adomokos / string_calculator_spec.coffee
Created Oct 2, 2011
Hackibou (09/01/2011) CoffeeScript file - working on the String Calculator kata
View string_calculator_spec.coffee
class StringCalculator
calculate: (input) ->
@validateArgument(input)
result = 0
result += parseInt(splitValue) for splitValue in @splitValues(input)
result
validateArgument: (input) ->
@adomokos
adomokos / tracks_controller.rb
Created Apr 15, 2011
This code is to demonstrate how I use test doubles for Rails.
View tracks_controller.rb
class TracksController < ApplicationController
def index
signed_in_user
end
def new
@track = Track.new
end
def create
@adomokos
adomokos / Error Publisher in Haml
Created Nov 10, 2010
Since this was - rightly - removed from Rails 3
View Error Publisher in Haml
module ApplicationHelper
def error_publisher(model)
# Thanks Joe!
return unless model.errors.any?
item_errors = ''
model.errors.full_messages.each do |e|
item_errors += "<li>#{e}</li>"
end
@adomokos
adomokos / JavaScript Calculator with Closure
Created Nov 7, 2010
A JavaScript Calculator with Closure
View JavaScript Calculator with Closure
/*
function Calculator() {
this.add = function(input) {
var result = 0;
for(i = 0; i<input.length; ++i) {
result += input[i];
}
return result;
}