Class: Couch::DesignDocument

Inherits:
Object
  • Object
show all
Defined in:
lib/couch/design_document.rb

Constant Summary collapse

MIME_TYPE_MAPPING =

Mime type mapping from extensions

{
  ".html" => "text/html",
  ".js" => "text/javascript",
  ".css" => "text/css",
}
JAVASCRIPT_FILES =

Files that should have a .js extension

%w[
  validate_doc_update
  lists/*
  shows/*
  updates/*
  views/*/*
]
EXCLUDE_FILES =

Files that should not be included in document

%w[
  README
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDesignDocument

Returns a new instance of DesignDocument.



28
29
30
# File 'lib/couch/design_document.rb', line 28

def initialize
  @hash = {}
end

Instance Attribute Details

#hashObject

Returns the value of attribute hash.



26
27
28
# File 'lib/couch/design_document.rb', line 26

def hash
  @hash
end

Instance Method Details

#base_urlObject

Base URL for document



127
128
129
# File 'lib/couch/design_document.rb', line 127

def base_url
  @base_url ||= File.join(database, id)
end

#databaseObject

Accessor for couch database



122
123
124
# File 'lib/couch/design_document.rb', line 122

def database
  @database ||= Couch.database
end

#idObject

Accessor for id



106
107
108
# File 'lib/couch/design_document.rb', line 106

def id
  hash["_id"] || Couch.id
end

#jsonObject

Returns a JSON string representation of the documents hash



94
95
96
# File 'lib/couch/design_document.rb', line 94

def json
  hash.to_json
end

#json=(json) ⇒ Object

Build the documents hash from a JSON string



100
101
102
# File 'lib/couch/design_document.rb', line 100

def json=(json)
  self.hash = JSON.parse(json)
end

#read(*filenames, &block) ⇒ Object

Read document from a filesystem.

Takes a filename, many filenames, or an array of filenames and assign the return value of a yielded block to the hash.

Nested hashes like { “hash” => { “key” => “value” } } can be constructed if the filename contains a slash (/), eg “hash/key”.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/couch/design_document.rb', line 44

def read(*filenames, &block)
  filenames.flatten.uniq.each do |filename|
    # skip exclude files
    next if EXCLUDE_FILES.include?(filename)

    key = filename.dup
    # strip extname from javascript files
    key.sub!(/\.js$/, '') if filename =~ /#{JAVASCRIPT_FILES.join('|')}/

    set_hash_at key, block.call(filename)
  end

  map_attachments!
  inject_makros!
end

#revObject

Accessor for rev



111
112
113
# File 'lib/couch/design_document.rb', line 111

def rev
  hash["_rev"] || Couch.rev
end

#rev=(new_rev) ⇒ Object

Updates rev in documents hash



116
117
118
# File 'lib/couch/design_document.rb', line 116

def rev=(new_rev)
  hash["_rev"] = new_rev
end

#url(options = {}) ⇒ Object

URL for accessing design document

Takes an optional options hash which gets converted to url encoded options and appended to the documents base url



137
138
139
# File 'lib/couch/design_document.rb', line 137

def url(options = {})
  base_url + build_options_string(options)
end

#write(directory = nil, doc = nil, &block) ⇒ Object

Write document to a filesystem

Takes a directoy as startpoint (default is nil), a document hash (default is the design documents hash) and recursively yields all keys and values to the given block.

Nested hashes like { “hash” => { “key” => “value” } } will result in the yielded filename “hash/key”.

The key “_attachments” has a special meaning: the value holds base64 encoded data as well as other metadata. This data will gets decoded and used as value for the key.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/couch/design_document.rb', line 75

def write(directory = nil, doc = nil, &block)
  reduce_attachments!
  reject_makros!

  doc ||= hash
  doc.each do |key, value|
    filename = directory ? File.join(directory, key) : key.dup
    if value.is_a?(Hash)
      write(filename, value, &block)
    else
      # append extname to javascript files
      filename << '.js' if filename =~ /#{JAVASCRIPT_FILES.join('|')}/
      block.call(filename, value)
    end
  end
end