# Fonctions auxiliaires pour l'implantation de minirails # transformer le contenu d'un hash en une suite d'attributs d'une balise HTML def hash2attributes(html_options) return "" if html_options.nil? html_options.inject(""){|acc,v|acc+" #{v[0].to_s}='#{v[1].to_s}'"} end # générer un lien vers la page name en ajoutant des options, on peut aussi ajouter des attributs # HTML supplémentaires def link_to(name, options = {}, html_options = nil) href=[] href<<""<#{name}" end # faire la mise en page d'une page, on ne traite que :action=>... et :layout=>false def render(options) # puts "render(#{options.inspect})" $rendering_action=options[:action] if options[:layout]==false $do_layout=false # puts "setting do_layout to false" end end private ## methods and variables for creating directories and files # Global variable to keep track of the components of the relative path. # Its starts empty and each subdirectory is pushed on entry and popped on exit $pwd=[] # builds the full name of the file from the individual components of the current # working directory and adds the file_name f at the end def full_name(f) File.join($pwd+[f]) end # checks if file or directory named f exists # if so # - it prints a message indicating that it exists # - returns false. # otherwise # - it calls the block to get creation action # - prints a message indicating the creation # - returns true def check_file(f) if File.exists?(f) puts "exists #{full_name(f)}" false else yield puts "create #{full_name(f)}" true end end # creates a new directory and calls the block to create its content def change_dir(d) if block_given? $pwd.push(d) Dir.chdir(d){yield} $pwd.pop end end def create_dir(d) check_file(d){Dir.mkdir(d)} if block_given? $pwd.push(d) Dir.chdir(d){yield} $pwd.pop end end # if file f exists # - create a new file # - call the block to gets its string content def create_file(f,exec=false) if check_file(f){if exec then File.new(f,File::CREAT|File::RDWR,0755) else File.new(f,"w") end} File.open(f,"w") do |f| f.puts yield end end end # build the directory structure starting with app_name def create_application(app_name) create_dir(app_name) do create_dir("app") do create_dir("controllers") create_dir("db") create_dir("models") create_dir("views")do create_dir("layouts") end end create_dir("script") do create_file("server",true) do <3000}) s.mount('/',MiniRailsServlet) trap("INT") { s.shutdown } s.start EOD end create_file("generate",true) do < Minirails application: #{model_name} <%= @content_for_layout %> EOD end end end end end