Skip to content

Instantly share code, notes, and snippets.

Nathan Ladd ntl

  • Austin, TX
View GitHub Profile
@ntl
ntl / pattern-matching.rb
Created Jun 15, 2018
Conditional Examples in Ruby
View pattern-matching.rb
# This is an example using Eventide's message handling DSL for declaring different
# handler methods based on message type
class SomeHandler
include Messaging::Handle
handle SomeMessage do |some_msg|
# ...
end
handle OtherMessage do |other_msg|
View initializer_arguments.rb
class InitializerArgumentsPaymentProcessor
def initialize(number, expiration_date, amount)
@number, @expiration_date, @amount = number, expiration_date, amount
end
def call
if SomeGateway.(number, expiration_date, amount)
true
else
false
View Gemfile
source "https://rubygems.org"
gem "whatever"
# Load minitest and red-green
group :minitest do
gem "minitest"
gem "minitest-rg"
end
@ntl
ntl / event_sourcing_example.rb
Last active Aug 29, 2015
In my usage so far, an "Event" doesn't get its own type; it's an object that transports attributes. The listeners also use a class method to route events to methods on the object. In this way, you can test event handlers as you would any other ruby method.
View event_sourcing_example.rb
# app/commands/receive_bonus_command.rb
# Commands look like ActiveModel+virtus "ish" objects
class ReceiveBonusCommand < Command
attribute :income_source_id, String
attribute :bonus_amount, Integer
attribute :effective_date, Date
validates :account_id, presence: true
def validate
View fixtures_vs_factories.rb
# The controller; yeah it's not great but I want to show the tests
class ProductsController < ApplicationController
def index
if params[:min_rating]
@products = Product.where("average_rating >= ?", params[:min_rating])
else
@products = Product.to_a
end
end
end
@ntl
ntl / simple_exposing.rb
Last active Aug 29, 2015
The idea here is to give you a simple method that can define your ivar for you, protecting against `nil` proactively, and record the location where the ivar was defined. Best part is, if someone looks for where `#expose` is defined, they'll be happy to find out it's not in a gem. Easy to modify/understand.
View simple_exposing.rb
# Instead of ditching ivars, just make them easier to discover:
class ApplicationController < ActionController::Base
private
# Expose an object as an ivar to your views. Explodes if you try to assign nil.
#
# Use:
#
You can’t perform that action at this time.