Skip to content

Instantly share code, notes, and snippets.

View bestie's full-sized avatar
❤️‍🔥

Stephen Best bestie

❤️‍🔥
View GitHub Profile
@bestie
bestie / tsort_spec.rb
Last active March 29, 2016 21:39
A topological sort head scratcher
# Ah it's OK I got this one myself. It's pretty late at night but I get it now!
# I'll leave this here in case anyone is interested. I've added the
# explaination below
# This is some code I wrote as excercise after watching
# https://youtu.be/QnWDU1wcsPA?t=470
#
# The first example mimics the result from the video and is there just to
# verify my use of the TSort module is correct.
class DateRangeFilter
def self.to_proc
->(constraint, events) { new(constraint, events).call }.to_proc
end
def initialize(constraint, events)
@constraint = constraint
@events = events
end
class ApplicationController
private
def core_application
CoreApplication.new
end
def adapter
RailsAdapter.new(self)
end
@bestie
bestie / procs.rb
Last active August 29, 2015 14:20
Proc behavior
# Proc behavior in reply to https://gist.github.com/jcoglan/0a37aee2582877b27920
def foo(&block)
p [:foo, 1]
puts "block returns " + block.call.inspect
p [:foo, 2]
end
def bar(&block)
p [:bar, 1]
@bestie
bestie / gh
Created December 15, 2014 13:20
$ gh # Open this repo on Github
#!/usr/bin/env ruby
organization, repo = %x[git remote --verbose]
.each_line
.select { |line| line.include?("origin") }
.select { |line| line.include?("fetch") }
.select { |line| line.include?("github.com") }
.map { |line| line.match(%r{github.com:([^/]+)/([^/]+)\.git})[1..-1] }
.first || ["drupal"] * 2
@bestie
bestie / extend_demo.rb
Created February 13, 2014 16:20
Ruby demo: extending modules with other modules
module Base
extend self
def hi
puts "Base says hi"
end
end
module Ext1
@bestie
bestie / typed_struct.rb
Created October 28, 2013 17:26
An HStruct that also contains type data,
require 'hstruct'
class TypedStruct < HStruct
def self.schema(schema = nil)
@__schema ||= schema
end
end
def TypedStruct(members_hash, &block)
member_names = members_hash.keys
@bestie
bestie / application_controller.rb
Last active June 21, 2016 08:57
"Improve your code with dependency injection" code samples
class ApplicationController < ActionController::Base
private
def app
APP
end
end
@bestie
bestie / proc_merge_curry_ext.rb
Created October 14, 2013 15:58
`Proc#merge_curry` Single argument Proc can be curried to receive an arbitrary number of hashes. The Proc is finally called with the merge result of all hashes. Hashes are merged in order, last winning.
module MergeCurry
def merge_curry(hash_count)
curried_proc = self
hashes = []
curry_again = Proc.new { |*args|
hashes = hashes.concat(args)
if hashes.size >= hash_count
final_hash = hashes.reduce({}) { |agg, hash|
@bestie
bestie / README.md
Last active December 19, 2015 02:38
Another service layer example, arguing for dynamically instantiated services rather than static class methods.

OO Service Layer Example

The revised (OO) AnalyticsService has an SRP violation. It knows:

  • How to capture analytics
  • When it should and shouldn't turn itself off

A more OO approach would to remove the second responsibilty and have something like the above where you have an that is object repsonsible for configuration.