Skip to content

Instantly share code, notes, and snippets.

View br3nt's full-sized avatar

br3nt

View GitHub Profile
@br3nt
br3nt / appointment.rb
Created April 24, 2014 07:16
Advanced Search utilising scopes (problem with match on any)
class Appointment < ActiveRecord::Base
belongs_to :patient
belongs_to :doctor
scope :between, ->(time_range) {
where(arel_table[:start_time].lt(time_range.first).and(arel_table[:end_time].gt(time_range.first)))
}
end
@br3nt
br3nt / rounding.rb
Created February 19, 2015 23:27
Rounding values
#
# Rounds a number
#
# If nearest == pivot, the rounding will always go up.
#
# Default behaviour:
# round(0) # 10
# round(1) # 10
# round(9) # 10
# round(10) # 20
@br3nt
br3nt / ProxyColumns.md
Last active August 29, 2015 14:15
ProxyColumns

ProxyColumns

ProxyColumns adds a proxy column over the top of an existing column associated with an active-record model.

This proxy column overrides the original column's accessor methods. The basic proxy functionality doesn't do much beside set and get the value of original columns.

The Proxy object can be subclassed/overridden to provide any sort of functionality required. This enables additional functionality to be added on top of the underlying column. A ProxyColumn can delegate based on the column and the column's value, as seen in MetaDataProxyColumn.

One of the problems I faced was that the accessor methods for a column on a model weren't generated until the first model was initialised. That is why the methods are added dynamically using the after_initialize callback.

@br3nt
br3nt / IndifferentString.rb
Created March 4, 2015 00:15
IndifferentString - like HashWithIndifferentAccess
class IndifferentString < String
def initialize(val)
# NOTE: would it be better to raise a TypeError if val isnt string or symbol?
# or is it okay to duck type here?
super(val.to_s)
end
def ==(other_object)
self.to_sym == other_object.to_sym
end
@br3nt
br3nt / Domain.rb
Created March 28, 2015 04:39
Domain for constraint programming
class Domain
INFINITY = 1.0 / 0.0
attr_reader :domain
# TODO:
# methods: <<, +, &, -
def initialize(*ranges)
@br3nt
br3nt / CompanionClass.md
Last active August 29, 2015 14:26
Companion Classes

TODO:

  • Allow existing classes to be companion classes
  • Allow companion classes to be overwritten by subclasses
  • Create module that allows classes to create multiple companion classes:
@br3nt
br3nt / dispatcher.js
Last active November 29, 2015 10:05 — forked from neilj/window-controller.js
Cross-tab window communication
function Dispatcher(channel_name, broadcast_to_self) {
this.channel_name = channel_name ? 'dispatcher:' + channel_name : 'dispatcher:default_channel';
this.id = Math.random();
this.isMaster = false;
this.others = {};
this.broadcast_to_self = broadcast_to_self === undefined ? true || !!broadcast_to_self;
this.callbacks = {};
window.addEventListener( 'storage', this, false );
window.addEventListener( 'unload', this, false );
@br3nt
br3nt / linked_list_example.rb
Created April 11, 2016 13:11
Linked List Implementation in Ruby
# create a new list
list = ListNode.new
# add some items to the head or tail
list.add_to_head(Node.new('Hello'))
list.add_to_tail(Node.new('World'))
list.to_a # => ["Hello", "World"]
# remove items from the head
list.head.remove
@br3nt
br3nt / examples.rb
Last active April 11, 2016 23:14
LRU Cache Implementation
# Create the LRU cache
cache = LRUCache.new(max_size: 3)
# adding a key value pair adds the vaue to the highest priority position
cache.add(:a, 'a')
cache.add(:b, 'b')
cache.add(:c, 'c')
cache.to_a # => ["c", "b", "a"]
cache.size # => 3
@br3nt
br3nt / examples.rb
Created May 6, 2016 05:06
Augmented Interval Tree
# Example of an Augmented Tree
#
# I have added an additional `lower_limit` method, which enables testing of overlapping children.
#
# See:
# - https://en.wikipedia.org/wiki/Interval_tree#Augmented_tree
# - http://www.davismol.net/2016/02/07/data-structures-augmented-interval-tree-to-search-for-interval-overlapping/
# - http://www.bowdoin.edu/~ltoma/teaching/cs231/fall08/Lectures/10-augmentedTrees/augtrees.pdf
#