Skip to content

Instantly share code, notes, and snippets.

View chanakasan's full-sized avatar

Chanaka Abeysinghe chanakasan

View GitHub Profile
@chanakasan
chanakasan / favorite-mappings.vim
Last active January 9, 2016 03:41
Favorite Vim Mappings
"=== NORMAL MODE ===
" visual select inner word
nnoremap vv viw
" visual select to the end of line
nnoremap vV vg_
" yank to the end of line
nnoremap Y y$
@chanakasan
chanakasan / meta-programming.php
Created December 31, 2015 04:37 — forked from ziadoz/meta-programming.php
Basic Meta Programming with PHP 5.4
<?php
trait MetaClass
{
protected $__classMethods = array();
static protected $__staticMethods = array();
public function __call($name, $args)
{
@chanakasan
chanakasan / virtual_proxy.rb
Last active November 13, 2015 13:20
Ruby Virtual Proxy Pattern
class VirtualProxy < BasicObject
def initialize(&loader)
@loader = loader
@object = nil
end
def method_missing(name, *args, &block)
__load__
@object.public_send(name, *args, &block)
end
@chanakasan
chanakasan / constraint_staff.rb
Last active September 2, 2015 16:41 — forked from stevenharman/constraint_staff.rb
Use Routing Constraints to limit access to Rails Routes. An example from Brewdega Cellar app.
module Constraint
class Staff
def matches?(request)
warden(request).authenticated? &&
warden(request).user.staff?
end
private
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, js: true) do
/*
* Selenium WebDriver JavaScript test with Mocha and NodeJS
*
* Start with: SELENIUM=PATH_TO_SELENIUM_JAR/selenium-server-standalone-2.31.0.jar mocha -t 10000 -R list google-sample.js
*
* Download selenium-server-standalone-2.31.0.jar from https://selenium.googlecode.com/files/selenium-server-standalone-2.31.0.jar
* 'sudo su' and 'npm install -g colors mocha selenium-webdriver'
*
* http://visionmedia.github.io/mocha/
* https://code.google.com/p/selenium/wiki/WebDriverJs
@chanakasan
chanakasan / contacts.js
Last active August 29, 2015 14:15 — forked from troyk/contacts.js
// Don't care much about inheritance at this point, but I'll probably attempt it at
// some point via cloning ancestor schema's
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var Contact = new Schema({
_type: String,
name: String
});
/**
* insertTextAtCursor
* An Utility function
* @param href
* @param selectedText
*/
function insertTextAtCursor(href, selectedText) {
var html = "<a href='" + href + "'>" + selectedText + "</a>";
var selectPastedContent = false;
var sel, range;
<?php
/**
* Return URL-Friendly string slug
* @param string $string
* @return string
*/
function seoUrl($string) {
//Unwanted: {UPPERCASE} ; / ? : @ & = + $ , . ! ~ * ' ( )
$string = strtolower($string);
<?php
/**
* Password hash generator
*
* @static
* @param string $password
* @return string
*/
public static function passwordHash ($password)