Archive for June, 2008

creating a tar.gz archive

tar -pczf name_of_your_archive.tar.gz /path/to/directory

truncating files to zero length in the bash shell

> some_file.ext

calling named_scope in a loop doesn’t seem to work in rails 2.1.0 ?

#  # Raises NoMethodError: undefined method `processing'
#  %(pending processing complete error).each do |s|
#    named_scope s.to_sym,
#      :conditions => ["state = ?", s],
#      :order => "priority, created_at"
#  end

named_scope :pending,
:conditions => "state = 'pending'",
:order => "priority, created_at"

named_scope :processing,
:conditions => "state = 'processing'",
:order => "priority, created_at"

named_scope :complete,
:conditions => "state = 'complete'",
:order => "priority, created_at"

named_scope :errorneous,
:conditions => "state = 'error'",
:order => "priority, created_at"

generating print optimized pdf documents from html and css using apache fop and css2xslfo

# Uses css2xslfo and apache fop to generate a print optimized pdf document
# from an html document and a css stylesheet. This works by converting the
# styled html to an xslfo document, which then gets translated to the final
# pdf document. For further details on apache fop and how wo write appropriate
# css stylesheets for css2xslfo, see http://xmlgraphics.apache.org/fop/ and
# http://www.re.be/css2xslfo/

module Css2fopRenderer

class PdfGenerationException < Exception; end
class HtmlGenerationException < Exception; end

# overwrite for customization
def after_html_generation; end

def css2fop(template_file, html_file, pdf_file, base_url, erb_binding = nil)
render_html(template_file, html_file, erb_binding)
after_html_generation
render_pdf(html_file, pdf_file, base_url)
end

def render_pdf(html_file, pdf_file, base_url)
success = execute_css2fop(html_file, pdf_file, base_url)
unless success && $?.exitstatus == 0 && (f = File.stat(pdf_file)) && f.size > 0
raise PdfGenerationException
end
end

def render_html(template_file, html_file, erb_binding = nil)
template = ERB.new(File.read(template_file))
File.open(html_file, "w") { |f| f.puts template.result(erb_binding) }
unless (f = File.stat(html_file)) && f.size > 0
raise HtmlGenerationException
end
end

# a shell script is used so that sysadmins can customize the fop installation
def execute_css2fop(html_file, pdf_file, base_url)
system "#{ENV['css2fop-pdf']} #{html_file} #{pdf_file} #{base_url}"
end

end

calling rake tasks with parameters

namespace :backup do

task :dump_dynamic_tables => :environment do

ENV[TABLES] = 'users'
Rake::Task['db:fixtures:export_for_tables'].invoke

end

end

sex spam

“Don’t let your giant sleep” — tergange1974@mariopuzo.com

ruby heredocs as parameters

irb(main):001:0> class_name = "Foo"
=> "Foo"
irb(main):002:0> eval(<
irb(main):003:1″   class #{class_name}
irb(main):004:1″     def foo
irb(main):005:1″       :foo
irb(main):006:1″     end
irb(main):007:1″   end
irb(main):008:1″ EOS
=> nil
irb(main):009:0> f = Foo.new
=> #
irb(main):010:0> f.foo
=> :foo

capistrano, thin, remote_cache, safe credentials and symlinked vendor/rails

set :application, "coolapp"
set :repository,  "http://snusnu.info/svn/#{coolapp}/trunk/coolapp"

set :deploy_via, :remote_cache
set :deploy_to, "/u/apps/coolapp"

role :app,  "snusnu.info", :primary => true

# rely on ssh-agent or the likes to serve our private key on authentication
ssh_options[:port] = 22
ssh_options[:username] = 'coolio'
ssh_options[:host_key] = "ssh-rsa"
ssh_options[:auth_methods] = %w(publickey)

# workaround for capistrano-2.3 bug
ssh_options[:keys] = %w(~/.ssh/id_dsa.snusnu.info ~/.ssh/id_rsa.snusnu.info)

# taken from Advanced Rails Recipes
desc "Watch multiple log files at the same time"
  task :tail_log, :roles => :app do
  stream "tail -f #{shared_path}/log/production.log"
end

# call this if 'deploy_via :remote_cache' is set
# and you want to deploy a different tag or branch
# inspired from Jonathan Weiss' explanation of the issue
# http://blog.innerewut.de/2008/3/12/remote-cache-pitfalls
task :delete_remote_cache, :roles => :app do
  run "rm -rf #{shared_path}/cached-copy"
end

namespace :symlink do

  task :vendor_rails, :roles => :app do
    run "ln -s #{shared_path}/rails #{release_path}/vendor/rails"
  end

end

after "deploy:update_code", "symlink:vendor_rails"

namespace :thin do

  %w(start stop restart).each do |action|
    desc "#{action.to_s.capitalize} the application's thin server(s)"
    task action.to_sym, :roles => :app do
      run "thin #{action} -C #{deploy_to}/current/config/thin.yml"
    end
  end
end

namespace :deploy do

  # we don't need sudo for cleanup
  before("deploy:cleanup") do
    set :use_sudo, false
  end

  # taken from Advanced Rails Recipes
  desc "Runs after every successful deployment"
  task :after_default do
    cleanup
  end

  # taken from Advanced Rails Recipes
  task :copy_database_configuration do
    production_db_config = "#{deploy_to}/shared/production.database.yml"
    run "cp #{production_db_config} #{release_path}/config/database.yml"
  end

  after "deploy:update_code", "deploy:copy_database_configuration"

  %w(start stop restart).each do |action|
    desc "#{action.to_s.capitalize} the thin server(s)"
    task action.to_sym do
      find_and_execute_task("thin:#{action}")
    end
  end

end

!nil is true

irb(main):001:0> nil
=> nil
irb(main):002:0> !nil
=> true
irb(main):003:0> !!nil
=> false
irb(main):004:0> nil && true
=> nil
irb(main):005:0> true && nil
=> nil
irb(main):006:0> !!nil && true
=> false
irb(main):007:0> true && !!nil
=> false

sex spam

“Get your lassie immediately turned on by the only glance at your mighty weapon!” — Lisa Sosa

« Older Entries