Class: CouchConsole
- Inherits:
-
Object
- Object
- CouchConsole
- Defined in:
- lib/commands/uri.rb,
lib/couchconsole.rb,
lib/commands/show.rb,
lib/commands/delete.rb,
lib/commands/update.rb,
lib/commands/new_doc.rb,
lib/commands/all_documents.rb
Class Method Summary collapse
Instance Method Summary collapse
- #allDocuments ⇒ Object
- #delete(id, field) ⇒ Object
- #execute(cmd) ⇒ Object
- #getDatabase ⇒ Object
- #go ⇒ Object
- #help ⇒ Object
- #init ⇒ Object
-
#initialize ⇒ CouchConsole
constructor
A new instance of CouchConsole.
- #new_doc ⇒ Object
- #show(id) ⇒ Object
- #showDesign(doc) ⇒ Object
- #showDocument(doc) ⇒ Object
- #update(id, field, value) ⇒ Object
- #uri(id) ⇒ Object
Constructor Details
#initialize ⇒ CouchConsole
Returns a new instance of CouchConsole.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/couchconsole.rb', line 5 def initialize @commands = [ { :regexp => /^\s*help\s*$/, :method => :help, :documentation => [["help", "Display this help"]] }, { :regexp => /^\s*quit\s*$/, :method => nil, :documentation => [["quit", "Quit"]] } ] Dir.glob( File.join(File.dirname(File.(__FILE__)), "commands", "*.rb") ).each do |file| require file init end end |
Class Method Details
.go ⇒ Object
75 76 77 |
# File 'lib/couchconsole.rb', line 75 def self.go CouchConsole.new.go end |
Instance Method Details
#allDocuments ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/commands/all_documents.rb', line 14 def allDocuments all = @db.documents puts "#{all["total_rows"]} documents :" all["rows"].each do |doc| puts " id : #{doc["id"]} - rev : #{doc["value"]["rev"]}" end end |
#delete(id, field) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/commands/delete.rb', line 11 def delete( id, field ) document = @db.get( id ) if field.size > 0 document.delete(field) document.save puts "*** Field `#{field}' deleted in document `#{id}'" else if document.class == CouchRest::Document document.destroy puts "*** Document `#{id}' deleted" else puts "!!! Can't delete document `#{id}'" end end rescue RestClient::ResourceNotFound puts "!!! Document `#{id}' does not exist." end |
#execute(cmd) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/couchconsole.rb', line 37 def execute( cmd ) executed = 0 @commands.each do |command| if match = command[:regexp].match(cmd) executed += 1 params = match.captures if params.size > 0 self.send command[:method], *params else self.send command[:method] end end end if executed == 0 puts "!!! Command unknown. Try help" end end |
#getDatabase ⇒ Object
24 25 26 27 |
# File 'lib/couchconsole.rb', line 24 def getDatabase print "Database URI : "; $stdin.readline.chomp end |
#go ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/couchconsole.rb', line 55 def go base = ARGV[0] || getDatabase @db = CouchRest.database!( base ) begin @db.info rescue Errno::ECONNREFUSED puts "!!! Can acces database #{@db}" return end prompt = ">> " while( true ) cmd = Readline.readline(prompt, true) break if cmd.nil? or cmd.chomp == "quit" execute( cmd ) end puts "\nBye! Bye !\n" end |
#help ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/couchconsole.rb', line 29 def help @commands.each do |command| command[:documentation].each do |cmd| printf "%-30s : %s\n", cmd[0], cmd[1] end end end |
#init ⇒ Object
2 3 4 5 6 7 8 9 |
# File 'lib/commands/uri.rb', line 2 def init puts "** initialize uri" @commands << { :regexp => /^\s*uri\s*(.*)\s*$/, :method => :uri, :documentation => [["uri id", "Returns the CouchDB uri for the document"]] } end |
#new_doc ⇒ Object
11 12 13 14 15 16 |
# File 'lib/commands/new_doc.rb', line 11 def new_doc document = CouchRest::Document.new document.database = @db document.save puts "** Document `#{document.id}' created!" end |
#show(id) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/commands/show.rb', line 23 def show( id ) document = @db.get( id ) if document.class == CouchRest::Document showDocument( document ) else showDesign( document ) end rescue RestClient::ResourceNotFound puts "!!! Document `#{id}' does not exist." end |
#showDesign(doc) ⇒ Object
11 12 13 |
# File 'lib/commands/show.rb', line 11 def showDesign( doc ) puts "!!! Show design is not yet implemented" end |
#showDocument(doc) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/commands/show.rb', line 15 def showDocument( doc ) size = 0 doc.keys.each { |k| size = k.size if k.size > size} doc.each do |k, v| printf "%#{size}s : %s\n", k, v end end |
#update(id, field, value) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/commands/update.rb', line 11 def update( id, field, value ) document = @db.get( id ) if document.class == CouchRest::Document document[field] = value begin document.save puts "*** Update `#{id}' : #{field} = #{value}" rescue => e puts "!!! Update `#{id}' faild with message #{e.}" end else puts "!!! Can't update `#{id}'" end rescue RestClient::ResourceNotFound puts "!!! Document `#{id}' does not exist." end |
#uri(id) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/commands/uri.rb', line 11 def uri( id ) document = @db.get( id ) puts document.uri rescue RestClient::ResourceNotFound puts "!!! Document `#{id}' does not exist." end |