module Roda::RodaPlugins::Path::ClassMethods

  1. lib/roda/plugins/path.rb

Methods

Public Instance

  1. freeze
  2. path
  3. path_block

Public Instance methods

freeze()

Freeze the path classes when freezing the app.

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