Class: Microframe::Mapper
- Inherits:
-
Object
- Object
- Microframe::Mapper
- Defined in:
- lib/microframe/routing/mapper.rb
Instance Attribute Summary collapse
-
#placeholders ⇒ Object
readonly
Returns the value of attribute placeholders.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(routes) ⇒ Mapper
constructor
A new instance of Mapper.
- #map(verb, path) ⇒ Object
- #match_begin_of_optional_components(a, optional) ⇒ Object
- #match_components(a, b) ⇒ Object
- #match_end_of_optional_components(a, optional) ⇒ Object
- #match_optional_components(match, optional, index) ⇒ Object
- #match_this(routes, path) ⇒ Object
Constructor Details
#initialize(routes) ⇒ Mapper
Returns a new instance of Mapper.
4 5 6 |
# File 'lib/microframe/routing/mapper.rb', line 4 def initialize(routes) @routes = routes end |
Instance Attribute Details
#placeholders ⇒ Object (readonly)
Returns the value of attribute placeholders.
3 4 5 |
# File 'lib/microframe/routing/mapper.rb', line 3 def placeholders @placeholders end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
3 4 5 |
# File 'lib/microframe/routing/mapper.rb', line 3 def routes @routes end |
Class Method Details
.start(route) ⇒ Object
8 9 10 |
# File 'lib/microframe/routing/mapper.rb', line 8 def self.start(route) new(route) end |
Instance Method Details
#map(verb, path) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/microframe/routing/mapper.rb', line 12 def map(verb, path) value = nil routes[verb].each do |route, target| if match_this(route, path) value = target break end end value end |
#match_begin_of_optional_components(a, optional) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/microframe/routing/mapper.rb', line 34 def match_begin_of_optional_components(a, optional) if a[-1] == "(" optional += 1 a.sub!("(", "") changed = true end return a, optional, changed end |
#match_components(a, b) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/microframe/routing/mapper.rb', line 23 def match_components(a, b) found = false if a == b found = true elsif a[0] == ":" && a[-1] != ")" && a[-1] != "(" @placeholders[a[1..-1].to_sym] = b found = true end return found end |
#match_end_of_optional_components(a, optional) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/microframe/routing/mapper.rb', line 43 def match_end_of_optional_components(a, optional) if a[-1] == ")" optional -= 1 a.sub!(")", "") changed = true end return a, optional, changed end |
#match_optional_components(match, optional, index) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/microframe/routing/mapper.rb', line 52 def match_optional_components(match, optional, index) if optional > 0 match = true index -= 1 end return match, index, match end |
#match_this(routes, 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/microframe/routing/mapper.rb', line 60 def match_this(routes, path) match = []; @placeholders = {}; index = 0; route = routes.split("/") paths = path.split("/") return false if route.size < paths.size route.each do |a| pending_match = true; matched = false; optional = 0; b = paths[index] while pending_match matched = match_components(a, b) pending_match = false a, optional, pending_match = match_begin_of_optional_components(a, optional) unless matched || pending_match a, optional, pending_match = match_end_of_optional_components(a, optional) unless matched || pending_match matched, index, pending_match = match_optional_components(matched, optional, index) unless matched || pending_match end match << matched index += 1 end !match.include? false end |