module Roda::RodaPlugins::Path::ClassMethods

  1. lib/roda/plugins/path.rb

Methods

Public Instance

  1. freeze
  2. path
  3. path_block
  4. path_classes

Public Instance methods

freeze()

Freeze the path classes when freezing the app.

[show source]
    # File lib/roda/plugins/path.rb
104 def freeze
105   path_classes.freeze
106   opts[:path_classes_methods].freeze
107   super
108 end
path(name, path=nil, opts=OPTS, &block)

Create a new instance method for the named path. See plugin module documentation for options.

[show source]
    # File lib/roda/plugins/path.rb
111 def path(name, path=nil, opts=OPTS, &block)
112   if name.is_a?(Class) || (path.is_a?(Hash) && (class_name = path[:class_name]))
113     raise RodaError, "can't provide path when calling path with a class" if path && !class_name
114     raise RodaError, "can't provide options when calling path with a class" unless opts.empty?
115     raise RodaError, "must provide a block when calling path with a class" unless block
116     if self.opts[:path_class_by_name]
117       if class_name
118         name = name.to_s
119       else
120         name = name.name
121       end
122     elsif class_name
123       name = name.to_s
124       raise RodaError, "invalid class name passed when using class_name option" unless VALID_CONSTANT_NAME_REGEXP =~ name
125       name = Object.class_eval(name, __FILE__, __LINE__)
126     end
127     path_classes[name] = block
128     self.opts[:path_class_methods][name] = define_roda_method("path_#{name}", :any, &block)
129     return
130   end
131 
132   if path.is_a?(Hash)
133     raise RodaError,  "cannot provide two option hashses to Roda.path" unless opts.empty?
134     opts = path
135     path = nil
136   end
137 
138   raise RodaError,  "cannot provide both path and block to Roda.path" if path && block
139   raise RodaError,  "must provide either path or block to Roda.path" unless path || block
140 
141   if path
142     path = path.dup.freeze
143     block = lambda{path}
144   end
145 
146   meth = opts[:name] || "#{name}_path"
147   url = opts[:url]
148   url_only = opts[:url_only]
149   relative = opts[:relative]
150   add_script_name = opts.fetch(:add_script_name, self.opts[:add_script_name])
151 
152   if relative
153     if (url || url_only)
154       raise RodaError,  "cannot provide :url or :url_only option if using :relative option"
155     end
156     add_script_name = true
157     plugin :relative_path
158   end
159 
160   if add_script_name || url || url_only || relative
161     _meth = "_#{meth}"
162     define_method(_meth, &block)
163     private _meth
164   end
165 
166   unless url_only
167     if relative
168       define_method(meth) do |*a, &blk|
169         # Allow calling private _method to get path
170         relative_path(request.script_name.to_s + send(_meth, *a, &blk))
171       end
172       # :nocov:
173       ruby2_keywords(meth) if respond_to?(:ruby2_keywords, true)
174       # :nocov:
175     elsif add_script_name
176       define_method(meth) do |*a, &blk|
177         # Allow calling private _method to get path
178         request.script_name.to_s + send(_meth, *a, &blk)
179       end
180       # :nocov:
181       ruby2_keywords(meth) if respond_to?(:ruby2_keywords, true)
182       # :nocov:
183     else
184       define_method(meth, &block)
185     end
186   end
187 
188   if url || url_only
189     url_meth = if url.is_a?(String) || url.is_a?(Symbol)
190       url
191     else
192       "#{name}_url"
193     end
194 
195     url_block = lambda do |*a, &blk|
196       # Allow calling private _method to get path
197       "#{_base_url}#{request.script_name if add_script_name}#{send(_meth, *a, &blk)}"
198     end
199 
200     define_method(url_meth, &url_block)
201     # :nocov:
202     ruby2_keywords(url_meth) if respond_to?(:ruby2_keywords, true)
203     # :nocov:
204   end
205 
206   nil
207 end
path_block(klass)

Return the block related to the given class, or nil if there is no block.

[show source]
    # File lib/roda/plugins/path.rb
210 def path_block(klass)
211   # RODA4: Remove
212   if opts[:path_class_by_name]
213     klass = klass.name
214   end
215   path_classes[klass]
216 end
path_classes()

Hash of recognizes classes for path instance method. Keys are classes, values are procs.

[show source]
    # File lib/roda/plugins/path.rb
 99 def path_classes
100   opts[:path_classes]
101 end