Class: Utopia::Middleware::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/middleware/controller/base.rb,
lib/utopia/middleware/controller/action.rb,
lib/utopia/middleware/controller/variables.rb,
lib/utopia/middleware/controller.rb

Defined Under Namespace

Classes: Action, Base, Variables

Constant Summary collapse

CONTROLLER_RB =
"controller.rb".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Controller

Returns a new instance of Controller.



40
41
42
43
44
45
46
# File 'lib/utopia/middleware/controller.rb', line 40

def initialize(app, options = {})
	@app = app
	@root = options[:root] || Utopia::Middleware::default_root

	@controllers = {}
	@cache_controllers = (UTOPIA_ENV == :production)
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



48
49
50
# File 'lib/utopia/middleware/controller.rb', line 48

def app
  @app
end

Instance Method Details

#call(env) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/utopia/middleware/controller.rb', line 117

def call(env)
	variables = (env["utopia.controller"] ||= Variables.new)
	
	request = Rack::Request.new(env)
	
	if result = invoke_controllers(variables, request)
		return result
	end
	
	return @app.call(env)
end

#invoke_controllers(variables, request, done = Set.new) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/utopia/middleware/controller.rb', line 85

def invoke_controllers(variables, request, done = Set.new)
	path = Path.create(request.path_info)
	controller_path = Path.new
	
	path.descend do |controller_path|
		# puts "Invoke controller: #{controller_path}"
		if controller = lookup_controller(controller_path)
			# We only want to invoke controllers which have not already been invoked:
			unless done.include? controller
				# If we get throw :rewrite, location, the URL has been rewritten and we need to request again:
				location = catch(:rewrite) do
					# Invoke the controller and if it returns a result, send it back out:
					if result = controller.process!(request, path)
						return result
					end
				end
				
				if location
					request.env['PATH_INFO'] = location.to_s
					
					return invoke_controllers(variables, request, done)
				end
				
				done << controller
			end
		end
	end
	
	# No controller gave a useful result:
	return nil
end

#load_controller_file(path) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/utopia/middleware/controller.rb', line 60

def load_controller_file(path)
	uri_path = path
	base_path = File.join(@root, uri_path.components)
	
	controller_path = File.join(base_path, CONTROLLER_RB)
	# puts "load_controller_file(#{path.inspect}) => #{controller_path}"
	
	if File.exist?(controller_path)
		klass = Class.new(Base)
		klass.const_set(:BASE_PATH, base_path)
		klass.const_set(:URI_PATH, uri_path)
		klass.const_set(:CONTROLLER, self)
		
		$LOAD_PATH.unshift(base_path)
		
		klass.class_eval(File.read(controller_path), controller_path)
		
		$LOAD_PATH.delete(base_path)
		
		return klass.new
	else
		return nil
	end
end

#lookup_controller(path) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/utopia/middleware/controller.rb', line 50

def lookup_controller(path)
	if @cache_controllers
		return @controllers.fetch(path.to_s) do |key|
			@controllers[key] = load_controller_file(path)
		end
	else
		return load_controller_file(path)
	end
end