Sha256: 3b7a503252e089faac7da92ebd7b7075dac512b38888cbbe4bc50011f650bee4

Contents?: true

Size: 1.6 KB

Versions: 9

Compression:

Stored size: 1.6 KB

Contents

require 'digest/md5'

module CouchRest
  module Mixins
    module DesignDoc
      
      def self.included(base)
        base.extend(ClassMethods)
      end
      
      module ClassMethods
        def design_doc_id
          "_design/#{design_doc_slug}"
        end

        def design_doc_slug
          return design_doc_slug_cache if (design_doc_slug_cache && design_doc_fresh)
          funcs = []
          design_doc['views'].each do |name, view|
            funcs << "#{name}/#{view['map']}#{view['reduce']}"
          end
          md5 = Digest::MD5.hexdigest(funcs.sort.join(''))
          self.design_doc_slug_cache = "#{self.to_s}-#{md5}"
        end

        def default_design_doc
          {
            "language" => "javascript",
            "views" => {
              'all' => {
                'map' => "function(doc) {
                  if (doc['couchrest-type'] == '#{self.to_s}') {
                    emit(null,null);
                  }
                }"
              }
            }
          }
        end

        def refresh_design_doc
          did = design_doc_id
          saved = database.get(did) rescue nil
          if saved
            design_doc['views'].each do |name, view|
              saved['views'][name] = view
            end
            database.save_doc(saved)
            self.design_doc = saved
          else
            design_doc['_id'] = did
            design_doc.delete('_rev')
            design_doc.database = database
            design_doc.save
          end
          self.design_doc_fresh = true
        end
        
      end # module ClassMethods
      
    end
  end
end

Version data entries

9 entries across 9 versions & 3 rubygems

Version Path
jchris-couchrest-0.16 lib/couchrest/mixins/design_doc.rb
jchris-couchrest-0.2 lib/couchrest/mixins/design_doc.rb
mattetti-couchrest-0.14.1 lib/couchrest/mixins/design_doc.rb
mattetti-couchrest-0.14.2 lib/couchrest/mixins/design_doc.rb
mattetti-couchrest-0.15 lib/couchrest/mixins/design_doc.rb
mattetti-couchrest-0.16 lib/couchrest/mixins/design_doc.rb
mattetti-couchrest-0.17 lib/couchrest/mixins/design_doc.rb
mattetti-couchrest-0.2 lib/couchrest/mixins/design_doc.rb
scottraio-couchrest-0.2 lib/couchrest/mixins/design_doc.rb