Class: CouchObject::View

Inherits:
Object
  • Object
show all
Defined in:
lib/couch_object/view.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, name) ⇒ View

Takes:

  • db as CouchObject::Database object or a string containing the database uri and database name (localhost:5984/mydb)

  • name (string). The name of the view

Returns: new CouchObject::View instance

Raises:

  • an exeption if it can’t figure out the database



39
40
41
42
# File 'lib/couch_object/view.rb', line 39

def initialize(db, name)
  @db = self.class.get_db(db)
  @name = name
end

Instance Attribute Details

#dbObject

Returns the value of attribute db.



43
44
45
# File 'lib/couch_object/view.rb', line 43

def db
  @db
end

Class Method Details

.create(db, name, query) ⇒ Object

Takes:

  • db as CouchObject::Database object or a string containing the database uri and database name (localhost:5984/mydb)

  • name (string). The name of the view

  • query (JSON string). The JSON document that should be added to the database

Returns: CouchObject::Reponse object instance

Raises:

  • an exeption if it can’t figure out the database



21
22
23
24
25
# File 'lib/couch_object/view.rb', line 21

def self.create(db, name, query)
  _db = self.get_db(db)
  # %2F => /
  _db.put("_design%2F#{name}", query)
end

Instance Method Details

#deleteObject



127
128
129
# File 'lib/couch_object/view.rb', line 127

def delete
  @db.delete("/#{db.name}/#{name}")
end

#nameObject



45
46
47
# File 'lib/couch_object/view.rb', line 45

def name
  "_view/#{@name.dup}"
end

#query(params = {}) ⇒ Object

Returns an array of the rows returned by the view

Takes:

  • params

    (hash): a hash of URL query arguments supported

    by couchDB. If omitted it defaults to not use a key and to update the view.

    Example:

    view.query({:update => false, :key => "bar"}) => Array
    

Returns:

  • a array of rows from the view

Raises:

  • CouchObject::Errors::MissingView if the view doesn’t exist



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/couch_object/view.rb', line 67

def query(params = {})

  response = raw_query(params)
  
  rows_to_return = []
  response["rows"].each do |params_for_object|
      rows_to_return << params_for_object["value"]
  end
  
  rows_to_return
  
end

#raw_query(params = {}) ⇒ Object

Returns the response data from CouchDB for a view query without

Takes:

  • params

    (hash): a hash of URL query arguments supported

    by couchDB. If omitted it defaults to not use a key and to update the view.

    Example:

    view.raw_query({:update => false, :key => "bar"}) => data
    

Returns:

  • the response data for the view

Raises:

  • CouchObject::Errors::MissingView if the view doesn’t exist



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/couch_object/view.rb', line 98

def raw_query(params = {})

  #Create a querystring with the parameters passed inn
  querystring = "?"
  params.each_pair do |key, value|
    querystring += \
      "#{key}=#{Utils.encode_querystring_parameter(value)}&"
  end
  querystring = querystring[0...-1]

  view_with_parameters = name + querystring

  response = JSON.parse(db.get(view_with_parameters).body)

  raise CouchObject::Errors::MissingView, \
    "The view '#{name}' doesn't exist on the server" \
    if response["error"] == "not_found"

  raise CouchObject::Errors::MapProcessError \
    if response["error"] == "map_process_error"
      
  raise CouchObject::Errors::CouchDBError, \
    "CouchDB returned and error and described the problem as #{response['reason']}" \
    if response["error"]
  
  return response
  
end