This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "active_support/core_ext/hash/transform_values" | |
class MatchData | |
# Returns a hash containing the names and matches of captures or nil. | |
# | |
# A key of the hash is a name of the named captures. A value of the hash is a | |
# matched string or an array of corresponding matches. | |
# | |
# puts /(?<foo>.)(?<bar>.)(?<baz>.)(?<baz>.)/.match("hoge", &:named_captures).inspect | |
# # => {"foo"=>"h", "bar"=>"o", "baz"=>["g", "e"]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Standart behavior: if you eager load an association with a specified :limit option, it will be ignored, returning all the associated objects | |
# This example shows how to eager load limited number of records in collection. | |
# Usage: | |
# Post.find(42).likes.size # => 300 | |
# Post.find(42).likes.limit_per_target(5).size # => 5 | |
# Post.preload(:likes).map {|t| t.likes.size } # => [100, 200, 300, ... ] | |
# Post.preload(:limited_likes).map {|t| t.likes.size } # => [5, 5, 5, ... ] | |
# | |
# recommended: postgres ~>9.4, ruby ~>2.2, rails ~>4.2, squeel |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
class Plutil | |
module JSON | |
def self.load(plist) | |
Plutil.convert plist, to: :json do |converted_io| | |
::JSON.load(converted_io) | |
end | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/sidekiq/active_job_logging_hook.rb | |
require 'sidekiq/middleware/server/logging' | |
class Sidekiq::ActiveJobLoggingHook < Sidekiq::Middleware::Server::Logging | |
def call(worker, item, queue) | |
klass = item['args'].try(:first).try(:[], 'job_class').presence || worker.class.to_s | |
Sidekiq::Logging.with_context("#{klass} JID-#{item['job_id'] || item['jid']}") do | |
begin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MigrateHstoreToJson < ActiveRecord::Migration | |
def up | |
rename_column :posts, :data, :data_hstore | |
add_column :posts, :data, :jsonb, default: {}, null: false, index: { using: 'gin' } | |
execute 'UPDATE "posts" SET "data" = json_object(hstore_to_matrix("data_hstore"))::jsonb' | |
remove_column :posts, :data_hstore | |
end | |
def down | |
rename_column :posts, :data, :data_jsonb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
VERBOSE="false" | |
while getopts "v" flag; do | |
case "${flag}" in | |
v) VERBOSE="true" ;; | |
esac | |
done | |
readonly VERBOSE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#= SplatStruct | |
# `Struct` subclass with splats support | |
# | |
# SampleClass = SplatStruct.new(:a, :b, [:c]) do |klass| | |
# def avgc | |
# Rational(c.inject(:+), c.length).to_f | |
# end | |
# end | |
# | |
# object = SampleClass.new(1,2,3,4,5) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>beforeRunningCommand</key> | |
<string>nop</string> | |
<key>command</key> | |
<string>#!/usr/bin/env ruby -wU | |
unless ENV['TM_FILENAME'] =~ /^(?:Gemfile|.*\.gemspec)$/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# = Kernel#switch | |
# Provides alternative switch-case syntax. | |
# | |
# # support methods: | |
# value = [] | |
# switch value do | |
# on empty?: -> { "none" } | |
# on one?: -> { "one" } | |
# on many?: -> { "many" } | |
# end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# helpers/bootstrap_helper/tabbable.rb | |
# Bootstrap 2 tabs helper method for Ruby on Rails | |
# http://getbootstrap.com/2.3.2/components.html | |
# | |
# == Usage (slim): | |
# | |
# = tabbable do |t| | |
# = t.section :tab_1 do | |
# / Content for Tab #1 |