Skip to content

Instantly share code, notes, and snippets.

Avatar

Oleg Andreev oleganza

View GitHub Profile
View f.rb
# Subject: f.rb
# From: Oleg Andreev <oleganza@gmail.com>
# Date: November 9, 2008
module Kernel
# Private functions for mixins (to avoid private methods clash)
# Usage:
# module MyModule
# def my_api_method
# F.some_private_function()
View find_by_token.rb
# class Person
# include FindByToken
# token_search :name, :nickname, :email
# end
#
# Person.find_by_token("oleg andreev", :limit => 100) => [...]
#
module FindByToken
DEFAULT_LIMIT = 100
View gist:25529
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])
View gist:29048
# 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
View gist:29421
# 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
View oleganza-suggestion.rb
# 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}])
View gist:30701
# 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
View gist:31604
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]
View simple_fixtures.rb
# 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)
View authentication.rb
#
# 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"