Skip to content

Instantly share code, notes, and snippets.

View jsborjesson's full-sized avatar

Jimmy Börjesson jsborjesson

View GitHub Profile
@jsborjesson
jsborjesson / randomColor.js
Created April 6, 2013 16:16
Random hex color in JS
'#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6);
@jsborjesson
jsborjesson / rsync-deploy.sh
Last active June 12, 2017 13:49
Deploying a folder via rsync over ssh. This is for deployments from OSX to Linux servers.
#!/bin/bash
# You might have to install a new version of rsync for this to work.
# This is easily done by `brew install rsync` and restarting your terminal.
# To use a file as an exclude-list you can use the option
# --exclude-from='donot-deploy.txt'
# Change these to match your settings
USER_NAME='username'
@jsborjesson
jsborjesson / descriptors.py
Created January 18, 2014 21:38
Python descriptors made easy
import re
from weakref import WeakKeyDictionary
class AdvancedDescriptor(object):
"""
Base class for descriptors, is hard to understand but works for most cases.
from https://www.youtube.com/watch?v=P92z7m-kZpc
"""
def __init__(self, name=None):
@jsborjesson
jsborjesson / _flashes.html.erb
Last active August 29, 2015 13:56
Bootstrap 3 flashes in Rails
<% flash.each do |type, message| %>
<div class="<%= bootstrap_flash_class type %>"><%= message %></div>
<% end %>
@jsborjesson
jsborjesson / rails_generate_secret_token.sh
Last active August 29, 2015 13:56
Generates a new secret token in a Rails app
# This oneliner generates a new secret_token.rb file,
# just paste it in your terminal at the root of your project
# and it will take care of the rest.
echo "$(rails r "p Rails.application.class.parent_name" | sed "s/\"//g")::Application.config.secret_token = '$(rake secret)'" > ./config/initializers/secret_token.rb
@jsborjesson
jsborjesson / json_for_strategy.rb
Created March 18, 2014 09:04
Easy testing of json API:s with Factory Girl
class JsonForStrategy
def initialize
@strategy = FactoryGirl.strategy_by_name(:build).new
end
delegate :association, to: :@strategy
def result(evaluation)
@strategy.result(evaluation).to_json
end
@jsborjesson
jsborjesson / test_helper.rb
Last active August 29, 2015 13:58
Minitest Spec for Rails.
require 'simplecov'
SimpleCov.start 'rails'
ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)
require 'minitest/autorun'
require 'rails/test_help'
require 'factories'
@jsborjesson
jsborjesson / register_statistics.rake
Created April 14, 2014 09:46
Add more folders to rake stats
task :register_statistics do
require 'rails/code_statistics'
STATS_DIRECTORIES << ['Services', 'app/services']
STATS_DIRECTORIES << ['Services Tests', 'test/services']
CodeStatistics::TEST_TYPES << 'Services Tests'
end
Rake::Task['stats'].enhance [:register_statistics]
IO.readlines("/usr/share/dict/words").map(&:strip) # array of english words
module Typoglycemia
def self.convert(text)
text.gsub(/(\w+)/) { |word| scramble_middle_characters(word) }
end
def self.scramble_middle_characters(word)
return word if word.length < 4
word[0] + shuffle_letters(word[1...-1]) + word[-1]
end