The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Hirb - And Tables for All

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Gabriel Horner

Posts: 62
Nickname: cldwaker
Registered: Feb, 2009

Gabriel Horner is an independent consultant who can't get enough of Ruby
Hirb - And Tables for All Posted: Mar 16, 2010 11:32 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Gabriel Horner.
Original Post: Hirb - And Tables for All
Feed Title: Tagaholic
Feed URL: http://feeds2.feedburner.com/tagaholic
Feed Description: My ruby/rails/knowledge management thoughts
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Gabriel Horner
Latest Posts From Tagaholic

Advertisement

Almost a year ago, hirb started as an itch to get mysql-like tables for Rails’ ActiveRecord models. Now, hirb 0.3.0 provides table views for ten additional database gems. Whether you use hirb with couch, mongo, riak or any of the databases supported by sequel or datamapper, hirb essentially turns irb into a database-agnostic database shell.

Database Gems

Here are the list of additional database gems and their modules/classes that now have table views:

Examples

Here’s all you need in your ~/.irbrc for hirb to work with the above gems and the following examples:

  require 'rubygems'
  require 'hirb'
  Hirb.enable

Keep in mind that while that the following examples are simple, your models/tables can be as complex as you’d like and hirb will still render your tables cleanly.

Sequel

For this example, you need to have sqlite3 installed.

  $ sudo gem install sqlite3-ruby sequel
  $ irb -rubygems -rsequel

  # Setup db and a model
  >> DB = Sequel.sqlite
  => #< Sequel::SQLite::Database: "sqlite:/">
  >> DB.create_table(:urls) { primary_key :id; String :name }
  => []
  >> class Url < Sequel::Model; end
  => nil

  # Nicely formatted tables!
  >> Url.create :name=>'example.com'
  +----+-------------+
  | id | name        |
  +----+-------------+
  | 1  | example.com |
  +----+-------------+
  1 row in set
  # Same as above
  >> Url.all

DataMapper

For this example, you need to have sqlite3 installed.

  $ sudo gem install sqlite3-ruby dm-core do_sqlite3
  $ irb -rubygems -rdm-core

  # Setup db and a model
  >> DataMapper.setup(:default, 'sqlite3::memory:')
  => #< DataMapper::Adapters::Sqlite3Adapter:0x188ceac ... >
  >> class Url
  >>  include DataMapper::Resource
  >>  property :id, Serial
  >>  property :name, String
  >> end
  => #< DataMapper::Property @model=Url @name=:name>
  >> Url.auto_migrate!
  => true

  # Nicely formatted tables!
  >> Url.create :name=>'example.com'
  +----+-------------+
  | id | name        |
  +----+-------------+
  | 1  | example.com |
  +----+-------------+
  1 row in set
  # Same as above
  >> Url.all

MongoMapper

For this example you need to have MongoDB installed.

  $ sudo gem install mongo_mapper
  $ irb -rubygems -rmongo_mapper

  # Setup db and a model
  >> MongoMapper.connection = Mongo::Connection.new
  => #< Mongo::Connection:0x1123d28 ... >
  >> MongoMapper.database = 'test'
  => 'test'
  >> class Url
  >>   include MongoMapper::Document
  >>   key :name, String
  >> end
  => #< MongoMapper::Plugins::Keys::Key:0x5a3520 ... >

  # Nicely formatted tables!
  >> Url.create :name=>'example.com'
  +-------------+--------------------------+
  | name        | _id                      |
  +-------------+--------------------------+
  | example.com | 4b97cf2978c2ec2854000001 |
  +-------------+--------------------------+
  1 row in set
  # Same as above
  >> Url.all

CouchRest

For this example you need to have CouchDB installed.

  $ sudo gem install couchrest
  $ irb -rubygems -rcouchrest

  # Setup db and a model
  >> class Url < CouchRest::ExtendedDocument
  >>   property :name
  >>   view_by :name
  >> end
  => false
  >> Url.database = CouchRest.database("http://127.0.0.1:5984/urls")
  => #< CouchRest::Database:0x170541c ... >

  # Nicely formatted tables!
  >> Url.create :name=>'example.com'
  +----------------------------------+-------------+
  | _id                              | name        |
  +----------------------------------+-------------+
  | af990a562a9a96703dd6ef0442a73db8 | example.com |
  +----------------------------------+-------------+
  1 row in set
  # Same as above
  >> Url.all  

Dynamic Views

Creating Them

All default views for the new database gems are due to hirb’s dynamic views. So what if you’re using a database gem that isn’t supported? Let’s answer this using friendly as an example (even though hirb already supports it):

  1. Identify the module or class that the database gem uses to define models. For friendly, this module is Friendly::Document.
  2. Determine what table options to pass to tables and how to generate them from the database gem’s model/table object. Although you can define as many options as you want, you must define the :fields option. To generate fields for a given friendly model object: model.attributes.keys
  3. Using the last two steps, define a dynamic view using Hirb.add_dynamic_view that returns a hash of table options:
  Hirb.add_dynamic_view("Friendly::Document", :helper=>:auto_table) {|obj|
    {:fields=>obj.class.attributes.keys}
  }

Read the docs for more about dynamic views.

Submitting Them

If you’ve created a dynamic view for a somewhat popular database gem, feel free to fork and add it to hirb’s default dynamic views. Since hirb’s default dynamic views are in module form, read the docs to understand how to write them. Although I do appreciate any forking, I’m looking mainly to add default views for commonly used database gems.

Read: Hirb - And Tables for All

Topic: MongoDB not ready? Previous Topic   Next Topic Topic: Support

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use