Skip to content

Instantly share code, notes, and snippets.

View yasaichi's full-sized avatar
😇

Yuichi Goto yasaichi

😇
View GitHub Profile
@yasaichi
yasaichi / camelize_keys.rb
Last active August 29, 2015 14:16
Hash#camelize_keys
require 'active_support/core_ext'
class Hash
unless method_defined?(:camelize_keys)
def camelize_keys(first_letter = :upper)
transform_keys { |key| key.camelize(first_letter) rescue key }
end
end
unless method_defined?(:camelize_keys!)
@yasaichi
yasaichi / keys.rb
Created March 13, 2015 01:28
rails/rails/blob/4-2-stable/activesupport/lib/active_support/core_ext/hash/keys.rb
class Hash
def transform_keys
return enum_for(:transform_keys) unless block_given?
result = self.class.new
each_key do |key|
result[yield(key)] = self[key]
end
result
end
@yasaichi
yasaichi / basic_auth.rb
Created April 5, 2015 09:11
パーフェクト Ruby on Rails 10章
require 'base64'
class BasicAuth
def initialize(app, user_name:, password:)
@app = app
@user_name = user_name
@password = password
end
def call(env)
@yasaichi
yasaichi / config.ru
Created April 5, 2015 09:28
パーフェクト Ruby on Rails 10章
require './basic_auth'
App = lambda do |env|
[200, { "Content-Type" => "text/html" }, ["Hello, Rack world!"]]
end
use BasicAuth, user_name: 'test:user', password: 'pass'
run App
@yasaichi
yasaichi / behaves_like.rb
Last active August 29, 2015 14:19
Object#behaves_like?
class Object
def behaves_like?(object)
expected_methods =
if object.respond_to?(:instance_methods)
object.instance_methods
else
object.methods
end
(expected_methods - methods).empty?
@yasaichi
yasaichi / neighbor_records.rb
Created April 19, 2015 13:30
Instance methods for ActiveRecord to fetch the "neighbor" records
module NeighborRecords
def previous_one(col_name = :id)
condition = self.class.arel_table[col_name].lt(public_send(col_name))
self.class.where(condition).order(col_name => :desc).first
end
def next_one(col_name = :id)
condition = self.class.arel_table[col_name].gt(public_send(col_name))
self.class.where(condition).order(col_name => :asc).first
end
@yasaichi
yasaichi / attr_accessor_extension.rb
Last active August 29, 2015 14:19
Implementation "#to_h", returning a hash which includes the attribute name and its value
require 'set' # Railsの場合不要
module AttrAccessorExtension
def to_h
Hash[self.class.__send__(:attributes).sort.map { |name| [name, public_send(name)] }]
end
def self.included(klass)
klass.extend(ClassMethods)
end
@yasaichi
yasaichi / okuribito.rb
Last active May 21, 2016 04:41
draft of okuribito
require "yaml"
require "active_support"
require "active_support/core_ext"
config = YAML.load_file("okuribito.yml")
class Okuribito
CLASS_METHOD_SYMBOL = "."
INSTANCE_METHOD_SYMBOL = "#"
PATTERN = /\A(?<symbol>[#{CLASS_METHOD_SYMBOL}#{INSTANCE_METHOD_SYMBOL}])(?<method_name>.+)\z/
module AppName::JourneyRouterPatch
APP_NAME_GENERIC_ROUTING_INFO_KEY = "app_name.generic_routing_info".freeze
def call(env)
response_array = super
# NOTE: When Journey::Router#call can't resolve a request, it returns an array like:
# [404, {'X-Cascade' => 'pass'}, ['Not Found']]
# ref. https://github.com/rails/journey/blob/v1.0.4/lib/journey/router.rb#L80
if response_array[1]["X-Cascade"] != "pass" && env.key?(APP_NAME_GENERIC_ROUTING_INFO_KEY)
<?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>ANSIBlackColor</key>
<data>
YnBsaXN0MDDUAQIDBAUGFRZYJHZlcnNpb25YJG9iamVjdHNZJGFyY2hpdmVyVCR0b3AS
AAGGoKMHCA9VJG51bGzTCQoLDA0OVU5TUkdCXE5TQ29sb3JTcGFjZVYkY2xhc3NPECcw
LjM1Mjk0MTE3NjUgMC40MzkyMTU2ODYzIDAuNTIxNTY4NjI3NQAQAYAC0hAREhNaJGNs
YXNzbmFtZVgkY2xhc3Nlc1dOU0NvbG9yohIUWE5TT2JqZWN0XxAPTlNLZXllZEFyY2hp