Skip to content

Instantly share code, notes, and snippets.

View ReSTARTR's full-sized avatar
:octocat:

Masaki Yoshida ReSTARTR

:octocat:
View GitHub Profile
@ReSTARTR
ReSTARTR / helloworld_controller.php
Created October 9, 2010 14:50
XHProf CustomPanel
<?php
class HelloworldController extends AppController
{
var $components = array(
'DebugKit.Toolbar' => array(
'xhprof' => array('configs' => array(
'XHPROF_ROOT' => '/var/vhosts/xhprof', // xhprof_lib,xhprof_htmlを含むディレクトリ
'XHPROF_SOURCE_NAME' => "helloworld", // 適当な値でOK。
'XHPROF_VIEWER_DOMAIN' => 'http://xhprof.localhost', // xhprofビューアのドメイン
'sortBy' => 'mu' // ソートキー。指定がない場合は'wt'の降順ソート。
@ReSTARTR
ReSTARTR / FileReader.scala
Created February 6, 2011 14:15
'with'文を使ったファイルオープン(かつ自動クローズ)
def swith(fileName: String)(f: Iterator[String]=>Unit) {
val in = new FileInputStream(fileName)
try {
f(Source.fromInputStream(in).getLines)
} finally {
in.close
}
}
// 使用例
swith("foo.txt") { lines =>
@ReSTARTR
ReSTARTR / CasbahSample.scala
Created March 6, 2011 12:45
sbt project setting.
import com.mongodb.casbah.Imports._
object CasbahSample {
val conn = MongoConnection()
val db = conn("casbah_test")
val collection = db("sample")
// 下記のように1行にまとめてもでもOK
// val collection = MongoConnection()("casbah_test")("sample")
def createRecordAndSave {
import com.novus.salat._
import com.novus.salat.global._
import com.mongodb.casbah.Imports._
case class User(id: Int, name: String, age: Int)
case class UserA(id: Int, name: String, age: Int)
case class UserB(id: Int, name: String, salary: Int)
case class Group(group_id: Int, name: String, leader: User, members: List[User])
object SalatSample {
@ReSTARTR
ReSTARTR / PhpSerializer.scala
Created March 13, 2011 14:36
string that is serialized by php convert to Map[Any, Any]
class PhpSerializer {
val patternString = """s:(\d+):\"([^;]*)\";?""".r
val patternInt = """i:(\d+);?""".r
val patternDouble = """d:(-)?(\d+)(\.\d+)?;?""".r;
val patternArray = """a:(\d+):\{(.*)\};?""".r;
def parseArray(len: Int, attr: String): Any = {
val childs = attr.split(";").toList
val len1 = childs.length
if (len1 > 1) {
@ReSTARTR
ReSTARTR / background.html
Created May 8, 2011 15:39
chrome notification snippet
<script>
(function() {
var def_notification_timeout=5000;
var def_username = 'yoshida';
function show(msg) {
var noti = webkitNotifications.createNotification(
'icon.png',
'From: my notification',
@ReSTARTR
ReSTARTR / RameeProject.scala
Created May 9, 2011 13:41
DotCloud sbt Project sample
import sbt._
class AppnameProject(info: ProjectInfo) extends DefaultWebProject(info) {
val JETTY7 = "7.3.1.v20110307"
val servletapi = "javax.servlet" % "servlet-api" % "2.5" % "compile"
val jetty7Server = "org.eclipse.jetty" % "jetty-server" % JETTY7 % "compile,test"
val jetty7Servlets = "org.eclipse.jetty" % "jetty-servlets" % JETTY7 % "compile,test"
val jetty7Webapp = "org.eclipse.jetty" % "jetty-webapp" % JETTY7 % "compile,test"
@ReSTARTR
ReSTARTR / OptionTypeTest.scala
Created May 18, 2011 14:21
Option型の動作確認
// execute in REPL
def testA(x:Boolean):Option[String] = if (x) Some("hoge") else None
testA(true) // Option[String] = Some(hoge)
testA(false) // Option[String] = None
testA(false).getOrElse("moge") // String = moge
testA(false).getOrElse(false) // Any = false
val result = testA(false).getOrElse("moge") // String = moge
@ReSTARTR
ReSTARTR / MonbodbServlet.scala
Created May 28, 2011 10:13
Access from Scala to MongoDB on DotCloud.
package main.scala
import javax.servlet.http.{HttpServlet,HttpServletRequest,HttpServletResponse}
import org.eclipse.jetty.server.Server
import java.io.PrintWriter
import java.net.URLEncoder
class MongoServlet extends HttpServlet {
import com.mongodb.casbah.Imports._
val conn = MongoConnection("mongo.example.dotcloud.com",5907)
@ReSTARTR
ReSTARTR / Project.scala
Created August 15, 2011 14:24
Data access test with Squeryl
import sbt._
import Process._
class Project(info: ProjectInfo) extends DefaultProject(info) {
val specs2 = "org.specs2" %% "specs2" % "1.5" % "test"
val squeryl = "org.squeryl" %% "squeryl" % "0.9.4"
val mysqlDriver = "mysql" % "mysql-connector-java" % "5.1.17"
}