Skip to content

Instantly share code, notes, and snippets.

case class JsonRpcBody[P <: Product](jsonrpc: String, method: String, params: P, id: String)
case class SampleParams(_1: String, _2: Int) extends Product2[String, Int]
// JsonRpcBody[SampleParams] にデコードするためには、DecodeJson[JsonRpcBody[SampleParams]] を
// 定義する必要がある。しかし、SampleParams 以外にも増えるのは予想され、その度に DecodeJson[JsonRpcBody[OtherParams]] などを
// 書くと大変めんどう。params の解釈の仕方が違うだけなのに、jsonrpc, method, id の部分も解釈しなきゃだし。
// と言うわけで以下のように書く。
implicit def jsonRpcBodyDecodeJson[P <: Product](implicit paramsDecodeJson: DecodeJson[P]): DecodeJson[JsonRpcBody[P]] =
import user
import message
object Main {
def main(args: Array[String]) {
import MessageSender._
val natsuki = User("Natsuki Ando")
val sakura = User("Sakura Domyoji")
// IDみたいな仕組みがあって
trait Identifier[A] {
val value: A
}
case class UserId(value: UUID) extends Identifier[UUID]
case class GroupId(value: UUID) extends Identifier[UUID]
object Main {
implicit class Capping[+A](value: A) {
def cap[B >: A](limit: B)(implicit ordering: Ordering[B]): B = {
if (ordering.lteq(value, limit)) value else limit
}
}
def main(args: Array[String]) {
println( 99.cap(100)) // => 99
println(101.cap(100)) // => 100
class Violation(message: String)
class Funny[V](value: V) {
def validateWith(validator: Validator[V]): Validity[V] = validator(value)
}
object Funny {
def apply[V](value: V): Funny[V] = new Funny(value)
}
@takkkun
takkkun / unicorn.cap
Last active December 25, 2015 10:59
lib/capistrano/tasksにでも置いて使う
set :unicorn_config_path, -> { current_path.join('config', 'unicorn.rb') }
set :unicorn_pid_path, -> { current_path.join('tmp', 'pids', 'unicorn.pid') }
set :unicorn_env, -> { fetch(:rails_env) || fetch(:stage) }
SSHKit.config.command_map[:unicorn_stop] = 'kill -QUIT'
SSHKit.config.command_map[:unicorn_restart] = 'kill -USR2'
namespace :deploy do
def unicorn_running?
trait Expression {
def +(right: Expression) = Addition(this, right)
def *(right: Expression) = Multiplication(this, right)
def **(right: Expression) = Exponential(this, right)
}
object Expression {
implicit def intToConst(value: Int) = Const(value)
}
var Color = function() {
if (arguments.length == 1)
[this.r, this.g, this.b] = Color.parse(arguments[0]);
else if (arguments.length == 3)
[this.r, this.g, this.b] = arguments;
else
throw 'invalid number of arguments, takes one or three arguments';
};
Color.parse = function(color) {
@takkkun
takkkun / user.rb
Last active December 19, 2015 01:19
class User < ActiveRecord::Base
attr_accessor :current_phase
def self.thereafter(phase)
-> do
current_phase.nil? or current_phase >= phase
end
end
validates :email, presence: true, if: thereafter(1)
module Definable
def self.redefine(object, method_name, &process)
original_method = object.method(method_name)
object.define_singleton_method(method_name) do |*args, &block|
process[*args, &block]
original_method[*args, &block]
end
end