couchup 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,16 +1,18 @@
1
- module Commands
2
- class Connect
3
- def run(host = 'localhost', port = 5984)
4
- Couchup.host = host
5
- Couchup.port = port
6
- if(Couchup.ready?)
7
- puts "Connected to #{Couchup.host}:#{Couchup.port}"
8
- else
9
- puts "Could not connect to #{Couchup.host}:#{Couchup.port}"
1
+ module Couchup
2
+ module Commands
3
+ class Connect
4
+ def run(host = 'localhost', port = 5984)
5
+ Couchup.host = host
6
+ Couchup.port = port
7
+ if(Couchup.ready?)
8
+ puts "Connected to #{Couchup.host}:#{Couchup.port}"
9
+ else
10
+ puts "Could not connect to #{Couchup.host}:#{Couchup.port}"
11
+ end
12
+ end
13
+ def self.describe
14
+ "Connects to the couch server. Takes host and port defaults to 5984"
10
15
  end
11
- end
12
- def self.describe
13
- "Connects to the couch server. Takes host and port defaults to 5984"
14
16
  end
15
17
  end
16
18
  end
@@ -0,0 +1,13 @@
1
+ module Couchup
2
+ module Commands
3
+ class Create
4
+ def run(*params)
5
+ Couchup.server.database!(params.first)
6
+ Use.new.run(params.first)
7
+ end
8
+ def self.describe
9
+ "Creates a new database and switches to the database"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,11 +1,13 @@
1
- module Commands
2
- class Get
3
- def run(id = nil)
4
- id.nil? ? Couchup.all : Couchup.get(id)
5
- end
6
-
7
- def self.describe
8
- "get [id] retuns the document with the id"
1
+ module Couchup
2
+ module Commands
3
+ class Get
4
+ def run(id = nil)
5
+ id.nil? ? Couchup.all : Couchup.get(id)
6
+ end
7
+
8
+ def self.describe
9
+ "get [id] retuns the document with the id"
10
+ end
9
11
  end
10
12
  end
11
13
  end
@@ -1,15 +1,18 @@
1
- module Commands
2
- class Help
3
- def run(option={})
4
- Commands.constants.each do |stuff|
5
- k = Commands.const_get(stuff)
6
- print stuff.downcase; print "\t\t"
7
- puts k.respond_to?(:describe) ? k.describe : "No Help"
1
+ module Couchup
2
+ module Commands
3
+ class Help
4
+ def run(param = nil)
5
+ commands = param.nil? ? Commands.constants : [param.camelize]
6
+ commands.each do |stuff|
7
+ k = Commands.const_get(stuff)
8
+ print stuff.downcase; print "\t\t"
9
+ puts k.respond_to?(:describe) ? k.describe : "No Help"
10
+ end
8
11
  end
9
- end
10
12
 
11
- def self.describe
12
- "Help on the system, 'help put' will provider help about put."
13
+ def self.describe
14
+ "Help on the system, 'help :get' will provider help about using get."
15
+ end
13
16
  end
14
17
  end
15
18
  end
@@ -0,0 +1,13 @@
1
+ module Couchup
2
+ module Commands
3
+ class Map
4
+ def run(*params)
5
+ MapReduce.map(*params)
6
+ end
7
+
8
+ def self.describe(p = nil)
9
+ "Runs a view with just the map function and retuns the results"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Couchup
2
+ module Commands
3
+ class Restart
4
+ def run(*params)
5
+ Couchup.server.restart!
6
+ end
7
+
8
+ def self.describe
9
+ "Restarts the couchdb server. Please be careful with it. It will not prompt you for anything."
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,11 +1,18 @@
1
- module Commands
2
- class Show
3
-
4
- def run(param = "databases")
5
- puts Couchup.databases.inspect
6
- end
7
- def self.describe
8
- "show databases"
1
+ module Couchup
2
+ module Commands
3
+ class Show
4
+ def run(param)
5
+ param = param.to_s
6
+ if(param.blank? || param == 'databases' )
7
+ puts Couchup.databases.inspect
8
+ else
9
+ puts Couchup.views.inspect
10
+ end
11
+ end
12
+
13
+ def self.describe
14
+ "show databases if databases is specified or views if vies is specified. defaults to databases."
15
+ end
9
16
  end
10
17
  end
11
18
  end
@@ -1,16 +1,20 @@
1
- module Commands
2
- class Use
3
- def run(database)
1
+ module Couchup
2
+ module Commands
3
+ class Use
4
+ def run(database)
4
5
 
5
- if Couchup.databases.include? database
6
- Couchup.database = database
7
- else
8
- puts "Database was not found"
6
+ if Couchup.databases.include? database
7
+ Couchup.database = database
8
+ puts Couchup.database.info.inspect
9
+ else
10
+ puts "Database was not found"
11
+ end
9
12
  end
10
- end
11
13
 
12
- def self.describe
13
- "Use provided database"
14
+ def self.describe
15
+ "Use provided database"
16
+ end
14
17
  end
15
18
  end
19
+
16
20
  end
@@ -0,0 +1,13 @@
1
+ module Couchup
2
+ module Commands
3
+ class View
4
+ def run(*params)
5
+ MapReduce.reduce(*params)
6
+ end
7
+
8
+ def self.describe(params = nil)
9
+ "Executes the given View. Runs Map and Reduce and retuns the results"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,40 +1,50 @@
1
- class Couchup
2
- class << self
3
- attr_accessor :port, :host
4
- def server
5
- @server ||= CouchRest::Server.new("http://#{host}:#{port}")
6
- end
7
-
8
- def database=(database)
9
- @db = CouchRest.database!("http://#{host}:#{port}/#{database}")
10
- end
1
+ module Couchup
2
+ class Couchup
3
+ class << self
4
+ attr_accessor :port, :host
5
+ def server
6
+ @server ||= CouchRest::Server.new("http://#{host}:#{port}")
7
+ end
11
8
 
