Module: Exegesis::Database::InstanceMethods

Defined in:
lib/exegesis/database.rb

Instance Method Summary collapse

Instance Method Details

#get(id, opts = {}) ⇒ Object

GETs a document with the given id from the database



94
95
96
97
98
99
100
101
102
# File 'lib/exegesis/database.rb', line 94

def get id, opts={}
  if id.kind_of?(Array)
    collection = opts.delete(:collection) # nil or true for yes, false for no
    r = post '_all_docs?include_docs=true', {"keys"=>id}
    r['rows'].map {|d| Exegesis.instantiate d['doc'], self }
  else
    Exegesis.instantiate raw_get(id), self
  end
end

#initialize(server, database_name = nil) ⇒ Object

Create a Database adapter for the given server and database name. Will raise RestClient::ResourceNotFound if the database does not exist.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/exegesis/database.rb', line 58

def initialize server, database_name=nil
  if database_name.nil?
    if server.match(/\A(https?:\/\/[-0-9a-z\.]+(?::\d+))\/(#{Exegesis::Database::VALID_NAME_PATTERN})\Z/)
      @server = Exegesis::Server.new($1)
      database_name = $2
    elsif server.match(/\A#{Exegesis::Database::VALID_NAME_PATTERN}\Z/)
      @server = Exegesis::Server.new #localhost
      database_name = server
    else
      raise "Not a valid database url or name"
    end
  else
    @server = server
  end
  @uri = "#{@server.uri}/#{database_name}"
  @server.get @uri # raise RestClient::ResourceNotFound if the database does not exist
end

#post(url, body = {}, headers = {}) ⇒ Object

POSTs the body to the database



124
125
126
127
128
129
130
# File 'lib/exegesis/database.rb', line 124

def post url, body={}, headers={}
  if body.is_a?(Hash) && body.empty?
    body = url
    url = ''
  end
  Exegesis::Http.post "#{@uri}/#{url}", (body || '').to_json, headers
end

#put(id, body, headers = {}) ⇒ Object

PUTs the body to the given id in the database



119
120
121
# File 'lib/exegesis/database.rb', line 119

def put id, body, headers={}
  Exegesis::Http.put "#{@uri}/#{id}", (body || '').to_json, headers
end

#raw_get(id, options = {}) ⇒ Object

performs a raw GET request against the database



82
83
84
85
86
87
88
89
90
91
# File 'lib/exegesis/database.rb', line 82

def raw_get id, options={}
  keys = options.delete(:keys)
  id = Exegesis::Http.escape_id id
  url = Exegesis::Http.format_url "#{@uri}/#{id}", options
  if id.match(%r{^_design/.*/_view/.*$}) && keys
    Exegesis::Http.post url, {:keys => keys}.to_json
  else
    Exegesis::Http.get url
  end
end

#save(docs) ⇒ Object

saves a document or collection thereof



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/exegesis/database.rb', line 105

def save docs
  if docs.is_a?(Array)
    post "_bulk_docs", { 'docs' => docs }
  else
    result = docs['_id'].nil? ? post(docs) : put(docs['_id'], docs)
    if result['ok']
      docs['_id'] = result['id']
      docs['_rev'] = result['rev']
    end
    docs
  end
end

#to_sObject Also known as: inspect



76
77
78
# File 'lib/exegesis/database.rb', line 76

def to_s
  "#<#{self.class.name}(Exegesis::Database):#{self.object_id} uri=#{uri}>"
end