Friday, May 25, 2007

ActiveRecord testing ouside of Rails

There is many tips about how to use ActiveRecord outside of Rails but rarely about ActiveRecord testing ouside of Rails. I am working on project that use ActiveRecord ouside of Rails and need to create fixture for testing. It take me a little bit of time to figure out how to do. The source code below demonstrates the trick

require 'test/unit'
require "active_record"
require "active_record/fixtures"

ActiveRecord::Base.establish_connection(
 :adapter => "mysql",
 :host => "localhost",
 :username => "nightlybatch",
 :password => "secret",
 :database => "web_orders"
)

#this is because fixture verify existence of rails database configuration
ActiveRecord::Base.configurations[:test] = ActiveRecord::Base.connection
  
class Product < ActiveRecord::Base
end

class TestFoo < Test::Unit::TestCase
 #set fixture path explicitly
 self.fixture_path="c:/temp"
 
 fixtures :products

 def test_ruby_on_rails_should_exist
  assert products(:ruby_on_rails)
 end

end

In order to pass the test, the fixture file products.yml shall be created in directory c:/temp, and products table shall be created in MySQL database
#c:/temp/products.yml
ruby_on_rails:
   id:   1
   name: ruby on rails
rails_recipes:
   id:   2
   name: rails recipes

Thursday, May 24, 2007

Ruby adoption in Vietnam

From the time, I know ruby, it is my favorite language, but it seems that no so many developer in Vietnam is using it. Today I have found Vietnam Ruby site. It is good news, that ruby got an attention in Vietnam and I am not alone, who see the huge benefit of wonderful and humane programming language.

Saturday, May 19, 2007

Notia

At my early time in Notia (in 1993 if my memory is correct), I has participated in a project, that develop a ERP package that includes General Ledges, Account Payable, Account Receivable, Warehouse Inventory modules. The technology used that time was Novell B*trieve as database engine, C++ and for business logic, Borland Turbo Vision for C++ for GUI. At that time, Tran Duc Trung was leader, based on university compiler's knowledge, he developed a kind of Domain Specific Language, that allow end-users or implementors define posting rules using that DSL. The "posting rules" DSL is comprehensive featuring expression, AND, OR boolean condition, with Account, Money concept. Other team member, Martin Smid went so far and defined other DSL for reporting function. we called it FRT. Until now, I have not seen better reporting tool with such flexibility, elegance, and easy to used. Other other side, we also faced a lot problems. One of them is that MS*DOS can not run program with more than 1MB static data segment. C++ is also not good choice for GUI development, we encountered a lot of errors related to allocation/free memory, using pointer. And one of most ironical is in using of the DSL itself. The software developers didn't understand well accounting to be honest we was not willing to learn it on the other hand implementors and customers was not matured enough in using the DSL in effective manner. Despite these facts, Notia was able to sold the software and implement it in hundreds customers including few big names as ABB.

Thursday, May 10, 2007

ruby module_function

Ruby 'module' allows us to define methods then turn these methods into instance methods of a class by using key word 'include'. In ruby world, peoples call this feature 'mixin'. e.g.

module Foo
   def hello
      puts 'hello'
   end
end
class Bar
include Foo
end
Bar.new.hello #=> hello

Sometime we need to call method defined in a module without including the module into a class. We can do it by

module Foo
  def self.world
     puts 'world'
  end
end
Foo.world #=> world

There is a question how do we turn method 'hello' so it can be used in both mixin style and standalone . The answer is using kernel method 'module_function'

module Foo
   module_function :hello
end
Foo.hello + ' ' + Foo.world # => hello world