Module: Roda::RodaPlugins::Path::ClassMethods
- Defined in:
- lib/roda/plugins/path.rb
Instance Method Summary collapse
-
#freeze ⇒ Object
Freeze the path classes when freezing the app.
-
#path(name, path = nil, opts = OPTS, &block) ⇒ Object
Create a new instance method for the named path.
-
#path_block(klass) ⇒ Object
Return the block related to the given class, or nil if there is no block.
-
#path_classes ⇒ Object
Hash of recognizes classes for path instance method.
Instance Method Details
#freeze ⇒ Object
Freeze the path classes when freezing the app.
91 92 93 94 95 |
# File 'lib/roda/plugins/path.rb', line 91 def freeze path_classes.freeze opts[:path_classes_methods].freeze super end |
#path(name, path = nil, opts = OPTS, &block) ⇒ Object
Create a new instance method for the named path. See plugin module documentation for options.
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 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 154 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 |
# File 'lib/roda/plugins/path.rb', line 98 def path(name, path=nil, opts=OPTS, &block) if name.is_a?(Class) raise RodaError, "can't provide path or options when calling path with a class" unless path.nil? && opts.empty? raise RodaError, "must provide a block when calling path with a class" unless block if self.opts[:path_class_by_name] name = name.name end path_classes[name] = block self.opts[:path_class_methods][name] = define_roda_method("path_#{name}", :any, &block) return end 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] url_only = opts[:url_only] relative = opts[:relative] add_script_name = opts.fetch(:add_script_name, self.opts[:add_script_name]) if relative if (url || url_only) raise RodaError, "cannot provide :url or :url_only option if using :relative option" end add_script_name = true plugin :relative_path end if add_script_name || url || url_only || relative _meth = "_#{meth}" define_method(_meth, &block) private _meth end unless url_only if relative define_method(meth) do |*a, &blk| # Allow calling private _method to get path relative_path(request.script_name.to_s + send(_meth, *a, &blk)) end elsif add_script_name define_method(meth) do |*a, &blk| # Allow calling private _method to get path request.script_name.to_s + send(_meth, *a, &blk) end else define_method(meth, &block) end end if url || 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 # Allow calling private _method to get path uri << send(_meth, *a, &blk) File.join(uri) end define_method(url_meth, &url_block) end nil end |
#path_block(klass) ⇒ Object
Return the block related to the given class, or nil if there is no block.
185 186 187 188 189 190 191 |
# File 'lib/roda/plugins/path.rb', line 185 def path_block(klass) # RODA4: Remove if opts[:path_class_by_name] klass = klass.name end path_classes[klass] end |
#path_classes ⇒ Object
Hash of recognizes classes for path instance method. Keys are classes, values are procs.
86 87 88 |
# File 'lib/roda/plugins/path.rb', line 86 def path_classes opts[:path_classes] end |