extract method

This commit is contained in:
Chris Anderson 2009-01-01 21:22:14 -08:00
parent f8dd2766d5
commit 98ff079093

View file

@ -19,6 +19,22 @@ module CouchRest
@db = CouchRest.new(host).database(dbname) @db = CouchRest.new(host).database(dbname)
end end
def push_app(appdir, appname)
libs = []
viewdir = File.join(appdir,"views")
attachdir = File.join(appdir,"_attachments")
views, lang = read_design_views(viewdir)
docid = "_design/#{appname}"
design = @db.get(docid) rescue {}
design['_id'] = docid
design['views'] = views
design['language'] = lang if lang
@db.save(design)
push_directory(attachdir, docid)
push_fields(appdir, docid)
end
def push_directory(push_dir, docid=nil) def push_directory(push_dir, docid=nil)
docid ||= push_dir.split('/').reverse.find{|part|!part.empty?} docid ||= push_dir.split('/').reverse.find{|part|!part.empty?}
@ -214,28 +230,12 @@ module CouchRest
end end
def push_app(appdir, appname) def dir_to_fields(dir)
libs = []
viewdir = File.join(appdir,"views")
attachdir = File.join(appdir,"_attachments")
views, lang = read_design_views(viewdir)
docid = "_design/#{appname}"
design = @db.get(docid) rescue {}
design['_id'] = docid
design['views'] = views
design['language'] = lang if lang
@db.save(design)
push_directory(attachdir, docid)
push_fields(appdir, docid)
end
def push_fields(appdir, docid)
fields = {} fields = {}
(Dir["#{appdir}/**/*.*"] - (Dir["#{dir}/**/*.*"] -
Dir["#{appdir}/views/**/*.*"] - Dir["#{dir}/views/**/*.*"] -
Dir["#{appdir}/_attachments/**/*.*"]).each do |file| Dir["#{dir}/_attachments/**/*.*"]).each do |file|
farray = file.sub(appdir, '').sub(/^\//,'').split('/') farray = file.sub(dir, '').sub(/^\//,'').split('/')
myfield = fields myfield = fields
while farray.length > 1 while farray.length > 1
front = farray.shift front = farray.shift
@ -250,6 +250,11 @@ module CouchRest
myfield[fname] = fguts myfield[fname] = fguts
end end
end end
return fields
end
def push_fields(appdir, docid)
fields = dir_to_fields(appdir)
design = @db.get(docid) rescue {} design = @db.get(docid) rescue {}
design.merge!(fields) design.merge!(fields)
@db.save(design) @db.save(design)
@ -303,6 +308,30 @@ module CouchRest
[views, language] [views, language]
end end
def read_forms(formdir)
lib = {}
language = nil
forms = {}
Dir["#{formdir}/*.*"].each do |viewfile|
view_parts = viewfile.split('/')
viewfile_name = view_parts.last
# example-map.js
viewfile_name_parts = viewfile_name.split('.')
viewfile_ext = viewfile_name_parts.last
view_name_parts = viewfile_name_parts.first.split('-')
func_type = view_name_parts.pop
view_name = view_name_parts.join('-')
contents = File.open(viewfile).read
if /^lib\..*$/.match viewfile_name
libs.push(contents)
else
views[view_name] ||= {}
language = LANGS[viewfile_ext]
views[view_name][func_type] = contents.sub(/(\/\/|#)include-lib/,libs.join("\n"))
end
end
end
def say words def say words
puts words if @loud puts words if @loud
end end