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' | |
| 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!) |
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 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 |
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 'base64' | |
| class BasicAuth | |
| def initialize(app, user_name:, password:) | |
| @app = app | |
| @user_name = user_name | |
| @password = password | |
| end | |
| def call(env) |
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
| """ | |
| 以下の論文で提案された改良x-means法の実装 | |
| クラスター数を自動決定するk-meansアルゴリズムの拡張について | |
| http://www.rd.dnc.ac.jp/~tunenori/doc/xmeans_euc.pdf | |
| """ | |
| import numpy as np | |
| from scipy import stats | |
| from sklearn.cluster import KMeans |
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 Object | |
| def behaves_like?(object) | |
| expected_methods = | |
| if object.respond_to?(:instance_methods) | |
| object.instance_methods | |
| else | |
| object.methods | |
| end | |
| (expected_methods - methods).empty? |
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
| 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 |
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 '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 |
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 "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/ |
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
| 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) |
OlderNewer