12
- def database
13
- @db.name
14
- end
9
+ def database=(database)
10
+ @db = CouchRest.database!("http://#{host}:#{port}/#{database}")
11
+ end
15
12
 
16
- def ready?
17
- uuid = nil
18
- begin
19
- uuid = server.next_uuid
20
- rescue
21
- puts $!.backtrace
22
- puts $!.inspect
23
- end
24
- !uuid.nil?
25
- end
13
+ def database
14
+ @db
15
+ end
16
+
17
+ def views(design = nil)
18
+ params = design.nil? ? {:startkey => '_design', :endkey => '_design0'} : {:key => "_design\\#{design}"}
19
+ designs = database.documents(params.merge(:include_docs => true))["rows"]
20
+ designs.collect do |d|
21
+ d["doc"]["views"].keys
22
+ end.flatten
23
+ end
24
+
25
+ def ready?
26
+ uuid = nil
27
+ begin
28
+ puts (info = server.info)
29
+ rescue
30
+ puts $!.backtrace
31
+ puts $!.inspect
32
+ end
33
+ !info.nil?
34
+ end
26
35
 
27
- def get(id)
28
- @db.get(id)
29
- end
36
+ def get(id)
37
+ @db.get(id)
38
+ end
30
39
 
31
- def all(options={})
32
- @db.documents
33
- end
40
+ def all(options={})
41
+ @db.documents
42
+ end
34
43
 
35
- def databases
36
- server.databases
37
- end
44
+ def databases
45
+ server.databases
46
+ end
38
47
 
48
+ end
39
49
  end
40
50
  end
@@ -0,0 +1,5 @@
1
+ class Array
2
+ def second
3
+ self[1] if size >1
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class String
2
+ def blank?
3
+ nil || empty?
4
+ end
5
+ end
@@ -3,6 +3,9 @@ class Symbol
3
3
  Object.const_get(to_s)
4
4
  end
5
5
 
6
+ def camelize
7
+ to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
8
+ end
6
9
  # cargo culted from rails inflector.
7
10
  def underscore
8
11
  word = to_s.dup
@@ -0,0 +1,27 @@
1
+ module Couchup
2
+ class MapReduce
3
+ def self.map(*params)
4
+ view({}, *params)
5
+ end
6
+
7
+ def self.reduce(*params)
8
+ view({:reduce => true}, *params)
9
+ end
10
+
11
+ private
12
+ def self.view(options, *params)
13
+ name = params.shift
14
+ view_params = {:include_docs => true}.merge(options)
15
+ if params.size == 1
16
+ val = params.first
17
+ view_params.merge!(:keys => val) if val.is_a? Array
18
+ view_params.merge!(val) if val.is_a? Hash
19
+ view_params.merge!(:key => val) unless val.nil?
20
+ end
21
+ response = Couchup.database.view(name, view_params)
22
+ rows = response["rows"]
23
+ puts "Found #{rows.size} items"
24
+ rows.each{|r| puts r.inspect}
25
+ end
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module Couchup
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/couchup.rb CHANGED
@@ -1,13 +1,18 @@
1
1
  require File.expand_path '../couchup/couchup', __FILE__
2
2
  Dir[File.expand_path('../couchup/commands/*.rb',__FILE__)].each { |file| require file}
3
3
  Dir[File.expand_path('../couchup/extensions/*.rb',__FILE__)].each { |file| require file}
4
+ Dir[File.expand_path('../couchup/*.rb',__FILE__)].each { |file| require file}
4
5
 
5
6
 
6
- Commands.constants.each do |c|
7
+ Couchup::Commands.constants.each do |c|
7
8
  instance_eval "
8
9
  def #{c.underscore}(*args)
9
- instance = Commands.const_get(:#{c}).new
10
- instance.run(*args)
10
+ begin
11
+ instance = Couchup::Commands.const_get(:#{c}).new
12
+ instance.run(*args)
13
+ rescue
14
+ puts $!.inspect
15
+ end
11
16
  end"
12
17
  end
13
18
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: couchup
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.3
5
+ version: 0.0.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - V Sreekanth
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-02-22 00:00:00 +05:30
13
+ date: 2011-02-24 00:00:00 +05:30
14
14
  default_executable: ey
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -121,12 +121,19 @@ files:
121
121
  - couchup.gemspec
122
122
  - lib/couchup.rb
123
123
  - lib/couchup/commands/connect.rb
124
+ - lib/couchup/commands/create.rb
124
125
  - lib/couchup/commands/get.rb
125
126
  - lib/couchup/commands/help.rb
127
+ - lib/couchup/commands/map.rb
128
+ - lib/couchup/commands/restart.rb
126
129
  - lib/couchup/commands/show.rb
127
130
  - lib/couchup/commands/use.rb
131
+ - lib/couchup/commands/view.rb
128
132
  - lib/couchup/couchup.rb
133
+ - lib/couchup/extensions/array.rb
134
+ - lib/couchup/extensions/string.rb
129
135
  - lib/couchup/extensions/symbol.rb
136
+ - lib/couchup/mapreduce.rb
130
137
  - lib/couchup/version.rb
131
138
  has_rdoc: true
132
139
  homepage: ""