Module: Couchbase::Operations::DesignDocs

Defined in:
lib/couchbase/operations/design_docs.rb

Defined Under Namespace

Classes: DesignDocAccess

Instance Method Summary collapse

Instance Method Details

#delete_design_doc(id, rev = nil) ⇒ true, false

Delete design doc with given id and revision.

Parameters:

  • id (String)

    Design document id. It might have ‘_design/’ prefix.

  • rev (String) (defaults to: nil)

    Document revision. It uses latest revision if rev parameter is nil.

Returns:

  • (true, false)

Since:

  • 1.2.0



94
95
96
# File 'lib/couchbase/operations/design_docs.rb', line 94

def delete_design_doc(id, rev = nil)
  client.deleteDesignDoc(id)
end

#design_docsHash

Fetch design docs stored in current bucket

Returns:

  • (Hash)

Since:

  • 1.2.0



42
43
44
# File 'lib/couchbase/operations/design_docs.rb', line 42

def design_docs
  DesignDocAccess.new(self)
end

#save_design_doc(data) ⇒ true, false

Update or create design doc with supplied views

Parameters:

  • data (Hash, IO, String)

    The source object containing JSON encoded design document. It must have _id key set, this key should start with _design/.

Returns:

  • (true, false)

Since:

  • 1.2.0



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/couchbase/operations/design_docs.rb', line 55

def save_design_doc(data)
  attrs = case data
          when String
            MultiJson.load(data)
          when IO
            MultiJson.load(data.read)
          when Hash
            data
          else
            raise ArgumentError, "Document should be Hash, String or IO instance"
          end

  id = attrs.delete('_id').to_s.split('/')[1]

  design_doc = DesignDocument.new(id)

  attrs['views'].each_pair do |view, functions|
    view_design = if functions['reduce']
                    ViewDesign.new(view.to_s.to_java_string, functions['map'].to_java_string, functions['reduce'].to_java_string)
                  else
                    ViewDesign.new(view.to_s.to_java_string, functions['map'].to_java_string)
                  end
    design_doc.getViews.add(view_design)
  end

  client.createDesignDoc(design_doc)
end