Module: Gear::Nancy

Defined in:
lib/camping/gear/nancy.rb

Overview

Nancy

Nancy is Camping gear that adds Sinatra style routing shortcuts to the Object namespace and to camping controllers themselves:

get '/' {
  "Hello World"
}

Calling the get method creates a controller, and in the event of no default app yet, creates an app named Frank.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.ensure_app(app) ⇒ Object

ensures an app exists for the controllers.



40
41
42
43
44
45
46
47
48
49
# File 'lib/camping/gear/nancy.rb', line 40

def ensure_app(app)
	if Camping::Apps.count == 0
		# In the case of a naked sinatra style invokation
		Camping.goes :Frank
		m = Camping::Apps.first
	else
		m = app
	end
	m
end

.included(mod) ⇒ Object



118
119
120
# File 'lib/camping/gear/nancy.rb', line 118

def included(mod)
	mod.extend(ClassMethods)
end

.make_camping_route(method, routes, app = nil, &block) ⇒ Object

Make a camping route provided with a method type, a route, an optional app, and a required block:

get '/another/thing' do
  render :another_view
end

Calling the shorthand make route helper methods inside of an app module, adds The route to that App. If you don’t have any apps yet, then an app named Frank will be made for you.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
116
# File 'lib/camping/gear/nancy.rb', line 61

def make_camping_route(method, routes, app=nil, &block)

	inf = caller.first.split(":")
	file_name, line_number = inf[0], inf[1]

	meth = method.to_s

	self.normalize_routes(routes) => {s:, rs:, symbol:}

	m = self.ensure_app app

	# Controller name
	cname = "#{meth.capitalize}#{symbol.to_s}"

	begin
		m.module_eval(%Q[
		module Controllers
			class #{cname} < R #{rs}
			end
		end
		], file_name, line_number.to_i)
	rescue => error
		if error.message.include? "superclass mismatch for class"
			raise "You've probably tried to define the same route twice using the sinatra method. ['#{rs}']"
		else
			raise error
		end
	end

	# This is an interesting block. At times we'll pass an App to a route
	# which will implicitly call it's `to_proc` method. In those cases, it's
	# that block that is set as the block here, and it returns a Rack response.
	# If we have a rack response instead of string, then we need to extract
	# the response then reassign the values. the r method  is a great helper
	# for that.
	constantine = m::X.const_get("#{cname}")
	if block.arity == -1
		constantine.send(:define_method, meth) { |*args|
			block[*args]
		}
	elsif block.arity == 1
		constantine.send(:define_method, meth) {
			res = block[@env] # if we're forwarding a response
			status = res[0]
			headers = res[1]
			body = res[2].flatten.first
			r(status, body, headers)
		}
	else # assuming arity is 0
		constantine.send(:define_method, meth) {
			block[]
		}
	end

	return nil
end

.normalize_routes(routes) ⇒ Object

normalizes the routes provided to the controller, then returns some variables used in make_camping_route



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/camping/gear/nancy.rb', line 20

def normalize_routes(routes)
	s = ""
	rs = ""
	routes.each do |r|
		if r == '/'
			r = 'Index'
		end
		rs += "'#{r}'" + ","
		r.split("/").each(&:capitalize!).each{|t|
			s << t.gsub(/[^a-z0-9A-Z ]/, '')
		}
		# s << r
	end
	rs.chop!

	symbol = s.to_sym
	{s: s, rs: rs, symbol: symbol}
end

.setup(app, *a, &block) ⇒ Object

required for compliance reasons



123
# File 'lib/camping/gear/nancy.rb', line 123

def setup(app, *a, &block) end