Skip to content

Instantly share code, notes, and snippets.

View dmitryame's full-sized avatar

Dmitry Amelchenko dmitryame

View GitHub Profile
@dmitryame
dmitryame / gist:966715
Created May 11, 2011 15:46
jruby hacks
#jruby hacks
jps -ml #show java processes running
jstack <pid> #get a jvm stack info
jruby -J-Djruby.reify.classes=true # a flag that tell jruby to compile ruby classes to real (matching) java classes
jvisualvm #great profiling tool
@dmitryame
dmitryame / gist:934457
Created April 21, 2011 13:21
model embedded collection of object_ids with mongoid
If you set has_and_belongs_to_many on one side only, then you need to
set :inverse_of to nil. For example:
class A
include Mongoid::Document
has_and_belongs_to_many :bs, :inverse_of => nil
end
class B
include Mongoid::Document
end
That should work with no trouble. Since you do not have the
class String
def character_occurance
result = {}
each_char do |character|
result[character] = 0 if result[character] == nil
result[character] += 1
end
result
end
end
@dmitryame
dmitryame / html_safe? override for string
Created April 5, 2011 21:56
this will override the html_safe? implementation of string
class Object
def html_safe?
true
end
end
class String
def html_safe?
@dmitryame
dmitryame / followers association with mongoid twitter style
Created April 2, 2011 20:29
followers association with mongoid twitter style
What is the best way to model user/follower (twitter style) relationships with mongo? Most prominent solution is to embed an array of followers ids in every user object. I'm an idealist, and believe that if we are modeling twitter, we eventually will run into a situation when a user has millions of followers which will blow. The relational many_to_many is not easy to model with mongo, mongo_id tries to offer some implementation of basic associations, but the many_no_many, or has_many through is not there yet and I doubt it ever will be.
My solution is a compromise between relational normalization and "embed everything in one document".
class User
include Mongoid::Document
include Mongoid::Timestamps
# user will have many followerships, each will embedd a follower user,
# the user_id is stored on the followership object
@dmitryame
dmitryame / blog in rails 3 in less then 10 minutes
Created March 30, 2011 12:42
A presentation script to demo how easy it is to create a blog app in 10 minutes with rails
rvm install 1.9.2
rvm use 1.9.2
gem list
gem install rails
gem list
rails new blog
cd blog
@dmitryame
dmitryame / gist:879405
Created March 21, 2011 12:52
Active resource hack to enable connection log trace. Put in initializers
class ActiveResource::Connection
# Creates new Net::HTTP instance for communication with
# remote service and resources.
def http
http = Net::HTTP.new(@site.host, @site.port)
http.use_ssl = @site.is_a?(URI::HTTPS)
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl
http.read_timeout = @timeout if @timeout
#Here's the addition that allows you to see the output
http.set_debug_output $stderr
@dmitryame
dmitryame / Calculate standard deviation in mongo
Created October 8, 2010 14:21
mongo db Standard deviation calculation with map reduce
// sample data
{ "_id" : ObjectId("4caf19200d282159bf000001"), "date" : "2010-10-06", "seq" : "00:00:00,000", "method" : "getUserByScbeId", "duration" : 3 }
{ "_id" : ObjectId("4caf19200d282159bf000002"), "date" : "2010-10-06", "seq" : "00:00:00,116", "method" : "createTicket", "duration" : 116 }
{ "_id" : ObjectId("4caf19200d282159bf000003"), "date" : "2010-10-06", "seq" : "00:00:00,131", "method" : "getCollectionMetadata", "duration" : 11 }
{ "_id" : ObjectId("4caf19200d282159bf000004"), "date" : "2010-10-06", "seq" : "00:00:00,137", "method" : "getParticipation", "duration" : 6 }
{ "_id" : ObjectId("4caf19200d282159bf000005"), "date" : "2010-10-06", "seq" : "00:00:00,139", "method" : "updateSocialObjectModified", "duration" : 371 }
{ "_id" : ObjectId("4caf19200d282159bf000006"), "date" : "2010-10-06", "seq" : "00:00:00,143", "method" : "getUserByScbeId", "duration" : 4 }