lib/hanami/routes.rb in hanami-2.1.0.beta2.1 vs lib/hanami/routes.rb in hanami-2.1.0.rc1
- old
+ new
@@ -58,28 +58,59 @@
def initialize(endpoint)
super("#{endpoint.inspect} is not compatible with Rack. Please make sure it implements #call.")
end
end
+ # Wrapper class for the (otherwise opaque) proc returned from {.routes}, adding an `#empty?`
+ # method that returns true if no routes were defined.
+ #
+ # This is useful when needing to determine behaviour based on the presence of user-defined
+ # routes, such as determining whether to show the Hanami welcome page in {Slice#load_router}.
+ #
# @api private
+ # @since 2.1.0
+ class RoutesProc < DelegateClass(Proc)
+ # @api private
+ # @since 2.1.0
+ def self.empty
+ new(proc {}, empty: true)
+ end
+
+ # @api private
+ # @since 2.1.0
+ def initialize(proc, empty: false)
+ @empty = empty
+ super(proc)
+ end
+
+ # @api private
+ # @since 2.1.0
+ def empty?
+ !!@empty
+ end
+ end
+
+ # @api private
def self.routes
@routes ||= build_routes
end
class << self
# @api private
def build_routes(definitions = self.definitions)
- return if definitions.empty?
+ return RoutesProc.empty if definitions.empty?
- proc do
+ routes_proc = proc do
definitions.each do |(name, args, kwargs, block)|
if block
public_send(name, *args, **kwargs, &block)
else
public_send(name, *args, **kwargs)
end
end
end
+
+ RoutesProc.new(routes_proc)
end
# @api private
def definitions
@definitions ||= []