Class: CouchRest::FileManager
- Inherits:
-
Object
- Object
- CouchRest::FileManager
- Defined in:
- lib/couchrest/helper/file_manager.rb
Constant Summary collapse
- LANGS =
{"rb" => "ruby", "js" => "javascript"}
- MIMES =
{ "html" => "text/html", "htm" => "text/html", "png" => "image/png", "css" => "text/css", "js" => "test/javascript" }
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#loud ⇒ Object
Returns the value of attribute loud.
Class Method Summary collapse
-
.generate_app(app_dir) ⇒ Object
Generate an application in the given directory.
Instance Method Summary collapse
-
#initialize(dbname, host = "http://localhost:5984") ⇒ FileManager
constructor
A new instance of FileManager.
- #pull_views(view_dir) ⇒ Object
- #push_app(appdir, appname) ⇒ Object
- #push_directory(push_dir, docid = nil) ⇒ Object
- #push_views(view_dir) ⇒ Object
Constructor Details
#initialize(dbname, host = "http://localhost:5984") ⇒ FileManager
Returns a new instance of FileManager.
16 17 18 |
# File 'lib/couchrest/helper/file_manager.rb', line 16 def initialize(dbname, host="http://localhost:5984") @db = CouchRest.new(host).database(dbname) end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
5 6 7 |
# File 'lib/couchrest/helper/file_manager.rb', line 5 def db @db end |
#loud ⇒ Object
Returns the value of attribute loud.
6 7 8 |
# File 'lib/couchrest/helper/file_manager.rb', line 6 def loud @loud end |
Class Method Details
.generate_app(app_dir) ⇒ Object
Generate an application in the given directory. This is a class method because it doesn’t depend on specifying a database.
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/couchrest/helper/file_manager.rb', line 208 def self.generate_app(app_dir) FileUtils.mkdir_p(app_dir) FileUtils.mkdir_p(File.join(app_dir,"attachments")) FileUtils.mkdir_p(File.join(app_dir,"views")) index_template = File.join(File.(File.dirname(__FILE__)), 'templates','index.html') index_dest = File.join(app_dir,"attachments","index.html") FileUtils.cp(index_template, index_dest) map_template = File.join(File.(File.dirname(__FILE__)), 'templates','example-map.js') map_dest = File.join(app_dir,"views","example-map.js") FileUtils.cp(map_template, map_dest) rereduce_template = File.join(File.(File.dirname(__FILE__)), 'templates','example-reduce.js') rereduce_dest = File.join(app_dir,"views","example-reduce.js") FileUtils.cp(rereduce_template, rereduce_dest) end |
Instance Method Details
#pull_views(view_dir) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/couchrest/helper/file_manager.rb', line 155 def pull_views(view_dir) prefix = "_design" ds = db.documents(:startkey => '#{prefix}/', :endkey => '#{prefix}/ZZZZZZZZZ') ds['rows'].collect{|r|r['id']}.each do |id| puts directory = id.split('/').last FileUtils.mkdir_p(File.join(view_dir,directory)) views = db.get(id)['views'] vgroups = views.keys.group_by{|k|k.sub(/\-(map|reduce)$/,'')} vgroups.each do|g,vs| mapname = vs.find {|v|views[v]["map"]} if mapname # save map mapfunc = views[mapname]["map"] mapfile = File.join(view_dir, directory, "#{g}-map.js") # todo support non-js views File.open(mapfile,'w') do |f| f.write mapfunc end end reducename = vs.find {|v|views[v]["reduce"]} if reducename # save reduce reducefunc = views[reducename]["reduce"] reducefile = File.join(view_dir, directory, "#{g}-reduce.js") # todo support non-js views File.open(reducefile,'w') do |f| f.write reducefunc end end end end end |
#push_app(appdir, appname) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/couchrest/helper/file_manager.rb', line 189 def push_app(appdir, appname) libs = [] viewdir = File.join(appdir,"views") attachdir = File.join(appdir,"attachments") views, lang = read_design_views(viewdir) # attachments = read_attachments(attachdir) docid = "_design/#{appname}" design = @db.get(docid) rescue {} design['_id'] = docid design['views'] = views design['language'] = lang @db.save(design) push_directory(attachdir, docid) # puts views.inspect end |
#push_directory(push_dir, docid = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/couchrest/helper/file_manager.rb', line 20 def push_directory(push_dir, docid=nil) docid ||= push_dir.split('/').reverse.find{|part|!part.empty?} pushfiles = Dir["#{push_dir}/**/*.*"].collect do |f| {f.split("#{push_dir}/").last => open(f).read} end return if pushfiles.empty? @attachments = {} @signatures = {} pushfiles.each do |file| name = file.keys.first value = file.values.first @signatures[name] = md5(value) @attachments[name] = { "data" => value, "content_type" => MIMES[name.split('.').last] } end doc = @db.get(docid) rescue nil unless doc say "creating #{docid}" @db.save({"_id" => docid, "_attachments" => @attachments, "signatures" => @signatures}) return end doc["signatures"] ||= {} doc["_attachments"] ||= {} # remove deleted docs to_be_removed = doc["signatures"].keys.select do |d| !pushfiles.collect{|p| p.keys.first}.include?(d) end to_be_removed.each do |p| say "deleting #{p}" doc["signatures"].delete(p) doc["_attachments"].delete(p) end # update existing docs: doc["signatures"].each do |path, sig| if (@signatures[path] == sig) say "no change to #{path}. skipping..." else say "replacing #{path}" doc["signatures"][path] = md5(@attachments[path]["data"]) doc["_attachments"][path].delete("stub") doc["_attachments"][path].delete("length") doc["_attachments"][path]["data"] = @attachments[path]["data"] doc["_attachments"][path].merge!({"data" => @attachments[path]["data"]} ) end end # add in new files new_files = pushfiles.select{|d| !doc["signatures"].keys.include?( d.keys.first) } new_files.each do |f| say "creating #{f}" path = f.keys.first content = f.values.first doc["signatures"][path] = md5(content) doc["_attachments"][path] = { "data" => content, "content_type" => MIMES[path.split('.').last] } end begin @db.save(doc) rescue Exception => e say e. end end |
#push_views(view_dir) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/couchrest/helper/file_manager.rb', line 99 def push_views(view_dir) designs = {} Dir["#{view_dir}/**/*.*"].each do |design_doc| design_doc_parts = design_doc.split('/') next if /^lib\..*$/.match design_doc_parts.last pre_normalized_view_name = design_doc_parts.last.split("-") view_name = pre_normalized_view_name[0..pre_normalized_view_name.length-2].join("-") folder = design_doc_parts[-2] designs[folder] ||= {} designs[folder]["views"] ||= {} design_lang = design_doc_parts.last.split(".").last designs[folder]["language"] ||= LANGS[design_lang] libs = "" Dir["#{view_dir}/lib.#{design_lang}"].collect do |global_lib| libs << open(global_lib).read libs << "\n" end Dir["#{view_dir}/#{folder}/lib.#{design_lang}"].collect do |global_lib| libs << open(global_lib).read libs << "\n" end if design_doc_parts.last =~ /-map/ designs[folder]["views"]["#{view_name}-map"] ||= {} designs[folder]["views"]["#{view_name}-map"]["map"] = read(design_doc, libs) designs[folder]["views"]["#{view_name}-reduce"] ||= {} designs[folder]["views"]["#{view_name}-reduce"]["map"] = read(design_doc, libs) end if design_doc_parts.last =~ /-reduce/ designs[folder]["views"]["#{view_name}-reduce"] ||= {} designs[folder]["views"]["#{view_name}-reduce"]["reduce"] = read(design_doc, libs) end end # cleanup empty maps and reduces designs.each do |name, props| props["views"].each do |view, funcs| next unless view.include?("reduce") props["views"].delete(view) unless funcs.keys.include?("reduce") end end designs.each do |k,v| create_or_update("_design/#{k}", v) end designs end |