Class: Sapp::RouteMap
- Inherits:
-
Object
- Object
- Sapp::RouteMap
- Defined in:
- lib/sapp/route_map.rb
Instance Attribute Summary collapse
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
- #add(verb, path, &handler) ⇒ Object
- #add_handler(path_hash, handler) ⇒ Object
- #add_path(path_hash, controller, &handler) ⇒ Object
- #add_path_to_index(controller, path) ⇒ Object
- #add_stream(path_hash, controller) ⇒ Object
- #create_set(controller, path_hash, &handler) ⇒ Object
- #empty_proc ⇒ Object
- #get_or_create_controller(path_hash, verbs) ⇒ Object
- #get_or_create_index(controller) ⇒ Object
- #get_or_create_paths(controller) ⇒ Object
- #get_or_create_verb(verb) ⇒ Object
-
#initialize ⇒ RouteMap
constructor
A new instance of RouteMap.
- #namespaces ⇒ Object
- #options ⇒ Object
- #path_exist?(controller, path) ⇒ Boolean
- #remove(verb, path = nil) ⇒ Object
- #set_namespace(names, nest) ⇒ Object
- #update_path(controller, path_hash, handler) ⇒ Object
Constructor Details
#initialize ⇒ RouteMap
Returns a new instance of RouteMap.
8 9 10 |
# File 'lib/sapp/route_map.rb', line 8 def initialize @routes = Hash.new end |
Instance Attribute Details
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
6 7 8 |
# File 'lib/sapp/route_map.rb', line 6 def routes @routes end |
Instance Method Details
#add(verb, path, &handler) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/sapp/route_map.rb', line 34 def add verb, path, &handler path_hash = Sapp::Path::Base.new(path, options: ).parse verbs = get_or_create_verb verb controller = get_or_create_controller path_hash, verbs if path_exist? controller, path_hash[:original] update_path controller, path_hash, handler else create_set controller, path_hash, &handler end end |
#add_handler(path_hash, handler) ⇒ Object
101 102 103 |
# File 'lib/sapp/route_map.rb', line 101 def add_handler path_hash, handler path_hash.merge handler: handler end |
#add_path(path_hash, controller, &handler) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/sapp/route_map.rb', line 93 def add_path path_hash, controller, &handler if block_given? controller[:paths] << add_handler(path_hash, handler) else controller[:paths] << add_handler(path_hash, empty_proc) end end |
#add_path_to_index(controller, path) ⇒ Object
68 69 70 |
# File 'lib/sapp/route_map.rb', line 68 def add_path_to_index controller, path controller[:index] << path end |
#add_stream(path_hash, controller) ⇒ Object
89 90 91 |
# File 'lib/sapp/route_map.rb', line 89 def add_stream path_hash, controller controller[:streams] << path_hash[:stream] end |
#create_set(controller, path_hash, &handler) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/sapp/route_map.rb', line 61 def create_set controller, path_hash, &handler get_or_create_paths controller get_or_create_index controller add_path path_hash, controller, &handler add_path_to_index controller, path_hash[:original] end |
#empty_proc ⇒ Object
24 25 26 |
# File 'lib/sapp/route_map.rb', line 24 def empty_proc @empty_proc ||= Proc.new { "Placeholder" } end |
#get_or_create_controller(path_hash, verbs) ⇒ Object
76 77 78 79 |
# File 'lib/sapp/route_map.rb', line 76 def get_or_create_controller path_hash, verbs controller = path_hash[:controller] verbs[controller] ||= Hash.new end |
#get_or_create_index(controller) ⇒ Object
85 86 87 |
# File 'lib/sapp/route_map.rb', line 85 def get_or_create_index controller controller[:index] ||= Array.new end |
#get_or_create_paths(controller) ⇒ Object
81 82 83 |
# File 'lib/sapp/route_map.rb', line 81 def get_or_create_paths controller controller[:paths] ||= Array.new end |
#get_or_create_verb(verb) ⇒ Object
72 73 74 |
# File 'lib/sapp/route_map.rb', line 72 def get_or_create_verb verb routes[verb] ||= Hash.new end |
#namespaces ⇒ Object
12 13 14 |
# File 'lib/sapp/route_map.rb', line 12 def namespaces @namespaces ||= Array.new end |
#options ⇒ Object
28 29 30 31 32 |
# File 'lib/sapp/route_map.rb', line 28 def = Hash.new [:namespaces] = namespaces if namespaces end |
#path_exist?(controller, path) ⇒ Boolean
57 58 59 |
# File 'lib/sapp/route_map.rb', line 57 def path_exist? controller, path controller.any? && controller[:index].include?(path) end |
#remove(verb, path = nil) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/sapp/route_map.rb', line 105 def remove verb, path=nil if path routes[verb].delete path else routes.delete verb end end |
#set_namespace(names, nest) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/sapp/route_map.rb', line 16 def set_namespace names, nest if nest && namespaces.any? @namespaces = namespaces << names else @namespaces = [names] end end |
#update_path(controller, path_hash, handler) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/sapp/route_map.rb', line 48 def update_path controller, path_hash, handler map = controller[:paths].collect do |path| if path[:original] == path_hash[:original] add_handler(path_hash, handler) end end controller[:paths] = map end |