Navigation Menu

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 / functions.php
Last active July 23, 2023 09:32 — forked from tilap/functions.php
Wordpress: How to automatically attache youtube/vimeo thumbnail as a post feature image from a post meta key
<?php
// https://github.com/WebDevStudios/Automatic-Featured-Images-from-Videos/blob/1.2.0/automatic-featured-images-from-videos.php
/**
* If a YouTube or Vimeo video is added in the post content, grab its thumbnail and set it as the featured image.
*
* @since 1.0.0
*
* @param int $post_id ID of the post being saved.
* @param string $video_thumbnail_url URL of the image thumbnail.
@ippeiukai
ippeiukai / cron.conf
Last active July 31, 2021 21:00
Running cron in AWS ElasticBeanstalk web tier.
files:
"/tmp/crontab":
mode: "000777"
owner: 'ec2-user'
group: 'ec2-user'
content: |
30 02 * * * sudo /usr/sbin/execute-in-eb-node-app 'node bin/is-eb-master.js' && sudo /usr/sbin/execute-in-eb-node-app 'npm run daily-maintenance'
encoding: plain
container_commands:
01-copy_eb_bin:
@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 / subset-of.ts
Created November 14, 2017 02:21
`SubsetOf` meta type
export type SubsetOf<T> = {
[P in keyof T]?: T[P];
};
@ippeiukai
ippeiukai / check-whether-eb-master-instance.js
Created October 5, 2016 02:05
For running something only on one instance in an ElasticBeanstalk environment. Inspired by http://blog.rotaready.com/scheduled-tasks-elastic-beanstalk-cron/
"use strict";
const AWS = require('aws-sdk');
const bluebird = require('bluebird');
/**
* @returns {Promise.<boolean>} - is on 'master' instance or not
*/
module.exports = bluebird.coroutine(function* (opts) {
if (opts == null) {
@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
$ irb
irb(main):001:0> class Fixnum
irb(main):002:1> def plus_with_two_plus_two_is_five(other)
irb(main):003:2> other += 1 if self == 2 && other == 2
irb(main):004:2> plus_without_two_plus_two_is_five(other)
irb(main):005:2> end
irb(main):006:1> alias_method :plus_without_two_plus_two_is_five, :+
irb(main):007:1> alias_method :+, :plus_with_two_plus_two_is_five
irb(main):008:1> end
Rubinius::InvalidBytecode: instruction argument is not a Fixnum: code: call, ip: 13
module Rubinius
config = {}
config[:config_file] = "/Users/ippei/src/rubinius-3.33/config.rb"
config[:command_line] = ["--prefix=/Users/ippei/.rvm/rubies/rbx-3.33", "--with-opt-dir=/usr/local/opt/openssl:/usr/local/opt/readline:/usr/local/opt/libyaml:/usr/local/opt/gdbm", "--llvm-path=/usr/local/Cellar/llvm/3.6.2"]
config[:build_make] = "make"
config[:build_rake] = "rake"
config[:build_perl] = "perl"
config[:llvm_path] = "/usr/local/Cellar/llvm/3.6.2"
config[:llvm_system_name] = nil
config[:llvm_configure] = "/usr/local/opt/llvm/bin/llvm-config"
@ippeiukai
ippeiukai / association_exists.rb
Created February 14, 2016 13:30
association_exists model plugin for Sequel. Like association_join, allows you to filter dataset with association.
module Sequel
module Plugins
module AssociationExists
module DatasetMethods
def association_exists(association, &block)
_association_exists(association, &block)
end