Skip to content

Instantly share code, notes, and snippets.

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

Stephen Best bestie

❤️‍🔥
View GitHub Profile
@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 / 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 / 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 / gist:2648506
Created May 9, 2012 20:16
How big are my Ruby files and which is the biggest?
find . -type f -name "*.rb" | xargs wc -l | sort -nr
class ApplicationController
private
def core_application
CoreApplication.new
end
def adapter
RailsAdapter.new(self)
end
@bestie
bestie / gist:5503849
Last active December 16, 2015 21:59
Explains the relationships between components in a system with and EBI entity, boundary, interactor architecture. This covers the front end boundaries (request / response for web apps) and leaves out the backend or persistence.

Uncle Bob's EBI Diagram Explained

Front end of EBI system

Interactors are objects that encapsulate a use-case (create user, delete post, etc) they contain application logic.

Entities contain core domain logic and have no knowledge of the interactors.

The delivery mechanism runs your application. This could be a web framework, command line interface or desktop application.

@bestie
bestie / gist:5504108
Last active December 16, 2015 21:59
Using service layer / service locator pattern with Rails.
class ThingsController < ApplicationController
def create
result = service.for(:create_thing).call(current_user, params) # I prefer to pass self
respond_with(result) # then the service can call render, respond_with or a method you write
end
private
def service
@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.

@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 / 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