Skip to content

Instantly share code, notes, and snippets.

Some questions about optional dependencies

1. A small ruby library

I'm writing a small Ruby library:

class FizzBuzzEndpoint

  def call(env)
// data density example
// shows how storing/sorting data by key can reduce I/O drastically in MongoDB
// diskloc gives file # and offset of a given document, divide by 512 for block #
// 2011 kcg
// start clean
db.disktest_noorg.drop();
db.disktest_org.drop();
// create some random data in non userid dense form
import pymongo
import random
from collections import defaultdict
import time
import sys
from pymongo import Connection
connection = Connection()
connection.drop_database('test_db')
collection = connection['test_db']['testing']
@bogn
bogn / v1_group_serializer.rb
Created April 29, 2014 21:09
versioned API through inheritance
module Export
module V1
class GroupSerializer < ActiveModel::Serializers
attributes :items
def items
docs = object.items.order_by(:position.asc)
ItemsSerializer.new(docs, special_options).serializable_hash
end
@bogn
bogn / gist:9467807
Created March 10, 2014 15:57
short inject statement
def self.fully_approved_query_value
MultiTenancy.current_version.locales.inject({}) do |result, locale|
result.merge(locale.to_s => true)
end
end
@bogn
bogn / group_by_caching.rb
Last active January 4, 2016 09:09
Naming is hard
module Models
module GroupByCaching
extend ActiveSupport::Concern
included do
CACHE_KEY_PREFIX = "#{self.name.collectionize}_grouped_by"
end
module ClassMethods
@bogn
bogn / gist:6291691
Last active December 21, 2015 10:28 — forked from powmedia/gist:5161061
Select2
/**
* Select2
*
* Renders Select2 - jQuery based replacement for select boxes
*
* Simply pass a 'config' object on your schema, with any options to pass into Select2.
* See http://ivaynberg.github.com/select2/#documentation
*/
Backbone.Form.editors.Select2 = Backbone.Form.editors.Base.extend({
@bogn
bogn / move_embedded_to_toplevel.rb
Last active December 19, 2015 08:49
Mongoid: Moving embedded records (recursively_embeds_many) to top-level. Not actually happy about it, as I don't have the experience with Mongoid to decide whether setting the state like this is risky. But it seems Mongoid 3.1.4 doesn't offer a more elegant way to do this. Feedback very welcome.
class Group
include Mongoid::Document
recursively_embeds_many
end
# remove from old parent
@group.parent_group.child_groups.delete @group
# set new state
# multilingual analysis for title search taken from :
# http://jprante.github.com/lessons/2012/05/16/multilingual-analysis-for-title-search.html
# or alternatively here (where the used plugin comes from, is less descriptive but has more technical details)
# https://github.com/yakaz/elasticsearch-analysis-combo
analysis:
filter:
germansnow:
type: snowball
language: German2
ngram_filter:
@bogn
bogn / ngram-wrong-highlight.sh
Last active December 16, 2015 09:09
A combo analyzer using the sub analyzers German2 snowball and ngram is yielding strange results for me (see comments). elasticsearch 0.20.2. elasticsearch-analysis-combo 1.1.0. Same goes for elasticsearch 0.90.RC2 and elasticsearch-analysis-combo 1.3.0.
curl -XDELETE 'localhost:9200/test'
echo
curl -XPUT 'localhost:9200/test?pretty' -d '{
"mappings" : {
"test" : {
"properties" : {
"text_de" : {
"type" : "string"
}