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 / HenceOrOtherwise.rb
Last active May 5, 2020 02:05
Ruby refinements for the conditional averse Rubyist
module HenceOrOtherwise
refine Object do
def hence
yield self
end
def otherwise
self
end
end # Object refinement
@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
a. "car".split().map{ |text| text.reverse }.join("")
b. "japan is awesome".split().reverse
c. [5, 6, 2, 9, 1].sort.reverse[0]
d. [1, 2, 2, 4, 2, 3, 7, 3].uniq!
problem 1. The function does is to receive an array and every content in the array will be string of arrays. I think its
much esier if you do this.
def bracketed_list(values)