Sha256: 02566b37f5c2b5ac8c137feaf61f9c6e8ebe4c45da92d1acb6246ba28c4fc053
Contents?: true
Size: 1.53 KB
Versions: 3
Compression:
Stored size: 1.53 KB
Contents
module Couch class Mapper MIME_TYPES = { ".html" => "text/html", ".js" => "text/javascript", ".css" => "text/css", } def initialize(root) raise "Root invalid" unless root && File.directory?(root) @root = root end def doc map @root end private def map(dirname, hash = {}) Dir.entries(dirname).each do |file| next if file =~ /^\./ filename = File.join(dirname, file) if file == "_attachments" hash['_attachments'] = map_attachments(filename) elsif File.directory?(filename) hash[file] = map(filename) elsif File.extname(filename) == '.js' # only .js files are mapped key = file.sub(/\.js$/, '') hash[key] = File.read(filename).strip end end hash end def map_attachments(dirname, hash = {}, keys = []) Dir.entries(dirname).each do |file| next if file =~ /^\./ filename = File.join(dirname, file) base = keys + [file] if File.directory?(filename) map_attachments filename, hash, base else hash[base.join('/')] = { "content_type" => mime_type(filename), "data" => base64(File.read(filename)), } end end hash end # CouchDB needs base64 encodings without spaces def base64(data) [data].pack("m").gsub(/\s/,'') end def mime_type(filename) ext = File.extname(filename) MIME_TYPES[ext] || 'text/plain' end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
couch-0.1.0 | lib/couch/mapper.rb |
couch-0.0.4 | lib/couch/mapper.rb |
couch-0.0.3 | lib/couch/mapper.rb |