Skip to content

Instantly share code, notes, and snippets.

@localhostdotdev
localhostdotdev / 1-active-support-test-case-let-it.rb
Last active April 23, 2019 02:58
adds let and it to ActiveSupport::TestCase
class ActiveSupport::TestCase
parallelize(workers: :number_of_processors)
def self.let(name, &block)
@@lets ||= SimpleHash.new
@@lets[name] = block
end
def self.it(&block)
name = "test_"
@localhostdotdev
localhostdotdev / 1-graphql inspect everything.query
Last active April 21, 2019 22:43
graphql api inspect / introspect / list fields / values / etc.
query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
subscriptionType { name }
types {
...FullType
}
directives {
name
@localhostdotdev
localhostdotdev / simple-hash.rb
Created April 20, 2019 18:31
Hash based SimpleHash (instead of HashWithIndifferentAccess)
class SimpleHash < Hash
def method_missing(method_name, *args, &block)
if keys.map(&:to_s).include?(method_name.to_s)
if args.empty? && block.nil?
send(:simple_fetch, method_name)
else
raise "can't pass arguments and/or blocks"
end
else
super
@localhostdotdev
localhostdotdev / performance of method_missing vs fetch vs [] vs Hash#[].rb
Created April 20, 2019 17:20
performance of method_missing vs fetch vs [] vs Hash#[]
class U
def self.compare(reports, n: 10)
Benchmark.bm do |x|
reports.each do |report|
if report.is_a?(Proc)
x.report { n.times { report.call } }
else
x.report(report.first) { n.times { report.last.call } }
end
end
@localhostdotdev
localhostdotdev / 1-test-struct-hash-open-struct-simple-hash-etc.rb
Last active April 24, 2019 17:46
testing different kind of key-value structures
$struct = Struct.new(:name, :writing?).new("localhostdotdev", true)
$hash = Hash[name: "localhostdotdev", writing?: true]
$openstruct = OpenStruct.new(name: "localhostdotdev", writing?: true)
$hashwithindifferentaccess = HashWithIndifferentAccess[name: "localhostdotdev", writing?: true]
$yourcustomclass = "<insert your class here>"
$simplehash = SimpleHash[name: "localhostdotdev", writing?: true]
def try(code)
puts code
p eval(code, binding)
@localhostdotdev
localhostdotdev / application-record-sql.rb
Last active April 14, 2019 09:17
ApplicationRecord.sql('select * from users left join accounts on accounts.user_id = users.id and accounts.kind = ?', 'something')
class ApplicationRecord < ActiveRecord::Base
def self.sql(sql, *values)
ActiveRecord::Base.connection.execute(
ActiveRecord::Base.send(:sanitize_sql_array, [sql, *values])
).each.to_a
end
end
users = ApplicationRecord.sql('select * from users')
users_and_accounts = ApplicationRecord.sql('select * from users left join accounts on accounts.user_id = users.id and accounts.kind = ?', 'something')
@localhostdotdev
localhostdotdev / using-biquery-ruby-free.md
Created April 13, 2019 12:10
using bigquery with ruby for free

using bigquery with ruby for free

main website asks for credit card, console/web ui just doesn't work.

.env:

# frozen_string_literal: true
require 'test_helper'
class SignUpTest < ActionDispatch::IntegrationTest
test 'can user sign up' do
get users_path
assert_response :success
@localhostdotdev
localhostdotdev / factory.rb
Last active April 10, 2019 01:14
preview of a simple factory thing for ruby which I use instead of factorybot / fixtures
class Factory
def self.create(clazz, *args)
build(clazz, *args).tap(&:save!)
end
def self.build(clazz, *args)
"Factory::#{clazz}".constantize.new(clazz, *args).tap(&:process).record
end
def self.create_list(clazz, count, *args)
set tabstop=2 shiftwidth=2 expandtab
set autoindent
set smarttab
syntax on
set shell=/bin/bash
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()