Skip to content

Instantly share code, notes, and snippets.

View nicholasjhenry's full-sized avatar

Nicholas Henry nicholasjhenry

View GitHub Profile
@nicholasjhenry
nicholasjhenry / team_member.rb
Last active August 29, 2015 13:58
Collaboration Rules
class TeamMember
def initialize(person, team)
add_person(person)
add_team(team)
end
def add_person(person)
test_add_person(person)
do_add_person(person)
@nicholasjhenry
nicholasjhenry / hash.rb
Created April 25, 2014 03:34
Hash Selection Pattern
{ true => -> { self.respond_with @post, @comment },
false => -> { self.xms_error @comment }
}[@comment.save].call
{ success: 'alert-success',
error: 'alert-danger',
notice: 'alert-info',
warn: 'alert-warning' }[flash[:type]]
@nicholasjhenry
nicholasjhenry / experiment.rb
Last active August 29, 2015 14:00
Services, domains, business rules and colloborations
class Order < ActiveRecord::Base
has_many :payments
scope :active, -> { where(:active, true) }
def self.fetch(id)
find(id)
end
# @stereotype business_rule
@nicholasjhenry
nicholasjhenry / anonymous.rb
Last active August 29, 2015 14:01
Anonymous Classes in Ruby
class Foo
def bar
baz = Class.new do
def qux
"QUX"
end
end
baz.new
end
end
@nicholasjhenry
nicholasjhenry / streamers.ex
Last active August 29, 2015 14:05
Streamers
defmodule Streamers do
defmodule M3U8 do
defstruct [:program_id, :path, :bandwidth]
end
@doc """
Find streaming index file in a given directory
"""
def find_index(directory) do
files = Path.join(directory, "*.m3u8")
@nicholasjhenry
nicholasjhenry / address.rb
Created August 21, 2014 14:46
Example output for a nested form
class Address < ActiveRecord::Base
belongs_to :person
end
@nicholasjhenry
nicholasjhenry / example.rb
Created August 30, 2014 18:42
Enumerator for decorating a collection
x = [1, 2, 3]
class Foo
def initialize(bar)
@bar = bar
end
def greeting
"Hello #{bar}"
end
@nicholasjhenry
nicholasjhenry / original.rb
Last active August 29, 2015 14:07
Refactoring Controller
def create
uploaded_file = params[:batch_payouts_csv]
unless valid_file_type?(uploaded_file)
render json: {"error_message" => I18n.t("merchant.messages.errors.invalid_file_type")}, status: 400
return
end
uploaded_csv_file = Batch::CsvFile.new(uploaded_file.path)
unless uploaded_csv_file.valid_size?
@nicholasjhenry
nicholasjhenry / about.md
Last active August 29, 2015 14:08
Examples of OO messaging (East-oriented programming)

From: http://thesecretsquad.wordpress.com/2014/10/25/dazed-and-confuzzled/

What should these messages look like? The basis for my analysis is:

An object should send messages to client-specific interfaces. This means an object should send messages that are meaningful to itself, and interfacing objects should have only those messages available.