Skip to content

Instantly share code, notes, and snippets.

@Martin-Nyaga
Martin-Nyaga / calculus.js
Last active November 2, 2022 18:21
Calculus with Javascript , Part 1
var CalculusEquation = function(){
return {}
}
// export the function when this file is included using require
module.exports = CalculusEquation;
@Martin-Nyaga
Martin-Nyaga / searchable.rb
Last active June 11, 2016 00:02
Make rails models easily searchable
# make a model searchable
module Searchable
extend ActiveSupport::Concern
module ClassMethods
@@excluded_search_fields = ["id", "created_at", "updated_at"]
def exclude_search_fields &block
@Martin-Nyaga
Martin-Nyaga / .chmod
Last active May 11, 2016 03:08
A little script that changes permissions of files using a .chmod file for settings
**/*.php 0755
some_executable 0777
files/readonly.pdf 0444
@Martin-Nyaga
Martin-Nyaga / .depconfig.yml
Last active May 11, 2016 03:04
An FTP Deployment script, takes settings from .depconfig.yml and deploys via FTP
# sample configuration
host: example.com
username: my_username
password: mysupersecretpassword
# source, has to be directory
src: src
# destination, has to be an existing directory on the server
dest: public_html/hackaway
@Martin-Nyaga
Martin-Nyaga / README.md
Last active October 10, 2016 18:43
Stop struggling to parse cookies.

CookieMonster.js

Stop struggling with cookies in Javascript, use CookieMonster.js :).

Usage

Include CookieMonster.js in your HTML file or load it using requirejs.

@Martin-Nyaga
Martin-Nyaga / matatu_parking_ticket.rb
Last active November 24, 2016 15:26
Turning edge-case if statements into polymorphism
class MatatuParkingTicket < ParkingTicket
def base_fee
1100
end
def surcharge
weekdays.inlcude?(@day) ? super * 1.5 : super
end
private
class ParkingTicket
BASE FEE = 1000
# This receives a long list of options for
# some other logic that I don't care about
def initialize(opts)
@day = opts[:day] || raise(":day is required")
@vehicle_type = opts[:vehicle_type] || raise(":vehicle_type is required")
# EET stands for environmental efficiency test
def surcharge
charge =
case @day
when "Monday", "Tuesday", "Wednesday", "Friday"
100
when "Thursday", "Saturday", "Sunday"
150
end
if @vehicle_type = "Truck"
def surcharge
charge =
case @day
when "Monday", "Tuesday", "Wednesday", "Friday"
100
when "Thursday", "Saturday", "Sunday"
150
end
if @vehicle_type = "Truck"
# get rid of the base fee constant
def base_fee
if @vehicle_type == "Matatu"
1100
elsif @vehicle_type == "Truck"
1200
else
1000
end
end