Module: CouchDocs

Defined in:
lib/couch_docs.rb,
lib/couch_docs/store.rb,
lib/couch_docs/version.rb,
lib/couch_docs/command_line.rb,
lib/couch_docs/design_directory.rb,
lib/couch_docs/document_directory.rb

Defined Under Namespace

Classes: CommandLine, DesignDirectory, DocumentDirectory, Store

Constant Summary collapse

LIBPATH =

:stopdoc:

::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
PATH =
::File.dirname(LIBPATH) + ::File::SEPARATOR
VERSION =
"1.3.2"

Class Method Summary collapse

Class Method Details

.destructive_database_create(db_uri) ⇒ Object

Create or recreate the database located at db_uri



79
80
81
# File 'lib/couch_docs.rb', line 79

def self.destructive_database_create(db_uri)
  Store.put!(db_uri, "")
end

.dump(db_uri, dir, only = nil) ⇒ Object

Dump all documents located at db_uri into the directory dir



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/couch_docs.rb', line 62

def self.dump(db_uri, dir, only=nil)
  null_dir = OpenStruct.new(:store_document => nil)

  doc_dir = (only == :design) ?
    null_dir : DocumentDirectory.new(dir)
  design_dir = (only == :doc) ?
    null_dir : DesignDirectory.new(dir)

  store = Store.new(db_uri, :only => only)
  store.map.each do |doc|
    doc.delete('_rev')
    (doc['_id'] =~ /^_design/ ? design_dir : doc_dir).
      store_document(doc)
  end
end

.libpath(*args) ⇒ Object

Returns the library path for the module. If any arguments are given, they will be joined to the end of the libray path using File.join.



87
88
89
# File 'lib/couch_docs.rb', line 87

def self.libpath( *args )
  args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
end

.path(*args) ⇒ Object

Returns the lpath for the module. If any arguments are given, they will be joined to the end of the path using File.join.



95
96
97
# File 'lib/couch_docs.rb', line 95

def self.path( *args )
  args.empty? ? PATH : ::File.join(PATH, args.flatten)
end

.put_design_dir(db_uri, dir) ⇒ Object

Upload design documents from dir to the CouchDB database located at db_uri



33
34
35
36
37
# File 'lib/couch_docs.rb', line 33

def self.put_design_dir(db_uri, dir)
  store = Store.new(db_uri)
  dir = DesignDirectory.new(dir)
  store.put_design_documents(dir.to_hash)
end

.put_dir(db_uri, dir) ⇒ Object

For a CouchDB database described by db_uri and a directory, dir containing design documents, creates design documents in the CouchDB database



20
21
22
23
# File 'lib/couch_docs.rb', line 20

def self.put_dir(db_uri, dir)
  self.put_design_dir(db_uri, "#{dir}/_design")
  self.put_document_dir(db_uri, dir)
end

.put_document_dir(db_uri, dir) ⇒ Object

Upload documents from dir to the CouchDB database located at db_uri



42
43
44
45
46
47
# File 'lib/couch_docs.rb', line 42

def self.put_document_dir(db_uri, dir)
  dir = DocumentDirectory.new(dir)
  dir.each_document do |name, contents|
    Store.put!("#{db_uri}/#{name}", contents)
  end
end

.put_file(db_uri, file_path) ⇒ Object

Upload a document located at file_path to the CouchDB database located at db_uri



53
54
55
56
57
# File 'lib/couch_docs.rb', line 53

def self.put_file(db_uri, file_path)
  contents = JSON.parse(File.read(file_path))
  name = File.basename(file_path, ".json")
  Store.put!("#{db_uri}/#{name}", contents)
end

.require_all_libs_relative_to(fname, dir = nil) ⇒ Object

Utility method used to require all files ending in .rb that lie in the directory below this file that has the same name as the filename passed in. Optionally, a specific directory name can be passed in such that the filename does not have to be equivalent to the directory.



104
105
106
107
108
109
110
# File 'lib/couch_docs.rb', line 104

def self.require_all_libs_relative_to( fname, dir = nil )
  dir ||= ::File.basename(fname, '.*')
  search_me = ::File.expand_path(
      ::File.join(::File.dirname(fname), dir, '**', '*.rb'))

  Dir.glob(search_me).sort.each {|rb| require rb}
end

.upload_dir(db_uri, dir) ⇒ Object

Alias for put_dir



26
27
28
# File 'lib/couch_docs.rb', line 26

def self.upload_dir(db_uri, dir)
  self.put_dir(db_uri, dir)
end

.versionObject

Returns the version string for the library.



12
13
14
# File 'lib/couch_docs.rb', line 12

def self.version
  VERSION
end