Sha256: 4a35dbf6fd31a0a13cc448fc00b3e22809b1d498b0ec38d87e77d42f4849854c

Contents?: true

Size: 1.65 KB

Versions: 15

Compression:

Stored size: 1.65 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 ||= Design.new(default_design_doc)
          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

15 entries across 15 versions & 6 rubygems

Version Path
glasner-couchrest-0.2.2 lib/couchrest/mixins/design_doc.rb
jchris-couchrest-0.17.0 lib/couchrest/mixins/design_doc.rb
jchris-couchrest-0.2.1 lib/couchrest/mixins/design_doc.rb
jchris-couchrest-0.2.2 lib/couchrest/mixins/design_doc.rb
jchris-couchrest-0.22 lib/couchrest/mixins/design_doc.rb
jrun-couchrest-0.17.1 lib/couchrest/mixins/design_doc.rb
jrun-couchrest-0.2.1.1 lib/couchrest/mixins/design_doc.rb
jrun-couchrest-0.2.1 lib/couchrest/mixins/design_doc.rb
mattetti-couchrest-0.2.1.0 lib/couchrest/mixins/design_doc.rb
mattetti-couchrest-0.2.1 lib/couchrest/mixins/design_doc.rb
mattetti-couchrest-0.20 lib/couchrest/mixins/design_doc.rb
mattetti-couchrest-0.21 lib/couchrest/mixins/design_doc.rb
mattetti-couchrest-0.22 lib/couchrest/mixins/design_doc.rb
matthewford-couchrest-0.2.1 lib/couchrest/mixins/design_doc.rb
samflores-couchrest-0.2.1 lib/couchrest/mixins/design_doc.rb