The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
RailsFS After a Couple Minutes of Tooling With Fuse, Whoa

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
Red Handed

Posts: 1158
Nickname: redhanded
Registered: Dec, 2004

Red Handed is a Ruby-focused group blog.
RailsFS After a Couple Minutes of Tooling With Fuse, Whoa Posted: Sep 21, 2005 2:59 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Red Handed.
Original Post: RailsFS After a Couple Minutes of Tooling With Fuse, Whoa
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded

Advertisement

Hot cats, I’m already in love with FuseFS. And it was only dropped on ruby-talk a bushel of hours ago. See, here’s a script which mounts the ActiveRecord classes in your Rails app as a Linux filesystem.

 #!/usr/bin/env ruby 

 require 'fusefs'
 require File.dirname(__FILE__) + '/../config/environment'

 class RailsFS < FuseFS::FuseDir
   def initialize
     @classes = {}
     require 'find'
     Find.find( File.join(RAILS_ROOT, 'app/models') ) do |model|
       if /(\w+)\.rb$/ =~ model
         model = $1
         kls = Inflector.classify( model )
         ( @classes[model] = Kernel::const_get( kls ) ).
           find :first rescue @classes.delete( model )
       end
     end
   end
   def directory? path
     tname, key = scan_path path
     table = @classes[tname]
     if table.nil?; false  # /table
     elsif key;     false  # /table/id
     else; true end
   end
   def file? path
     tname, key = scan_path path
     table = @classes[tname]
     key and table and table.find( key )
   end
   def can_delete?; true end
   def can_write? path; file? path end
   def contents path
     tname, key = scan_path path
     table = @classes[tname]
     if tname.nil?; @classes.keys.sort  # /
     else; table.find( :all ).map { |o| o.id.to_s } end  # /table
   end         
   def write_to path, body
     obj = YAML::load( body )
     obj.save
   end
   def read_file path
     tname, key = scan_path path
     table = @classes[tname]
     YAML::dump( table.find( key ) )
   end 
 end

 if (File.basename($0) == File.basename(__FILE__))
   root = RailsFS.new
   FuseFS.set_root(root)
   FuseFS.mount_under(ARGV[0])
   FuseFS.run # This doesn't return until we're unmounted.
 end

Make sure you have Linux 2.4-2.6, FUSE and Ruby-FuseFS.

Save the above script as script/filesys in your Rails app. (If you’d rather not cut-and-paste the above, it’s here.)

Now, run mkdir ~/railsmnt. Then, script/filesys ~/railsmnt. The rules are as follows:

  • ls ~/railsmnt will give you a list of tables.
  • ls ~/railsmnt/table will list IDs from the table.
  • cat ~/railsmnt/table/id will display a record in YAML.
  • vim ~/railsmnt/table/id to edit the record in YAML!

See, we got your ObjFSDB right here. Now I dare you to set up an FTP site to let people upload YAML files right into your database. (Thanks to Greg Milliam for his brilliant work, more userspace filesystem ideas here.)

Read: RailsFS After a Couple Minutes of Tooling With Fuse, Whoa

Topic: Today's One Liner Previous Topic   Next Topic Topic: Kindness is the currency of open source support

Sponsored Links



Google
  Web Artima.com   

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