Skip to content

Instantly share code, notes, and snippets.

@mrk21
mrk21 / mocha
Last active April 2, 2016 07:55
Fix an issue of the invisible color of mocha on solized dark.
#!/bin/bash
substitution='s/\x1b\[90m/\x1b[92m/g'
$(cd $(dirname $0); pwd)/__mocha -c "$@" > >(perl -pe "$substitution") 2> >(perl -pe "$substitution" 1>&2)
@mrk21
mrk21 / plantuml.rb
Created July 13, 2016 00:00
Make UML diagrams from a PlantUML script embedded in Markdown
require 'pp'
require 'fileutils'
file = ARGV[0]
dirname = File.basename(file,'.*')
FileUtils.mkdir_p dirname
source = File.read(file)
i = 1
source.gsub(/(?<=<!---).+?(?=--->)/m) do |m|
@mrk21
mrk21 / paperclip_radis.rb
Created March 15, 2017 02:09
Paperclip stored Redis
# @see https://gist.github.com/basgys/5712426#gistcomment-1274422
class UploadFile
include ActiveModel::Model
include Paperclip::Glue
# Paperclip required callbacks
define_model_callbacks :save, only: [:after]
define_model_callbacks :commit, only: [:after]
define_model_callbacks :destroy, only: [:before, :after]
@mrk21
mrk21 / graphql_ruby_class_based_api_test.rb
Last active November 17, 2017 12:22
A test code for class based API that is graphql-ruby 1.8.pre1 new feature(see: https://github.com/rmosolgo/graphql-ruby/blob/1fa7a2a4/guides/schema/class_based_api.md).
Bundler.require
class Hoge
attr_accessor :id, :name_bar
def initialize(id: nil, name_bar: nil)
self.id = id
self.name_bar = name_bar
end
end
@mrk21
mrk21 / midpoint_displacement.rb
Created August 14, 2018 23:47
中点変位法
def midpoint_displacement(size, rect = [], x = 0, y = 0, depth = 0)
result = {}
avg_height = ->(heights) {
heights.sum / heights.size.to_f
}
rand_height = ->(h) {
(rand - 0.5) * (2.0 ** -(depth * h))
}
@mrk21
mrk21 / has_url_helpers.rb
Created November 8, 2018 01:25
UrlHelper for Models
module HasUrlHelpers
extend ActiveSupport::Concern
# URL Helpers for Models
# @see https://github.com/rails/rails/blob/aa3dcabd874a3e82e455e85a1c94a7abaac2900a/actionpack/lib/action_controller/metal/url_for.rb
# @see https://github.com/rails/rails/blob/b13a5cb83ea00d6a3d71320fd276ca21049c2544/actionpack/lib/abstract_controller/url_for.rb
# @see https://github.com/rails/rails/blob/14d3c7c2c9b89fe76a3677f99d102dd6ca729927/actionpack/lib/action_dispatch/routing/url_for.rb
# @see https://github.com/rails/rails/blob/14d3c7c2c9b89fe76a3677f99d102dd6ca729927/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
# @see https://github.com/rails/rails/blob/cba27402388380d672199a1a46de0a06ef383332/actionpack/lib/action_dispatch/routing/route_set.rb#L504
class UrlHelpers
on run
repeat
set pid to executeAutoClicker()
log pid
delay 5
activateMinecraft()
delay 1
toggleAutoClick()
delay 60
killAutoClicker(pid)
@mrk21
mrk21 / time_with_zone.rb
Created December 26, 2018 16:52
Thread-safe `Time#in_time_zone(zone)` on pure Ruby
class Time
@@tz_mutex = Mutex.new
# @param zone [String] Time zone ID(e.g. +'Asia/Tokyo'+)
# @example
# require 'time_with_zone' # This module
# require 'time'
# t = Time.parse('2018-09-12T10:00:00Z')
# t.in_time_zone('America/Los_Angeles').dst? # => true
# t.in_time_zone('Asia/Tokyo').dst? # => false
@mrk21
mrk21 / userdata.sh
Last active January 15, 2019 16:45
Register private hosted zone
#!/bin/bash
# Register private hosted zone
# @see [aws 起動毎にローカル IP をローカル DNS (Route53) の A レコードに突っ込む - Qiita](https://qiita.com/selfnavi/items/a731514cd8174d86caf5)
INSTANCE_ID=`curl http://169.254.169.254/latest/meta-data/instance-id | sed -e 's/^i-//g'`
IPV4=`curl http://169.254.169.254/latest/meta-data/local-ipv4`
HOSTNAME=`echo "app-:instance-id:.example.local" | sed -e "s/:instance-id:/${INSTANCE_ID}/g"`
HOSTED_ZONE_ID=<hosted-zone-id>
cat <<JSON > /root/register-route53.json
@mrk21
mrk21 / admin_foo_models.rb
Last active February 1, 2019 07:59
Fast CSV downloading for Rails
ActiveAdmin.register FooModel do
extend CsvDumpableResource
end