Module: Roda::RodaPlugins::Path::ClassMethods
- Defined in:
- lib/roda/plugins/path.rb
Instance Method Summary collapse
-
#path(name, path = nil, opts = {}, &block) ⇒ Object
Create a new instance method for the named path.
Instance Method Details
#path(name, path = nil, opts = {}, &block) ⇒ Object
Create a new instance method for the named path. See plugin module documentation for options.
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 |
# File 'lib/roda/plugins/path.rb', line 38 def path(name, path=nil, opts={}, &block) if path.is_a?(Hash) raise RodaError, "cannot provide two option hashses to Roda.path" unless opts.empty? opts = path path = nil end raise RodaError, "cannot provide both path and block to Roda.path" if path && block raise RodaError, "must provide either path or block to Roda.path" unless path || block if path path = path.dup.freeze block = lambda{path} end meth = opts[:name] || "#{name}_path" url = opts[:url] add_script_name = opts[:add_script_name] if add_script_name || url || opts[:url_only] _meth = "_#{meth}" define_method(_meth, &block) end unless opts[:url_only] if add_script_name define_method(meth) do |*a, &blk| request.script_name.to_s + send(_meth, *a, &blk) end else define_method(meth, &block) end end if url || opts[:url_only] url_meth = if url.is_a?(String) || url.is_a?(Symbol) url else "#{name}_url" end url_block = lambda do |*a, &blk| r = request scheme = r.scheme port = r.port uri = ["#{scheme}://#{r.host}#{":#{port}" unless DEFAULT_PORTS[scheme] == port}"] uri << request.script_name.to_s if add_script_name uri << send(_meth, *a, &blk) File.join(uri) end define_method(url_meth, &url_block) end nil end |