MyFunnyDev

web, coding and beyond

Cron in Ruby

without comments

Check out how Whenever gem can simplify cron configuration.

Written by Michał Kuklis

July 27th, 2009 at 2:10 am

Posted in Uncategorized

Tagged with ,

key-value store

without comments

Key/Value DB

Written by Michał Kuklis

July 26th, 2009 at 11:40 am

Posted in Uncategorized

daemon_controller + Thinking Sphinx

with 3 comments

I’ve created simple rails initiator in order to start Sphinx through daemon_controller based on the Thinking Sphinx configuration. I hope it will help somebody.

require 'daemon_controller'
 
def before_start
  if not ThinkingSphinx.define_indexes?
    config = ThinkingSphinx::Configuration.instance
    cmd = "#{config.bin_path}#{config.indexer_binary_name} --config \"#{config.config_file}\" --all"
    cmd << " --rotate" if ThinkingSphinx.sphinx_running?
    system cmd
  end
end
 
if defined?(ThinkingSphinx)
  if not ThinkingSphinx.sphinx_running? 
    conf_instance = ThinkingSphinx::Configuration.instance
    @controller = DaemonController.new(
      :identifier => 'Sphinx search server',
      :start_command => "#{conf_instance.bin_path}#{conf_instance.searchd_binary_name} --pidfile --config \"#{conf_instance.config_file}\"",
      :before_start => method(:before_start),
      :ping_command => lambda { TCPSocket.new(conf_instance.configuration.searchd.address, conf_instance.configuration.searchd.port) },
      :pid_file => conf_instance.configuration.searchd.pid_file,
      :log_file => conf_instance.configuration.searchd.log)
      @controller.start
  end
end

Written by Michał Kuklis

July 22nd, 2009 at 2:13 am

Posted in Uncategorized

Tagged with ,

The Rails Initialization Process

without comments

I found really nice description of rails initialization process here.

Written by Michał Kuklis

July 22nd, 2009 at 1:08 am

Posted in Uncategorized

Tagged with

ruby maxins in rails plugins

without comments

Very often when looking at the code in rails plugins you can run into this:

module Taggable 
  def self.included(base)
    base.extend(ClassMethods)
  end
  module module ClassMethods
    #methods here
  end
end

This is a part of a bigger pattern which is shown below:

module ModuleA
  def self.included(base)
    # add class methods from ModuleB
    base.extend(ModuleB)
  end
end
 
module ModuleB
  def act_as_hello
    p "hello from module B"
  end
end
 
class ClassC
 #class body here
end
 
# include moduleA in classC
ClassC.send(:include, ModuleA)
 
class ClassD < ClassC
  act_as_hello
end
 
classD = ClassD.new

The pattern is used often when developing plugins with ActiveRecord. What we gain by inheriting from ClassC (class ClassD < ClassC) are instance methods from ModuleA. This is done by:

ClassC.send(:include, ModuleA)

Moreover since ModuleA is included in ClassC, ModuleA’s initializer def self.included(base) will be invoked at the time ModuleA is mixed with ClassC. The invocation will call base.extend(ModuleB). In this case base represents ClassC which will be extended by adding class methods from ModuleB. The ModuleA’s init method is shown again below:

def self.included(base)
  # add class methods from ModuleB to ClassC
  base.extend(ModuleB)
end

At the end our ClassD has now access to all class methods defined in ModuleB. act_as_hello will be called during ClassD initialization:

class ClassD < ClassC
  act_as_hello
end

Written by Michał Kuklis

July 20th, 2009 at 9:16 pm

Posted in Ruby, ruby on rails

Tagged with ,

capistrano recipes for ubuntu

without comments

Few nice capistrano recipes which may help you automate Ubuntu Server setup:

Written by Michał Kuklis

July 19th, 2009 at 8:25 pm

Posted in Uncategorized

Tagged with , ,

Decoration in Ruby

without comments

Interesting discussion about object decoration in ruby.

Written by Michał Kuklis

June 30th, 2009 at 9:59 pm

Posted in Uncategorized

Tagged with

Class and Instance Variables In Ruby

without comments

Written by Michał Kuklis

June 13th, 2009 at 9:27 pm

Posted in Ruby

few more ruby links

without comments

Written by Michał Kuklis

May 18th, 2009 at 7:24 pm

Posted in Ruby

Tagged with

drop all tables and sequences in oracle

without comments

tables:

SELECT 'DROP TABLE ' || table_name || ' CASCADE CONSTRAINTS;' FROM user_tables;

sequences:

SELECT 'DROP SEQUENCE ' || sequence_name || ';' FROM user_sequences;

Written by Michał Kuklis

May 18th, 2009 at 11:07 am

Posted in Uncategorized

Tagged with