class Lux::Controller LUX_PRINT_ROUTES = !!ENV['LUX_PRINT_ROUTES'] unless defined?(LUX_PRINT_ROUTES) # define common http methods as constants [:get, :head, :post, :delete, :put, :patch].map(&:to_s).map(&:upcase).each { |m| eval "#{m} ||= '#{m}'" } # rescue from errors Lux::RescueFrom.define(self) # this is needed for Rescuce actions to work without Lux prefix define_singleton_method(:page) { Lux.page } ### def debug { :@locale=>@locale, :@nav=>nav, :@subdomain=>@subdomain, :@domain=>@domain } end def body? Lux.page.body ? true : false end def body data Lux.page.body(data) end def page Lux.page end def nav Lux.page.nav end def params Lux.page.params end def redirect where, msgs={} Lux.page.redirect where, msgs end def request Lux.page.request end # gets only root def root opts if LUX_PRINT_ROUTES @route_target = opts print_route end call opts unless nav.root end ### plug and default prepares def plug(name, &block) return if body? m = "#{name}_plug".to_sym return Lux.error(%[Method "#{m}" not defined in Lux::Controller]) unless respond_to?(m) send(m, &block) end def rescued_main if respond_to?(:main) @@rescue_from_ivar.call(self) { main } else Lux.error %[No instance method "main" in Lux::Controller defiend] end end def print_route route = case @route when Regexp @route.to_s.ljust(20).red when Symbol ('/%s' % @route).ljust(20).green when nil '/'.ljust(20).green else @route.to_s.ljust(20).light_blue end route_target = case @route_target when Symbol ':%s' % @route_target when Array action = @route_target[1] == 'print-routes' ? @route : @route_target[1] '%s.action("%s")' % [@route_target[0], action] else @route_target end puts '%s => %s (%s)' % [route, route_target.to_s.ljust(40), @route.class] end def route route_object return if body? if route_object.class == Hash @route = route_object.keys.first @route_target = route_object.values.first else @route = route_object end print_route if LUX_PRINT_ROUTES return unless if @route.is_a?(String) page.nav.to_s.start_with?(@route) elsif @route.is_a?(Regexp) page.nav.to_s =~ @route else nav.root == @route.to_s end @route_object = route_object case @route_target when Proc @route_target.call when Array call @route_target[0], @route_target[1] else call @route_target end end alias :match :route def get object return unless get? route object end def post object return unless post? route object end def get? Lux.page.request.request_method == 'GET' end def post? Lux.page.request.request_method == 'POST' end # call :api_router # call Main::UsersCell # call Main::UsersCell, :index # call 'main/orgs#show' def call object=nil, action=nil return true if body? case object when Symbol return send(object) when String if object.include?('#') object = object.split('#') object[0] = ('%s_cell' % object[0]).classify.constantize else object = ('%s_cell' % object).classify.constantize end end object, action = object if object.is_a? Array Lux.log ' %s %s # %s' % [object.to_s.light_blue, nav.path, @route_object] if action object.action action else object.call end Lux.error 'Lux cell "%s" called via route "%s" but page body is not set' % [object, @route] unless body? true end end