Skip to content

Instantly share code, notes, and snippets.

View RichOrElse's full-sized avatar

Ritchie Paul Buitre RichOrElse

View GitHub Profile
@RichOrElse
RichOrElse / authenticater.rb
Created January 5, 2016 18:14
Refinement versus Service Object
module Authenticate
refine User do
def authenticate(unencrypted_password)
BCrypt::Password.new(password_digest) == unencrypted_password
end
end
refine NilClass do
def authenticate(unencrypted_password)
false
@RichOrElse
RichOrElse / open_struct_implementation.rb
Last active January 11, 2016 15:10
Indinero sample test
# Question 3:
# Basic implementation of OpenStruct without gems.
# Create a class that can be initialized with a hash, the instance should have getters and setters for the keys of the hash
class OpenStructImplementation
def initialize(attributes = {})
@attributes = {}
attributes.each do |attr, value|
@RichOrElse
RichOrElse / filter_fix.rb
Last active May 17, 2016 09:28
Stitch Import fix
module InvoiceServices::Stitch
module FilterFix
refine ActiveSupport::TimeWithZone do
def as_stitch_filter
in_time_zone('Pacific Time (US & Canada)').strftime('%Y-%m-%dT%H:%M:%S')
end
end
refine Date do
def as_stitch_filter
@RichOrElse
RichOrElse / active_record_relation.rb
Created October 5, 2016 05:35
Active Record Relation monkeypatch
module ActiveRecord
class Relation
def to_rows
connection.select_rows(to_sql)
end
def to_result
connection.select_all(to_sql)
end
module Results
refine Symbol do
def [](body, **header)
[self, header, body]
end
end
end
class Results < Struct.new(:status, :header, :body)
Success = Class.new(Results) { def initialize(body, **header) super(:success, header, body) end; def success; yield end; def success?; true end }
@RichOrElse
RichOrElse / status_code.rb
Created July 23, 2017 02:47
Rails http status codes
100 = :continue
101 = :switching_protocols
102 = :processing
200 = :ok
201 = :created
202 = :accepted
203 = :non_authoritative_information
204 = :no_content
205 = :reset_content
206 = :partial_content
class ApplicationController < ActionController::API
include Knock::Authenticable
end
class PaymentController < ApplicationController
before_action :authorize_user
def create
current_user.pay current_billing, payment_params
end
@RichOrElse
RichOrElse / combinators.rb
Last active August 29, 2018 17:46
Adds Function Composition methods to Ruby objects
module FunctionComposition
module Combinators
refine Proc do
def by(*options, &be)
proc { |*input| call(*input, *options, &be) }
end
def of(one, &be)
proc { |*input| call(one, *input, &be) }
end
@RichOrElse
RichOrElse / be.rb
Last active September 3, 2018 14:37
Refinements to use Object#b, Object#to_be and Array#or_be_one
module Be
refine Object do
def be(or_not_to_be = self, *)
or_not_to_be
end
def to_be(&to)
to.be(self)
end
end