Skip to content

Instantly share code, notes, and snippets.

View oleganza's full-sized avatar

Oleg Andreev oleganza

View GitHub Profile
# class Person
# include FindByToken
# token_search :name, :nickname, :email
# end
#
# Person.find_by_token("oleg andreev", :limit => 100) => [...]
#
module FindByToken
DEFAULT_LIMIT = 100
Generates a byte code for the "if" expression.
To understand how it works consider the
following Scheme code:
(if (> 3 8) 10 14)
The expression results in the following byte code:
array('i', [9, 0, 9, 1, 5, 6, 1, 2, 17, 14, 9, 2, 16, 16, 9, 3])
# oleganza 25.11.2008
def total_srand!(srand_key = ENV['SRAND'])
srand_key = (srand_key || 42).to_i
srand(srand_key)
if defined?(UUID) # for uuidtools gem (uses /dev/random, /dev/urandom by default)
::UUID.instance_variable_set(:@random_device, :use_rand_please)
end
srand_key
end
# client.rb
class Person
include DataMapper::Resource
include EMRPC::Pid
after :update, :process_stats
def process_stats
StatServer.send(:process_stats, self, :stats_processed, person.attributes)
end
# Sometimes :symbol.operator looks nice.
# I generally hate "global variables programming",
# but this extension seems to be safe and handy.
def experimental_by_oleganza
# arrays for OR, hashes for AND with in-Symbol operators
all([{:name => ["john", "jane"]}, {:age.gr => 3, :age.lt => 65}])
# same as above, but with optional :any, :all keys (:any for OR, :all for AND)
all([{:name => ["john", "jane"]}, {:any => [{:age.gt => 3}, {:parental_control.not => :nil}], :age.lt => 65}])
@oleganza
oleganza / gist:30701
Created December 1, 2008 10:13 — forked from kpumuk/gist:30569
# Given a set of documents this method returns a list of tags associated with
# those documents ordered by the ones occuring on the most documents. Tags th
# only appear on one doc are excluded from the list.
# If the user supplies a specific tag to exclude it will not be included
# in the list.
def related_tags(docs, exclude_tag = nil)
# DM triggers single SQL query for all docs' tags when doc.tags is called.
docs[0,20].inject({}) do |hash, doc|
doc.tags.inject(hash) do |hsh, tag|
hsh[tag] ||= 0
@oleganza
oleganza / gist:31604
Created December 3, 2008 16:34
auto_sign_in.rb
module AutoSignIn
PARAM = :sign_in_token
protected
# usage:
# before :auto_sign_in
# url(..., auto_sign_in_params(person))
#
def auto_sign_in
if tk = params[PARAM]
# SimpleFixtures is an alternative to dm-sweatshop
#
# Oleg Andreev <oleganza@gmail.com>
# November 24, 2008
# License: WTFPL (http://sam.zoy.org/wtfpl/)
#
module SimpleFixtures
# Define a named fixture. Named fixtures other than :default are merged to :default one
#
# A.define_fixture(:a => 1)
#
# Helps to authenticate person using password and email
#
class Person
Authentication = DeferredModule.new do
property :crypted_password, String, :length => 50, :accessor => :protected # DM bug #663, :lazy => [:auth_data]
property :salt, String, :length => 50, :accessor => :protected # DM bug #663, :lazy => [:auth_data]
attr_accessor :password, :password_confirmation
validates_length :password, :min => 4, :if => :password_required?
validates_is_confirmed :password, :if => :password_required?, :message => "Password and confirmation do not match"
module PluralizedConsts
P = self
def const_missing(sym)
return super(sym) if Thread.current[:pc_const_missed]
begin
super(sym)
rescue NameError => e
begin
Thread.current[:pc_const_missed] = true