Skip to content

Instantly share code, notes, and snippets.

View danielfone's full-sized avatar

Daniel Fone danielfone

View GitHub Profile
module TestReflection
module_function
def recursive_parent_associations(klass, list=Set.new)
assocs = klass.reflect_on_all_associations(:belongs_to) + klass.reflect_on_all_associations(:has_one)
assocs.each do |a|
next if list.include?(a.name)
list << a.name
recursive_parent_associations(a.klass, list)
end
def nest(rows, *attributes)
rows.each_with_object({}) do |row, hash|
# e.g. row = { colour: "red", size: "small", shape: "cube", count: 4 }
# attributes = [:colour, :size, :shape]
# split out the last key from the rest
# row.values_at(:colour, :size, :shape) => ["red", "small", "cube"]
*rest, last = *row.values_at(*attributes) # last = "cube"
# rest = ["small", "red"]
require 'active_model'
#
# The reusable validator
#
class PermittedKeysValidator < ActiveModel::EachValidator
def check_validity!
raise ArgumentError, "you must supply permitted keys" unless permitted_keys.present?
end
@danielfone
danielfone / animals.rb
Created February 17, 2016 00:53
Reopening classes
$ ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
$ pry
[1] pry(main)> class Animal
[1] pry(main)* def noise
[1] pry(main)* "bump"
[1] pry(main)* end
[1] pry(main)* end
=> :noise
[2] pry(main)> class Dog < Animal

What temperature will the 2016 Christchurch Marathon be?

The Christchurch marathon is being held on 5th June this year, and the race starts at 8:30 am. Since I plan to run it, I wanted to know what the temperature was likely to be. Fortunately, NIWA publishes the National Climate Database online for free, and with a few quick queries later, I had all the data I needed. Helpfully, the database exposes the 9 a.m. air temperatures from the "Screen Observations" of its stations (amongst many, many other metrics) which is close enough to the marathon start time (of course, we'd expect it to warm up over the course of the race, but this is a good start). Also helpfully, there is a station in Hagley Park around which the marathon is run.

The dataset provided every 9 a.m. reading going back to 1972, which I subsequently filtered to readings from the 5th Jun ± 4 days. A little excel-fu gives us our required stats and a pretty graph.

I am no

Keybase proof

I hereby claim:

  • I am danielfone on github.
  • I am danielfone (https://keybase.io/danielfone) on keybase.
  • I have a public key whose fingerprint is F30F 53FA 4E66 F2A1 97D4 7124 B8D3 AD7D 56AD 919D

To claim this, I am signing this object:

def post_process(*style_args) #:nodoc:
return if @queued_for_write[:original].nil?
catch :validation_failed do
instance.run_paperclip_callbacks(:post_process) do
instance.run_paperclip_callbacks(:"#{name}_post_process") do
throw :validation_failed if @options[:check_validity_before_processing] && instance.errors.any?
post_process_styles(*style_args)
end
end
Small Arrays: (80 + 40)
Calculating -------------------------------------
b & a | a 24.860k i/100ms
sort_by 27.909k i/100ms
set 7.622k i/100ms
-------------------------------------------------
b & a | a 339.055k (± 3.3%) i/s - 1.715M
sort_by 393.757k (± 2.9%) i/s - 1.982M
set 84.387k (± 2.1%) i/s - 426.832k
#! /usr/bin/env ruby
require 'active_record' # 4.2.3
require 'paperclip' # 4.3.0
ActiveRecord::Migration.verbose = false
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
ActiveRecord::Base.raise_in_transactional_callbacks = true
ActiveRecord::Schema.define(:version => 1) do
create_table :widgets, force: true do |t|
def changes
@changes ||= created_changes + updated_changes + deleted_changes
end
def created_changes
changes_for created_fields, :created
end
def updated_changes
changes_for updated_fields, :updated