Skip to content

Instantly share code, notes, and snippets.

View patorash's full-sized avatar
👓
I'm sleepy

patorash patorash

👓
I'm sleepy
View GitHub Profile
@patorash
patorash / deep_sort.rb
Created February 14, 2022 10:48
HashとArrayで再帰的にソートしたかったのだが、複雑なkeyやvalueのケースは考慮したくなかったのでgemにするのは諦めたやつ。Refinementsを使ってコアクラスを汚染しないようにした。
# frozen_string_literal: true
module DeepSort
class Error < StandardError; end
refine Hash do
def deep_sort
keys = self.keys
raise DeepSort::Error, "Invalid Keys(#{keys})" unless keys.all? { |k| k.is_a?(String) || k.is_a?(Symbol) || k.is_a?(Numeric) }
@patorash
patorash / elasticsearch_relation_connection.rb
Last active June 17, 2021 04:13
ElasticsearchRelationConnection for graphqu-ruby with elasticsearch-model
module Connections
class ElasticsearchRelationConnection < GraphQL::Pagination::RelationConnection
def nodes
@nodes ||= limited_nodes.records.to_a
end
# Rubocopにload_nodesメソッドが不要と言われた
# しかし、継承元のRelationConnectionで呼ばれているのでnodesメソッドのエイリアスにしておく
# また、元々private methodだったので変更しておく
alias_method :load_nodes, :nodes
@patorash
patorash / add_column_comment_from_enum.rb
Created February 24, 2021 09:49
Add column comment from ActiveRecord::Enum. with enum_help gem.
class AddColumnCommentFromEnum < ActiveRecord::Migration[6.0]
def up
table_with_columns.each do |table_name, columns|
next unless I18n.exists?("enums.#{table_name.singularize}")
columns.map(&:name).each do |column_name|
next unless I18n.exists?("enums.#{table_name.singularize}.#{column_name}")
model = table_name.classify.constantize
enums = model.send(column_name.pluralize)
@patorash
patorash / add_column_comment_from_i18n.rb
Last active February 18, 2021 03:00
Add column comment from I18n
class AddColumnCommentFromI18n < ActiveRecord::Migration[6.0]
def up
table_with_columns.each do |table_name, columns|
columns.map(&:name).each do |column_name|
next unless I18n.exists?("activerecord.attributes.#{table_name.singularize}.#{column_name}")
change_column_comment(
table_name,
column_name,
I18n.t("activerecord.attributes.#{table_name.singularize}.#{column_name}"),
)
@patorash
patorash / mackerel.rake
Last active February 17, 2021 09:35
Post custom metrics(table count, column count, and comment count) to mackerei.io
require 'mackerel/client'
namespace :mackerel do
namespace :service_metric do
namespace :database do
desc 'Post table count, column count, and comment count to mackerel.'
task post: :environment do
client = Mackerel::Client.new(mackerel_api_key: ENV['MACKEREL_API_KEY'])
service_name = ENV['MACKEREL_SERVICE_NAME']
time = Time.zone.now.to_i
@patorash
patorash / .zshrc
Last active August 18, 2023 00:30
公開用.zshrc。rbenv, nodenv, goenv, pyenv, zplug, peco, ghqをインストールしてあること。
export PATH="~/bin:$PATH"
export GOENV_ROOT="$HOME/.goenv"
export PATH="$GOENV_ROOT/bin:$PATH"
eval "$(goenv init -)"
export RBENV_ROOT="$HOME/.rbenv"
export PATH="$RBENV_ROOT/bin:$PATH"
eval "$(rbenv init -)"
export NODENV_ROOT="$HOME/.nodenv"
@patorash
patorash / .zshrc
Last active June 27, 2022 02:57
zsh functions
# ctrl + Rで、pecoでhistoryを参照
function peco-select-history() {
# historyを番号なし、逆順、最初から表示。
# 順番を保持して重複を削除。
# カーソルの左側の文字列をクエリにしてpecoを起動
# \nを改行に変換
BUFFER="$(history -nr 1 | awk '!a[$0]++' | peco --query "$LBUFFER" | sed 's/\\n/\n/')"
CURSOR=$#BUFFER # カーソルを文末に移動
zle -R -c # refresh
}
@patorash
patorash / turbolinks_form_get.js
Last active April 17, 2020 02:50
Support GET from with Turbolinks.
@patorash
patorash / .dockerignore
Last active February 25, 2020 02:41
Macの開発環境をDockerにした際のファイル達
.circleci
.docker
.idea
.git
.github
doc
.env*
.gitignore
.ruby-version
@patorash
patorash / peco_git_commit.fish
Created March 12, 2019 07:29
historyから選択した文字列でgit commitのメッセージをセットする。
function peco_git_commit --description="Select git commit message by history"
if set -q $argv
history | peco | read line
else
history | peco --query $argv | read line
end
if test -n "$line"
commandline "git commit -m \"$line\""
end
set -e line