Skip to content

Instantly share code, notes, and snippets.

🐼
fuzzy logic

Josh Goodall inopinatus

🐼
fuzzy logic
Block or report user

Report or block inopinatus

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@inopinatus
inopinatus / digest_queries.rb
Created Apr 25, 2019
SHA digest queries for ActiveRecord + postgresql
View digest_queries.rb
# frozen_string_literal: true
module DigestQueries
def predicate_builder
@predicate_builder ||= super.tap do |pb|
pb.register_handler(Digest::Instance, DigestHandler.new(pb))
end
end
class DigestHandler
@inopinatus
inopinatus / table_to_csv.rb
Last active Jan 12, 2019 — forked from sandys/table_to_csv.rb
convert a html table to CSV using ruby
View table_to_csv.rb
# run using ```rvm jruby-1.6.7 do jruby "-J-Xmx2000m" "--1.9" tej.rb```
require 'rubygems'
require 'nokogiri'
require 'csv'
f = File.open("table.html")
doc = Nokogiri::HTML(f)
CSV.open("output.csv", 'w') do |csv|
@inopinatus
inopinatus / verify_and_decrypt_session_cookie52.rb
Created Jul 29, 2018
Decrypt Rails 5.2 session cookies
View verify_and_decrypt_session_cookie52.rb
require 'cgi'
require 'active_support'
def verify_and_decrypt_session_cookie(cookie, secret_key_base = Rails.application.secret_key_base)
cookie = CGI::unescape(cookie)
salt = 'authenticated encrypted cookie'
encrypted_cookie_cipher = 'aes-256-gcm'
serializer = ActiveSupport::MessageEncryptor::NullSerializer
key_generator = ActiveSupport::KeyGenerator.new(secret_key_base, iterations: 1000)
View simpsonian.rb
def list((head, *tail))
case tail.length
when 0
head&.fetch(:name).to_s
when 1
"#{list [head]} & #{list tail}"
else
"#{list [head]}, #{list tail}"
end
end
@inopinatus
inopinatus / invoice.rb
Last active Jun 25, 2018
json models embedded in your activerecord models
View invoice.rb
class Invoice < ApplicationRecord
include JsonModel::Attribute
json_attribute_model :payment, Payment
validates_associated :payment
end
@inopinatus
inopinatus / hash_proc_lookup.rb
Created Jun 21, 2018
Cheap lookups with Hash#to_proc
View hash_proc_lookup.rb
[:foo, :bar].map(&{ bar: "BAZ", foo: "QUUX" })
@inopinatus
inopinatus / unique_collected_validator.rb
Last active Jul 12, 2018
Solve unique validation problem within nested simultaneous destruction and creation
View unique_collected_validator.rb
class UniqueCollectedValidator < ActiveRecord::Validations::UniquenessValidator
def initialize(options)
unless options[:within].present?
raise ArgumentError, ":within option is required to determine the unique scope's collection."
end
unless Array(options[:within]).all? { |within| within.respond_to?(:to_sym) }
raise ArgumentError, "#{options[:within]} is not a supported format for the :within option. " \
"Pass a symbol or an array of symbols instead: `within: :profiles`"
end
super
@inopinatus
inopinatus / attachable_sti_type.rb
Last active Jun 19, 2018
Remapping type column values using the Rails 5 Attributes API, good for both STI and polymorphic belongs_to
View attachable_sti_type.rb
# lib/attachable_sti_type.rb
class AttachableStiType < ActiveRecord::Type::String
def cast_value(value)
if value =~ /\A([a-z]+)\/([a-z]+)\Z/
"#{$2}/#{$1}_record".classify
else
super
end
end
@inopinatus
inopinatus / gist:4e936864ab3d5b79b8f050bce39b3667
Created Jun 16, 2018 — forked from steveclarke/gist:1411146
Git: Setting up a Remote Repository and Doing Initial Push
View gist:4e936864ab3d5b79b8f050bce39b3667

Setup remote repository:

ssh git@example.com
mkdir my_project.git
cd my_project.git
git init --bare

On local machine:

cd my_project
@inopinatus
inopinatus / fibers_with_blocks.rb
Created Jun 2, 2018
Block yielding to a block, via fibers.
View fibers_with_blocks.rb
# importer/exporter interaction via fibers
require 'fiber'
class Importer
DATA = [
["foo", "bar"],
["jazz", "quux"],
["cats", "dogs"]
]
You can’t perform that action at this time.