Module: Ext::Mount::ControllersClassMethods

Defined in:
lib/ext/mount.rb

Instance Method Summary collapse

Instance Method Details

#mount(path, opts = {}) ⇒ Object Also known as: M

Mounts the path given by ‘path’

Options :

* urls : list of accessible
* name : name of the controller, false sets no controller
* listing : set to false if you don't want directory listing.

Option defaults for ‘/var/my_path’ :

* urls : /my_path(|/.*)
* name : MyPath

Renders the view Views#mount_listing if dir listing is enabled.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ext/mount.rb', line 83

def mount(path, opts={})
  defaults = {
    :name    => File.basename(path).gsub(/(^|_)(.)/){ $2.upcase },
    :url     => "/#{File.basename(path)}",
    :listing => true
  } 
  opts = defaults.merge(opts)

  urls = ["#{opts[:url]}(|/.*)"]

  klass = Class.new() do
    meta_def(:urls) { urls }
    meta_def(:path) { path }
    meta_def(:listing?) { opts[:listing] }
  end
  klass.class_eval do
    def get(file='') # :nodoc:
      @path = File.join(self.class.path, file)
      if File.directory?(@path) and self.class.listing?
        @files = Dir[File.join(@path, '*')].map{|f| File.basename(f)}
        if has_view?("#{self.class.name.downcase}_listing")
          render("#{self.class.name.downcase}_listing")
        else
          render(:mount_listing)
        end
      else
        sendfile(@path)
      end
    end
  end
  const_set(opts[:name], klass) if opts[:name]

  if $DBG
    puts "** mounted #{path}"
    puts "Controller : #{klass}"
    puts "Options    : #{opts.inspect}"
  end

  klass
end