Skip to content

Instantly share code, notes, and snippets.

@kml
kml / mongoid_extensions_strict_document.rb
Last active August 29, 2015 14:25
Mongoid::Extensions::StrictDocument
# encoding: utf-8
# https://gist.github.com/kml/ce164e1e0c1bde2ab96d
require "mongoid"
module Mongoid
module Extensions
module DefaultScopeOnlyId
def self.included(base)
require "active_attr"
require "memoist"
class Model
include ActiveAttr::Model
extend Memoist
# by default ActiveAttr calls typecaster every time attribute reader is called
# this patch adds memoization
# https://github.com/cgriego/active_attr/blob/v0.8.5/lib/active_attr/attributes.rb#L199
class ClosedStruct < OpenStruct
# https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L176
def method_missing(mid, *args)
return super if @table.has_key?(mid.to_sym)
raise NoMethodError, "undefined method `#{mid}' for #{self}"
end
end
@kml
kml / Gemfile_puma_single_worker_pidfile_patch.rb
Last active December 9, 2015 14:01
Create pidfile before loading the application. Thanks to this monit/god knows that process exists and will not try to run other instance event when initialization takes a long time.
gem "puma", require: false, platform: :jruby
# Issue: https://github.com/puma/puma/issues/785
begin
require "puma/single"
class ::Puma::Single
alias unpatched_run run
@kml
kml / README.md
Last active October 13, 2015 08:33
RecordPlayer

Record responses and play them from local cache.

Installation:

git clone https://gist.github.com/4368cae5de3f7384fd08.git recordplayer
mkdir ~/.recordplayer

Usage:

@kml
kml / rails_eager_load_i18n_translations_and_unicode_database.rb
Last active October 15, 2015 11:49
Eager load data before first request
# encoding: utf-8
# Source: https://gist.github.com/kml/79bd8cedf64caadd18ca
require "active_support/i18n_railtie"
# Eager load translations.
# https://github.com/svenfuchs/i18n/blob/master/lib/i18n/backend/simple.rb
# rails c
# I18n.backend.initialized?
# encoding: utf-8
phrase = "sterownik".force_encoding("utf-8")
regexp = /(?i-mx:^bram[ay])|(?i-mx:(prz[e??]s[l??][ao]|element)[a-z??????]* ogrodz[a-z??????]*)|(?i-mx:(sterowniki?|automatyka).*\bbramy?)|(?i-mx:^tralk[ai])|(?i-mx:bojlery?|pojemno.ciow[ey])|(?i-mx:[a-z]+grzewacze? wody)/
puts phrase.match(regexp).inspect
=begin
UTF8Encoding.java:35:in `length': java.lang.ArrayIndexOutOfBoundsException: 9
from BaseUTF8Encoding.java:91:in `mbcToCode'
@kml
kml / active_support_disable_circular_references_check.rb
Last active November 10, 2015 09:31
Remove circular references protection in ActiveSupport::JSON::Encoding::Encoder
# encoding: utf-8
# Source: https://gist.github.com/kml/3395d73d874b0c302224
# https://github.com/rails/rails/blob/3-2-stable/activesupport/lib/active_support/json/encoding.rb
require "active_support/json/encoding"
unless defined?(ActiveSupport::JSON::Encoding::CircularReferenceError)
abort "#{__FILE__}: CircularReferenceError is not defined. Remove this patch!"
end
# encoding: utf-8
# Usage:
# jruby coercible_coercer_object_method_missing_patch_bench.rb
# jruby coercible_coercer_object_method_missing_patch_bench.rb patch
# Issue: https://github.com/solnic/coercible/pull/22
if $0 == __FILE__
require "bundler/inline"
# encoding: utf-8
# Source: https://gist.github.com/kml/349d3ae1716e6500cbee
# Benchmark: https://gist.github.com/kml/0fb1588fc1b6ba4c60fa
# Issue: https://github.com/solnic/coercible/pull/22
# https://github.com/solnic/coercible/blob/master/lib/coercible/coercer/object.rb#L163
require "coercible"
if Coercible::VERSION != "1.0.0"