The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Console Update With Your Editor

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
Console Update With Your Editor Posted: Feb 28, 2009 5:16 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Gabriel Horner.
Original Post: Console Update With Your Editor
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

Rails’ script/console makes it easy to fetch, view and edit your database records. But can you edit those records as quickly as you edit code in your text editor? Riiight, like editing our database records in an editor is gonna happen? It already has.

While fleshing out my own bookmark manager, a console-based Rails app, I got tired of editing my database records through update_attribute and update_attributes. Yes, you can autocomplete and alias away method calls but it still didn’t feel fast enough for the massive amount of retagging and annotating I was doing. So I prayed to the flying spaghetti monster and the console_update plugin was born. (I actually didn’t ‘pray’ but let’s not delve into semantics.)

Before I go into a long spiel about how it works you probably just want examples:

  bash> script/console
  
  #Set your editor if you don't have your environment variable EDITOR set.
  ConsoleUpdate.editor = 'insert flame war here'
  
  # Invoke the editor on your first url object.
  irb>> Url.first.console_update
  
  # In your editor you get a stringified version of your ActiveRecord objects.
  ==============
  ---
  - name: http://funnyordie.com
    id: 1
    description: i swear i've seen this site somewhere before
  ==============
  # Edit with your crazy editor-fu, save and exit. Your record is updated.
  
  # Perhaps we only want to edit the name column
  irb>> Url.first.console_update :only=>%w{name}
  
  # Perhaps we want to edit everything but the name column
  irb>> Url.first.console_update :except=>%w{name}  

So at this point, we’ve edited a record. It’s nice for those records that have long text fields. But what about multiple records?

  
  irb>> records = Url.all :limit=>10
  # Edit multiple records just like you would with one record.
  irb>> Url.console_update records
  
  # console_update takes the same options as above
  irb>> Url.console_update records, :only=>%w{name}
  
  # This is nice but why not just chain it to the end of a named_scope?
  irb>> ConsoleUpdate.enable_named_scope
  
  # Using the named_scope :tagged_with, fetches all urls tagged with sweetness and drops them in an editor.
  irb>> Url.tagged_with('sweetness').console_update

You may have noticed in the first set of examples that an edited file looked like YAML. That’s because it is. By default, ConsoleUpdate uses YAML as a filter, but console_update is format-agnostic. Prefer to see your database records in a different way? Roll your own!.

So now for the fascinating explanation of how console_update works! Well, maybe not that fascinating. Your database records are converted to an array of hashes. This array is converted by your preferred filter into a string. You edit the string as a temporary file in your preferred editor. You save the file, the file is converted back by the filter into an array of hashes, and the modified records are updated.

Oh yeah, I forgot to mention that ConsoleUpdate doesn’t care what columns/attributes you edit. So if you have any accessor methods which update associated objects when saved, your associated objects get updated as well. It has worked nicely for editing associated tags of my bookmarks. Play with it on Github or just install with gem install cldwalker-console_update.

Read: Console Update With Your Editor

Topic: A Brief Guide To Amethyst Previous Topic   Next Topic Topic: RDoc 2.4.1

Sponsored Links



Google
  Web Artima.com   

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