Mon, 09 Jul 2007
Exception handling
./lib/mymodule.rb
module MyModule

  class Error < RuntimeError; end
  class ConnectionError < Error; end

  def connect
    
    if something_failed?
      raise ConnectionError, "connection failed due to..."
    end
  end
end
somewhere in controller
  include MyModule

  def do_connect
    begin
      connect
    rescue MyModule::ConnectionError => err
      flash.now[:notice] => err
    end
  end

(posted at 01:15 | filed under programming/rails | link)    (comments | add new)
own application configuration
./config/my_configuration.yml
MyConfig:
 username: my_username
 password: my_password
 host: my_host
./config/environment.rb
require 'ostruct'
require 'yaml'

OwnConfigFile = "#{RAILS_ROOT}/config/my_configuration.yml"
if File.exist?(MyConfigFile)
  ::ApplicationConfig = OpenStruct.new(YAML.load_file(MyConfigFile))
end
in application:
config = ApplicationConfig.MyConfig;
puts config['username']

(posted at 01:08 | filed under programming/rails | link)    (comments | add new)