Skip to content

Instantly share code, notes, and snippets.

View peter's full-sized avatar

Peter Marklund peter

View GitHub Profile
@peter
peter / programming-languages.md
Created May 10, 2014 12:47
Programming Languages

Programming Languages

Lisp (1958)

Interpreted, dynamic typing, functional

C (1972)

Compiled, static typing, imperative (procedural)

@peter
peter / es6-uncensored.js
Last active August 29, 2015 14:01
ES6 Uncensored
// 1. Swith two variables
// ES5
var temp = a;
a = b;
b = temp;
// ES6 - destructuring assignment
[b, a] = [a, b];
@peter
peter / gist:75aa6cf67f370b5bae4e
Created May 11, 2015 08:46
JavaScript ramda example - imperative to functional refactoring
// BEFORE REFACTORING (IMPERATIVE)
var filteredLinks = [];
var weekdayCounts = {};
// Limit to 4 links per weekday.
// Loop through all links and keep track of
// how many are in each weekday.
widget.links.forEach(function(link) {
var weekday = link.group;
if (weekdayCounts[weekday] === undefined) {
weekdayCounts[weekday] = 0;
@peter
peter / gist:496e99b118e7688e2cab
Created May 25, 2015 18:54
Ramda mapObj example
'use strict';
var R = require('ramda');
var blank = function(value) {
return value == null || (typeof value === 'string' && value.trim().length === 0);
};
var present = function(value) {
return !blank(value);
class Foo
def bar
end
end
namespace :db do
# NOTE: In Rails 3 there will be a db:setup task but it only works if ActiveRecord::Base.schema_format == :ruby
# See http://afreshcup.com/2009/05/11/seed-data-in-rails-3
# The following patch fixes db:schema:load to work with schema_format :sql:
# https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2789-make-rake-task-dbschemaload-compatible-with-sql-schema-format
desc 'Recreate the database, load the schema, and initialize with the seed data'
task :super_setup => [ 'db:drop', 'db:create' ] do
system("cat db/development_structure.sql db/development_data.sql | script/dbconsole")
seed_file = File.join(Rails.root, 'db', 'seeds.rb')
load(seed_file) if File.exist?(seed_file)
@peter
peter / synchronized_function.js
Last active September 3, 2015 16:06
Hack to prevent JavaScript function from executing concurrently (to avoid race conditions etc.)
'use strict';
// NOTE: this in memory locking approach will only work with a single process
// You might use something like Redis och memcachached for storing the lock if you need to lock
// across several processes.
var synchronizedFunction = function(lockIdFn, fn) {
var locks = {};
var synchronized = function() {
var args = Array.prototype.slice.call(arguments),
lockId = lockIdFn.apply(null, args);
# In lib/tasks/deploy.rake
namespace :deploy do
def write_error_page(status, locale = nil)
dest_filename = [status.to_s, locale, "html"].compact.join(".")
File.open(File.join(Rails.root, "public", dest_filename), "w") do |file|
path = File.join(Rails.root, "app", "views", "errors", "#{status}.rhtml")
file.print ERB.new(File.read(path)).result
end
end
describe "Mocking/Stubbing Associations with RSpec" do
it "can be done with proxy_target" do
# Suppose the Article model has an after_save callback that invokes notify_change on the author
# if the article has been changed.
@article.author.proxy_target.should_receive(:notify_change)
@article.update_attributes(:body => "new body")
end
end
class Author
def invoke_inside(method, *args)
self.send(method, *args)
end
end
author = Article.first.author
def author.foobar
"foobar"
end