Skip to content

Instantly share code, notes, and snippets.

View RichOrElse's full-sized avatar

Ritchie Paul Buitre RichOrElse

View GitHub Profile
@RichOrElse
RichOrElse / data_fetch.rb
Last active February 21, 2020 08:45
Data Fetch
class DataFetch < SimpleDelegator
include Enumerable
delegate :each, :grep, to: :@collection
NOTHING = proc {}
def initialize(collection, otherwise = NOTHING)
super(@collection = collection)
@otherwise = otherwise
end
@RichOrElse
RichOrElse / method.rb
Last active November 14, 2018 06:49
Partial Application in Ruby
class Method
def by(*head)
to_proc.by(*head)
end
def with(*head, &blk)
to_proc.with(*head, &blk)
end
def as(such)
@RichOrElse
RichOrElse / delegate_to_current.rb
Created October 30, 2018 13:40
Rails 5 extractions
class DelegateToCurrent
delegate :current, to: :@context
delegate_missing_to :current
def initialize(context)
@context = context
end
define_singleton_method(:[], &method(:new))
end
@RichOrElse
RichOrElse / git-cheatsheet.md
Last active January 16, 2019 15:07
Git commands for mac
@RichOrElse
RichOrElse / exceptions.rb
Last active October 5, 2018 03:00
ActiveRecord exceptions aliasing
module ApplicationRecord::Exceptions
Failure = ActiveRecord::ActiveRecordError
UpdateFailure = ActiveRecord::RecordInvalid
UndoTransaction = ActiveRecord::Rollback
end
@RichOrElse
RichOrElse / refinements_of.rb
Last active October 4, 2018 12:46
Ruby Refinements micro-library.
class RefinementsOf < Module
def initialize(*types, with: nil, &block)
super(&block)
types.each_with_object(include(*with)) do |type, m|
refine(type) do prepend m
module_exec(m, &block) if block_given?
end
end
@RichOrElse
RichOrElse / cloud_storing.rb
Last active October 4, 2018 00:42
Refactoring code from Put chubby models on a diet with concerns by David Heinemeier Hansson https://t.co/ZzvZohgMFd
class CloudStoring < SimpleDelegator
def new(params)
super with_dropbox_key(params)
end
def reset_dropbox_key(id, storing: find(id))
storing.update! with_dropbox_key
end
private
@RichOrElse
RichOrElse / feature_photo_controller.rb
Last active October 27, 2018 17:12
Refactoring code from RailsConf 2017: In Relentless Pursuit of REST by Derek Prior https://youtu.be/HctYHe-YjnE?t=12m14s
# app/controllers/feature_photo_controller.rb
class FeaturePhotoController < ApplicationController
include DCI::Roles(user_photos: Featuring)
before_action :authenticate_user!
before_action -> { with! user_photos: current_user.photos }
def create
if user_photos.feature params[:photo_id]
redirect_to photos_path
@RichOrElse
RichOrElse / alive.rb
Last active October 29, 2018 07:52
Conway's Game of Life in Ruby
class Alive < Set; include Cartesian
def next
Alive.new merge(&NEIGHBORS).select { |cell| thriving? cell }
end
def thriving?(cell)
live_neighbors = neighboring(cell).size
3 == live_neighbors or (2 == live_neighbors and include?(cell))
end
end
@RichOrElse
RichOrElse / nothingness.rb
Last active November 30, 2018 07:27
Ruby Refinements for handling nil cases.
module Nothingness
using Module.new {
refine NilClass do
def *(number)
end
end
}
refine Numeric do
alias_method :nothing?, :zero?