Skip to content

Instantly share code, notes, and snippets.

View alainravet's full-sized avatar

Alain Ravet alainravet

  • Braine l'Alleud, Belgium
View GitHub Profile
module DefaultValues
def has_default_values(default_values = {})
cattr_accessor :default_values
self.default_values = default_values
after_initialize :assign_default_values
include InstanceMethods
require 'active_record'
require 'database_cleaner'
connection_info = YAML.load_file("config/database.yml")["test"]
ActiveRecord::Base.establish_connection(connection_info)
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
@alainravet
alainravet / service object with on_success and on_failure hooks.rb
Last active August 29, 2015 14:20
Service Object with on_success and on_failure blocks
#----------------------
# Library code:
class BasicServiceObject
attr_reader :object
def initialize(o)
@object = o
end
end
@alainravet
alainravet / RPM calculator.rb
Last active August 29, 2015 14:21
a RPN calculator in 19 lines of Ruby
def RPN(*data)
stack = []
data.each do |el|
case el
when :+, :-, :*, :/, :&, :|, :<< # 1,2,3,:*,:+ == 1 + 2 $ 3
a, b = stack.pop(2)
stack.push a.send(el.to_sym, b)
when Class # 3,Float == Float(3)
stack.push send(el.name, stack.pop)
when Symbol # '1+2',:eval == eval('1+2')
@alainravet
alainravet / print_stacktrace.rb
Created January 24, 2009 21:12
How to print the stacktrace in Ruby
# How to print the stacktrace from anywhere :
# 1°
#------------------------------------------------------------------------------
module Kernel
def print_stacktrace
raise
rescue
puts $!.backtrace[1..-1].join("\n")
end
@alainravet
alainravet / gist:79077
Created March 14, 2009 14:11
benchmark your ruby tests
# modified version of http://github.com/gittobi/tobi_test_timer
# differences :
# - prints all the tests
# - adds a counter and a bargraph
#
# output :
#. 336 2222222 : 2.584 - test_create_an_image_with_some_participations_in_one_go(ImageAdvancedTest)
#. 337 ++ : 0.173 - test cleanup_user_picture_path() does not translate proper upload
#. 338 ++ : 0.135 - test cleanup_user_picture_path() translates tmp upload path(ImagePathRepairTest)
#. 339 : 0.005 - test shorten_image_url() clears 1 level(ImagePathRepairTest)
# download and git methods swiped from http://github.com/Sutto/rails-template/blob/07b044072f3fb0b40aea27b713ca61515250f5ec/rails_template.rb
require 'open-uri'
def download(from, to = from.split("/").last)
#run "curl -s -L #{from} > #{to}"
file to, open(from).read
rescue
puts "Can't get #{from} - Internet down?"
exit!
@alainravet
alainravet / smtp_tls.rb
Created July 20, 2009 17:46
lib/smtp_tls.rb
require "openssl"
require "net/smtp"
Net::SMTP.class_eval do
private
def do_start(helodomain, user, secret, authtype)
raise IOError, 'SMTP session already started' if @started
check_auth_args user, secret, authtype if user or secret
@alainravet
alainravet / .autotest
Created August 4, 2009 18:43
~/autotest config file
#autotest configuration
#
require 'redgreen/autotest'
require 'autotest/timestamp'
# ####################
# GROWL CONFIG
# ####################
require 'rubygems'
require 'parallel' #http://github.com/grosser/parallel
$stdout.sync = true # Auto-flush for interactive I/O
def slow_action
def fibo(n)
0 == n ? 0 :
1 == n ? 1 :