Skip to content

Instantly share code, notes, and snippets.

View jeroenr's full-sized avatar

Jeroen Rosenberg jeroenr

View GitHub Profile
@jeroenr
jeroenr / git-pre-receive-hook.sh
Created June 4, 2012 21:23
Detects when feature branches are being created or deleted
track_branches() {
# --- Arguments
oldrev=$(git rev-parse $1)
newrev=$(git rev-parse $2)
refname="$3"
case "$refname" in
refs/heads/*)
branch=$(expr "$refname" : "refs/heads/\(.*\)")
featurebranch=$(expr "$branch" : "\(.*/.*\)")
@jeroenr
jeroenr / _searchbox.html.erb
Created July 16, 2012 11:02
Ajax search with Rails 3, UJS and jQuery waypoints
<%= form_tag '/trip', :remote => true, :id => 'searchbox' do -%>
<div id="main" >
<label>Search</label>
<%= text_field_tag :text, @text, :id => 'text' %>
</div>
</div>
<% end -%>
@jeroenr
jeroenr / test.rb
Created July 19, 2012 08:59
Wtf Ruby
def f(a = "", b = {}, c = "", d = "")
puts "a=#{a}"
puts "b=#{b}"
puts "c=#{c}"
puts "d=#{d}"
end
f(c: "c", d: "d")
@jeroenr
jeroenr / Gemfile
Created July 19, 2012 09:34
Make will_paginate generate ajax links
source 'https://rubygems.org'
gem 'rails', '3.2.3'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
@jeroenr
jeroenr / Accommodation.rb
Created August 7, 2012 17:09
Use ActiveResource with non-Rails REST API
class Accommodation < ActiveResource::Base
class << self
def element_path(id, prefix_options = {}, query_options = nil)
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
"#{prefix(prefix_options)}#{collection_name}/#{id}#{query_string(query_options)}"
end
def collection_path(prefix_options = {}, query_options = nil)
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
"#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}"
@jeroenr
jeroenr / WeirdIntegerParser.scala
Last active November 15, 2017 09:26
Scala pattern matching with regex: parsing weird integer strings
val MaxInt = """(inf)""".r
val NormalInt = """(\d*)""".r
def parseInt(integerString:String): Int = {
integerString match {
case MaxInt(_) => Integer.MAX_VALUE
case NormalInt(_) => Integer.valueOf(integerString)
case _ => throw new NumberFormatException(String.format("Cannot parse %s as an integer", integerString))
}
}
@jeroenr
jeroenr / index.html.erb
Created August 31, 2012 15:26
Use ERB template to pass rendered HTML to backbone router (BLEHHH)
<div id="posts"></div>
<script type="text/javascript">
$(function() {
// Blog is the app name
window.router = new Blog.Routers.PostsRouter({posts: <%= @posts.to_json.html_safe -%>});
Backbone.history.start();
});
</script>
@jeroenr
jeroenr / posts_router.js.coffee
Created August 31, 2012 15:28
Fetch JSON from server based on backbone model URL and render backbone view
class Blog.Routers.PostsRouter extends Backbone.Router
routes:
"posts/:page" : "index"
".*" : "index"
index: (page) ->
pageNumber = page || 1
@posts = new Blog.Collections.PostsCollection()
@view = new Blog.Views.Posts.IndexView({model:@posts, page:parseInt(pageNumber)+1})
@jeroenr
jeroenr / Build.scala
Last active December 20, 2015 23:19
Multi select faceted search on dynamic index with Play!, scalastic and elastic search
import sbt._
import Keys._
import play.Project._
object ApplicationBuild extends Build {
val appName = "tweetsearch"
val appVersion = "1.0-SNAPSHOT"
val appDependencies = Seq(
@jeroenr
jeroenr / JsonUtil.scala
Last active September 12, 2018 08:28
Easy JSON (un)marshalling in Scala with Jackson
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
object JsonUtil {
val mapper = new ObjectMapper with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
def toJson(value: Map[Symbol, Any]): String = {