standalone snippets

How to use RSpec and ZenTest in a standalone Ruby project

Tagged rspec, autotest, zentest, standalone, ruby  Languages ruby

First install the RSpec and ZenTest gem:

$ sudo gem install rspec zentest

Next create the spec folder:

$ cd project_folder
$ mkdir spec

Save the following to spec/helper.rb:

$LOAD_PATH.unshift File.dirname(__FILE__) + '/..'

require 'rubygems'
require 'spec'
#require 'spec/rake/spectask' not needed, because ZenTest supports rspec now

Now create spec/transcoder_spec.rb, and add the following test to it:

require File.dirname(__FILE__) + '/helper'
require File.dirname(__FILE__) + '/../transcoder.rb'

context "Transcoder" do
  setup do
    # Setup your stuff here
  end
  
  it "should support 3gp format" do
    Transcoder.convert("me_and_you.3gp").should == true
  end
end

Let's not forget the class we're testing, put this code in lib/transcoder.rb:

class Transcoder
  def initialize
  end
  
  def self.convert(file)
    return true
  end
end

Note that autotest automatically looks for your code in the lib folder.

Now run the test with the zentest command:

autotest

Change your files and autotest will run the test again.

Tip: Read Getting started with Autotest - Continuous Testing and Setting up autotest to use Growl on OSX for more information on how to increase your productivity.

How to use ActiveRecord without Rails

Tagged activerecord, standalone, rails, ruby, migration  Languages ruby

This is an example of how to use ActiveRecord without Rails. Tested with Rails 6.

Short version

require 'active_record'
require 'pg' # postgresql

ActiveRecord::Base.establish_connection(
  adapter: "postgresql",
  encoding: "unicode",
  database: "matrix",
  username: "postgres",
  password: ""
)

class User < ActiveRecord::Base
end

Long version

app.rb:

APP_ENV = ENV.fetch('APP_ENV').to_sym
puts "Starting app in #{APP_ENV} mode..."

require 'active_record'
require 'sqlite3'
require 'logger'

ActiveRecord::Base.logger = Logger.new('debug.log')
DB_CONF = YAML.load(Erubis::Eruby.new(IO.read('config/database.yml')).result)

ActiveRecord::Base.schema_format = :sql
ActiveRecord::Base.logger = LOG
ActiveRecord::Base.configurations = DB_CONF
ActiveRecord::Base.establish_connection(DB_CONF.fetch(APP_ENV.to_s))


class User < ActiveRecord::Base
end

db/migrate/001_schema.rb

class Schema < ActiveRecord::Migration
  def change
    create_table :users, force: true do |t|
      t.string :name
    end
  end
end

Rakefile:

task :environment do
  RAKE_PATH = File.expand_path('.')
  RAKE_ENV  = ENV.fetch('APP_ENV', 'development')
  ENV['RAILS_ENV'] = RAKE_ENV

  Bundler.require :default, RAKE_ENV

  ActiveRecord::Tasks::DatabaseTasks.database_configuration = ActiveRecord::Base.configurations
  ActiveRecord::Tasks::DatabaseTasks.root             = RAKE_PATH
  ActiveRecord::Tasks::DatabaseTasks.env              = RAKE_ENV
  ActiveRecord::Tasks::DatabaseTasks.db_dir           = 'db'
  ActiveRecord::Tasks::DatabaseTasks.migrations_paths = ['db/migrate']
  ActiveRecord::Tasks::DatabaseTasks.seed_loader      = OpenStruct.new(load_seed: nil)
end

# Use Rails 6 migrations
load 'active_record/railties/databases.rake'

database.yml:

development:
  adapter: sqlite3
  database: db/data.sqlite3
  pool: 5
  timeout: 5000

References: https://api.rubyonrails.org/classes/ActiveRecord/Tasks/DatabaseTasks.html