Class: RelaxDB::DesignDocument

Inherits:
Object
  • Object
show all
Defined in:
lib/relaxdb/design_doc.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(design_doc_name, data) ⇒ DesignDocument

Returns a new instance of DesignDocument.



7
8
9
10
# File 'lib/relaxdb/design_doc.rb', line 7

def initialize(design_doc_name, data)
  @design_doc_name = design_doc_name
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/relaxdb/design_doc.rb', line 5

def data
  @data
end

Class Method Details

.get(design_doc_name) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/relaxdb/design_doc.rb', line 39

def self.get(design_doc_name)
  begin
    database = RelaxDB.db
    resp = database.get("_design/#{design_doc_name}")
    DesignDocument.new(design_doc_name, JSON.parse(resp.body))
  rescue HTTP_404
    DesignDocument.new(design_doc_name, {"_id" => "_design/#{design_doc_name}"} )
  end
end

Instance Method Details

#add_map_view(view_name, function) ⇒ Object



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

def add_map_view(view_name, function)
  add_view(view_name, "map", function)
end

#add_reduce_view(view_name, function) ⇒ Object



16
17
18
# File 'lib/relaxdb/design_doc.rb', line 16

def add_reduce_view(view_name, function)
  add_view(view_name, "reduce", function)
end

#add_validation_func(function) ⇒ Object



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

def add_validation_func(function)
  @data["validate_doc_update"] = function
  self
end

#add_view(view_name, type, function) ⇒ Object



25
26
27
28
29
30
# File 'lib/relaxdb/design_doc.rb', line 25

def add_view(view_name, type, function)
  @data["views"] ||= {}
  @data["views"][view_name] ||= {}
  @data["views"][view_name][type] = function
  self            
end

#destroy!Object



49
50
51
52
53
# File 'lib/relaxdb/design_doc.rb', line 49

def destroy!
  # Implicitly prevent the object from being resaved by failing to update its revision
  RelaxDB.db.delete("#{@data["_id"]}?rev=#{@data["_rev"]}")
  self      
end

#saveObject



32
33
34
35
36
37
# File 'lib/relaxdb/design_doc.rb', line 32

def save
  database = RelaxDB.db    
  resp = database.put(@data["_id"], @data.to_json)
  @data["_rev"] = JSON.parse(resp.body)["rev"]
  self
end