Skip to content

Instantly share code, notes, and snippets.

@ippeiukai
ippeiukai / sequel_each_in_batches.rb
Last active March 26, 2024 16:01
ActiveRecord's find_each and find_in_batches ported to Sequel. Sequel's paged_each is not practical when converting large data due to its use of transaction and offset. (Special thanks to @nomuson for working out the primary_keys_expr logic together.)
# ActiveRecord's find_each and find_in_batches ported to Sequel.
# Sequel's paged_each is not practical when converting large data due to its use of transaction and offset.
#
# Usage:
#
# SequelEachInBatches.find_each(dataset, keys) { |record| ... }
#
# It can also monkey patch Sequel::Dataset:
#
# Sequel::Dataset.send(:include, SequelEachInBatches)
@ippeiukai
ippeiukai / sequelize-find-each.js
Last active March 22, 2021 12:57
Sequelize port of find_each in ActiveRecord. (https://github.com/sequelize/sequelize/issues/686 )
"use strict";
const Sequelize = require('sequelize');
const Promise = Sequelize.Promise;
const DEFAULT_BATCH_SIZE = 3000;
/**
* Port of ActiveRecord::Base.find_each of Rails.
@ippeiukai
ippeiukai / singleton_with_class_delegator.rb
Created June 17, 2016 07:53
Singleton but as convenient as class methods; it's both.
module SingletonWithClassDelegator
extend ActiveSupport::Concern
included do
include Singleton
original_delegate_method = method(:delegate).unbind if self.respond_to?(:delegate)
extend SingleForwardable
if original_delegate_method.present?
# restore the delegate of ActiveSupport that has been overshadowed by SingleForwardable
define_singleton_method :delegate, original_delegate_method
@ippeiukai
ippeiukai / faster_fixture_loading.rb
Last active December 18, 2015 09:59
A monkey patch that speeds up fixture loading of Rails 3.2. The speed up in my case was by five-fold (with ``time rake db:fixtures:load``), quite significant if you have a large set of fixtures and load them regularly.
# for Rails 3.2.12
# loads fixtures a lot faster
require 'active_record/fixtures'
class ActiveRecord::Fixtures
# based on activerecord-3.2.12/lib/active_record/fixtures.rb l.462
# modified lines are marked CHANGED
def self.create_fixtures(fixtures_directory, table_names, class_names = {})
table_names = [table_names].flatten.map { |n| n.to_s }
module TwoPlusTwoIsFive
def self.included(base)
base.class_exec do
alias_method :plus_without_two_plus_two_is_five, :+
private :plus_without_two_plus_two_is_five
alias_method :+, :plus_with_two_plus_two_is_five
public :+
end
end
@ippeiukai
ippeiukai / sequel_abstract_model.rb
Last active November 25, 2015 09:47
Monkey patch to Sequel (http://github.com/jeremyevans/sequel) that lets you cleanly create an abstract model.
require 'sequel/model'
module Sequel
class << self
# abstract model needs to avoid inherited callback
# (see https://groups.google.com/forum/#!msg/sequel-talk/OG5ti9JAJIE/p1iqO57cwqwJ)
# MyAbstractModel = Sequel.new_abstract_model do
# # ... your common stuff ...
module MongoidEmbeddedObjectModification
# Custom field behaves more like an immutable value.
# Your changes to the object obtained with getter method will not take an effect unless you assign it back.
# https://github.com/mongoid/mongoid/issues/3341
def modify(mongoid_doc, field_name)
obj = mongoid_doc.public_send(field_name)
yield obj
mongoid_doc.public_send("#{field_name}=", obj)
end
@ippeiukai
ippeiukai / io_streaming_response_body.rb
Last active November 25, 2015 09:22
Stream data as a file in Rails as they are written to IO.
class IOStreamingResponseBody
def self.render(options = {}, &block)
instance = new(options)
instance.define_singleton_method :render do |dummy_io|
block.call(dummy_io)
end
instance
end
@ippeiukai
ippeiukai / enumerator_with_position_and_size.rb
Created August 27, 2015 02:14
Add `with_position_and_size` method to Enumerator. Those are quite handy inside a loop.
module EnumeratorWithPositionAndSize
# @yields [original_params, position_and_size]
# @yieldparam position_and_size [(Integer, Integer)] 2-element array of position (first being 1) and size; size may be nil (see Enumerator#size)
# @example
# %w[a b c d e].each.with_position_and_size { |char, (position, size)| puts "#{char} (#{position}/#{size})" }
def with_position_and_size
return to_enum(__method__) unless block_given?
my_size = self.size
self.with_index(1) do |original_params, position|
@ippeiukai
ippeiukai / use_single_forwardable_with_active_support.rb
Created May 14, 2015 02:29
Have you ever wanted to use def_single_delegator from SingleForwardable but still continue to use `delegate` of ActiveSupport?
module UseSingleForwardableWithActiveSupport
extend ActiveSupport::Concern
included do
original_delegate_method = method(:delegate).unbind if self.respond_to?(:delegate)
extend SingleForwardable
if original_delegate_method.present?
# restore the delegate of ActiveSupport that has been overshadowed by SingleForwardable
define_singleton_method :delegate, original_delegate_method