Skip to content

Instantly share code, notes, and snippets.

@Altech
Altech / modification-mode.el
Created May 19, 2013 05:33
simple minor modes to decorate buffer text(bold, underlie).
(define-minor-mode modi-temporary-mode
"presentation mode of modi."
nil
" modi:temporary"
(list
(cons (kbd "C-;") 'modi:underline-region-temporary)
(cons (kbd "C-'") 'modi:bold-region-temporary)
(cons (kbd "C-.") 'modi:remove-decorations-of-region))
(modi:visualize-decorations-of-buffer))
@Altech
Altech / squeeze.rb
Created June 16, 2013 07:36
squeeze in Ruby.
class String
def squeeze(c)
s = ""
now = false
0.upto(self.size-1) do |i|
if self[i] == c
if now
next
else
s << self[i]
@Altech
Altech / rails_template.rb
Created August 16, 2013 10:18
my rails init script.
# Gems
# ==================================================
# Segment.io as an analytics solution (https://github.com/segmentio/analytics-ruby)
gem "analytics-ruby"
# For encrypted password
gem "bcrypt-ruby"
# Useful SASS mixins (http://bourbon.io/)
gem "bourbon"
@Altech
Altech / modify_json.rb
Created October 18, 2013 03:28
modify json interactively on Ruby data structure.
#!/usr/bin/env ruby
def modify_json(file_path)
require 'json'
require 'colorize'
require 'pry'
raise "The file is not exist!".red if not File.exists? file_path
json = JSON.parse(File.read(file_path))
puts "json.keys: " + json.keys.join(",")
binding.pry
@Altech
Altech / kvs.erl
Created October 25, 2013 05:55
simple kvs server in erlang.
-module(kvs).
-export([start/0, store/2, lookup/1]).
start() -> register(kvs, spawn(fun() -> loop() end)).
store(Key, Value) -> rpc({store, Key, Value}).
lookup(Key) -> rpc({lookup, Key}).
rpc(Q) ->
@Altech
Altech / preloader.rb
Created June 21, 2017 07:49
Preloader for RESTful API using ActiveModelSerializers
module Api
## Used in ActionController
class Preloader
# @param [Array] attributes fields to select.
# @param [Hash] associations associations to include and fields to select of them.
# @example When you select id and name from companies,
# preload_for(companies, [:id, :name], {})
# @example When you includes posts and employees and employees avatar,
# preload_for(companies, [], {posts: {}, employees: {avatar: {}}})
def self.preload_for(rel, attributes, associations)
@Altech
Altech / patch.rb
Last active November 5, 2017 11:50
A patch to ActiveModel::Serializer
module ActiveModel
class Serializer
# @api private
def relationship_value_for(association, adapter_options, adapter_instance)
return association.options[:virtual_value] if association.options[:virtual_value]
association_serializer = association.serializer
association_object = association_serializer && association_serializer.object
return unless association_object
# [Note] @Altech
module Api::ControllerHelpers
extend ActiveSupport::Concern
included do
include InstanceMethods
before_action :prepare_restful_params
end
module InstanceMethods
def prepare_restful_params
module Api
class PreloadRule
def initialize
@attributes, @associations = {}, {}
end
attr_accessor :attributes, :associations
def attribute(name, includes:)
unless name.is_a?(Symbol)
module Api
class Preloader
# @param [Array] attributes fields to select.
# @param [Hash] associations associations to include and fields to select of them.
# @example When you select id and name from companies,
# preload_for(companies, [:id, :name], {})
# @example When you includes posts and employees and employees avatar,
# preload_for(companies, [], {posts: {}, employees: {avatar: {}}})
def self.preload_for(rel, attributes, associations)
unless rel.is_a?(ActiveRecord::Relation)