Skip to content

Instantly share code, notes, and snippets.

View rewinfrey's full-sized avatar
🏄‍♂️
fix f = let x = f x in x

Rick Winfrey rewinfrey

🏄‍♂️
fix f = let x = f x in x
View GitHub Profile
-- situation was strange:
-- resource_attributes is a json column
-- needed to do a time interval comparison for a where clause in the query, but the resource_attributes->'created_at'
-- attribute was actually a json array that looked like this:
-- [nil, '2015-08-07 23:43:23TZ']
-- I needed a way to compare against the second element in that array (the timestamp string)
-- and thanks to a helpful SO post I was able to create a postgres function allowing me to isolate the timestamp from the
-- containing json array
-- http://stackoverflow.com/questions/18833970/querying-inside-postgres-json-arrays
@rewinfrey
rewinfrey / daylight_savings_time_finder.ruby
Last active September 11, 2015 14:23
Daylight Savings Time Finder
def daylight_savings_time_starts(year)
date = Date.new(year, 3, 1)
return Date.new(year, 3, 8) if date.wday == 0
return Date.new(year, 3, (7 - date.wday + 7 + 1))
end
def daylight_savings_time_stops(year)
date = Date.new(year, 11, 1)
return date if date.wday == 0
return Date.new(year, 11, (7 - date.wday + 1))
@rewinfrey
rewinfrey / dataset_utilities.R
Created August 21, 2015 00:41
R Utility Functions for cleaning, randomizing, sorting and creating test / training sets
clean_data <- function(dataset) {
na.omit(dataset)
}
randomize <- function(dataset) {
dataset$randomized <- runif(length(dataset[,1]))
dataset
}
randomize_ordered <- function(dataset) {
# Support for Rspec / Capybara subdomain integration testing
# Make sure this file is required by spec_helper.rb
# (e.g. save as spec/support/subdomains.rb)
def switch_to_subdomain(subdomain)
# lvh.me always resolves to 127.0.0.1
hostname = subdomain ? "#{subdomain}.lvh.me" : "lvh.me"
Capybara.app_host = "http://#{hostname}"
end
@rewinfrey
rewinfrey / static_methods.rb
Last active August 29, 2015 14:19
Ruby static method refactoring
# original implementation refactored after extract method (but still using static methods)
# criticism for this approach is:
# - dependencies are passed between method calls, rather than referenced via instance methods
# - cannot declare static methods as private (this is not valid)
# - there are multiple responsibilities (I'd argue this is not valid)
# - can be difficult to test
class SyncToAnalyticsService
ConnectionFailure = Class.new(StandardError)
@rewinfrey
rewinfrey / sql.txt
Created March 13, 2015 23:11
sql with negative look ahead
SELECT organizations.name,
organizations.id,
organizations.ancestry,
tmp.average_activated_time_of_children from organizations
JOIN (
SELECT org.ancestry,
AVG(org.updated_at - org.created_at) as average_activated_time_of_children,
CAST(substring(org.ancestry from '\d+(?!\/)$') as integer) as organization_id
FROM organizations org
INNER JOIN accounts acc ON acc.organization_id=org.id
@rewinfrey
rewinfrey / application.json
Created March 10, 2015 15:43
Sample application json
{
1: {
sectionName: "Section One",
sectionId: 1
questions: {
1: {
prompt: "This is question one",
questionType: "basic-input"
questionId: 12
},
@rewinfrey
rewinfrey / example.js
Last active August 29, 2015 14:16
self-registering js on window example
// assets/javascripts/interview_guides_edit_helper.js
(function($) {
var InterviewGuidesEditHelper = {};
// init function is called from the view with the options passed in
InterviewGuidesEditHelper.init = function(options) {
this.parentSelector = options.parentSelector;
this.sectionSortableClass = options.sectionSortableClass;
this.questionSortableClass = options.questionSortableClass;
this.deletedSectionSortableClass = options.deletedSectionSortableClass;
@rewinfrey
rewinfrey / ycombinator.rb
Created March 2, 2015 07:37
sample factorial y combinator in Ruby
result = -> (builder, number) { builder.call(builder, number) }.call(
-> (recurse, number) {
return 1 if number == 0
return number * recurse.call(recurse, number - 1)
},
5
)
result # => 120
@rewinfrey
rewinfrey / example_strategy_factory.rb
Created February 13, 2015 23:07
Example Strategy Factory
# this would be called in the controller,
# whose return value is the Strategy class
class CandidateJobDocumentStrategyFactory
def self.strategy_for(document_type)
case document_type
when :resume then resume_strategy
when :cover_letter then cover_letter_strategy
when :application then application_strategy
end