Class: ActionFramework::Routes

Inherits:
Object
  • Object
show all
Defined in:
lib/actionframework/routes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRoutes

Returns a new instance of Routes.



13
14
15
16
17
# File 'lib/actionframework/routes.rb', line 13

def initialize
	@routes = {:get => {}, :post => {}, :update => {}, :delete => {}, :patch => {}}
	@models = []
	@redirects = []
end

Instance Attribute Details

#modelsObject

Returns the value of attribute models.



10
11
12
# File 'lib/actionframework/routes.rb', line 10

def models
  @models
end

#redirectsObject

Returns the value of attribute redirects.



11
12
13
# File 'lib/actionframework/routes.rb', line 11

def redirects
  @redirects
end

#routesObject

Returns the value of attribute routes.



9
10
11
# File 'lib/actionframework/routes.rb', line 9

def routes
  @routes
end

Instance Method Details

#build_regex(string) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/actionframework/routes.rb', line 19

def build_regex string
	string = string.gsub("{{","(?<")
	string = string.gsub("}}",">(.*))") 
	string.insert(0,"^")
	string = string+"$"
	regex = Regexp.new (string)
	regex
end

#delete(hash) ⇒ Object



40
41
42
# File 'lib/actionframework/routes.rb', line 40

def delete hash
	@routes[:delete][build_regex(hash.keys.first.to_s)] = hash[hash.keys.first.to_s] 
end

#get(hash) ⇒ Object



28
29
30
# File 'lib/actionframework/routes.rb', line 28

def get hash
	@routes[:get][build_regex(hash.keys.first.to_s)] = hash[hash.keys.first.to_s] 
end

#model(name) ⇒ Object



48
49
50
51
# File 'lib/actionframework/routes.rb', line 48

def model name
	# @models[name of the class of the model] = name of class of the access policy of the model
	@models << name 
end

#patch(hash) ⇒ Object



44
45
46
# File 'lib/actionframework/routes.rb', line 44

def patch hash
	@routes[:patch][build_regex(hash.keys.first.to_s)] = hash[hash.keys.first.to_s] 
end

#post(hash) ⇒ Object



32
33
34
# File 'lib/actionframework/routes.rb', line 32

def post hash
	@routes[:post][build_regex(hash.keys.first.to_s)] = hash[hash.keys.first.to_s] 
end

#redirect(hash) ⇒ Object



64
65
66
67
# File 'lib/actionframework/routes.rb', line 64

def redirect(hash)
	hash[:from] = build_regex(hash[:from])
	@redirects << hash
end

#redirect?(req) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/actionframework/routes.rb', line 69

def redirect? (req)
	@redirects.each do |redirect|
		if(matched = req.path.match(redirect[:from]))
			begin
				matched.names.each do |key|
					redirect[:to] = redirect[:to].gsub("{{#{key}}}",matched[key])
				end
			rescue Exception => e
			end
			return OpenStruct.new(redirect)
		end
	end	
	nil
end

#route(path, method) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/actionframework/routes.rb', line 53

def route(path,method)
	@routes[method.downcase.to_sym].each do |regex,controller|
		puts regex.inspect
		
		if(matched = path.match(regex))
			return [controller,matched]
		end
	end
	return nil
end

#update(hash) ⇒ Object



36
37
38
# File 'lib/actionframework/routes.rb', line 36

def update hash
	@routes[:update][build_regex(hash.keys.first.to_s)] = hash[hash.keys.first.to_s] 
end