Skip to content

Instantly share code, notes, and snippets.

View Catharz's full-sized avatar

Craig Read Catharz

  • Redbubble
  • Melbourne, Australia
View GitHub Profile
/*
Some syntactic sugar for dealing with Time in Swift
Examples:
3.days.inHours
3.6.hours.ago
12.hours.inDays
*/
@Catharz
Catharz / model_creator.rb
Created June 11, 2014 04:05
Create Rails models for every table in the database
invalid_chars = [' ', '$', '(', ')']
ActiveRecord::Base.connection.tables.each do |table_name|
name = table_name
invalid_chars.map { |ch| name = name.gsub(ch, '_') }
name = name.camelize
name = name + "_" if Module.const_defined? name
eval %{
class #{name} < ActiveRecord::Base
@Catharz
Catharz / falling_disks.rb
Created April 16, 2013 01:10
Codility Omega 2013 - Falling Disks
def falling_disks(rings, disks)
min, disk = rings[0], 0
rings.each_index do |ring|
min = rings[ring] if rings[ring] < min
rings[ring] = min if rings[ring] > min
end
(rings.size - 1).downto(0) do |ring|
disk += 1 if disks[disk] <= rings[ring]
@Catharz
Catharz / character.rb
Created March 29, 2013 04:42
FactoryGirl issues with attributes_for
class Character < ActiveRecord::Base
belongs_to :player, :inverse_of => :characters, :touch => true
belongs_to :archetype, :inverse_of => :characters, :touch => true
validates_presence_of :name
validates_presence_of :player, :archetype, :char_type, :on => :update
validates_uniqueness_of :name
validates_format_of :char_type, :with => /g|m|r/ # General Alt, Main, Raid Alt
end
@Catharz
Catharz / hash_parser.rb
Created March 18, 2013 01:55
Failing test on travis-ci
class HashParser
def initialize(hash)
if hash.has_key? "class"
hash["archetype"] = hash["class"]
hash.delete "class"
end
hash.each do |k, v|
if v.is_a? Hash
nv = HashParser.new(v)
else
@Catharz
Catharz / haml.rake
Created February 24, 2012 06:50
Rake task to convert views from erb to haml
class ToHaml
def initialize(path)
@path = path
end
def convert_all!
Dir["#{@path}/**/*.erb"].each do |file|
haml_file = file.gsub(/\.erb$/, '.haml')
puts "Converting #{File.basename(file)} to #{File.basename(haml_file)}"
`html2haml -rx #{file} #{haml_file}`
@Catharz
Catharz / 01_create_instances.rb
Created February 6, 2012 02:02
Rails Migration from 1-M to M-M Relationship
class CreateInstances < ActiveRecord::Migration
def self.up
# Create the new instances table
create_table :instances do |t|
t.references :zone
t.references :raid
t.datetime :start_time
t.datetime :end_time
t.timestamps