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(defined_elt, optional) ⇒ Object
- #match_components(defined_elt, incoming_elt) ⇒ Object
- #match_end_of_optional_components(defined_elt, optional) ⇒ Object
- #match_optional_components(match, optional, index) ⇒ Object
- #match_this(defined, incoming) ⇒ Object
Constructor Details
#initialize(routes) ⇒ Mapper
4 5 6 7 |
# File 'lib/microframe/routing/mapper.rb', line 4 def initialize(routes) @routes = routes @placeholders = {}; 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
9 10 11 |
# File 'lib/microframe/routing/mapper.rb', line 9 def self.start(route) new(route) end |
Instance Method Details
#map(verb, path) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/microframe/routing/mapper.rb', line 13 def map(verb, path) value = nil if routes[verb] routes[verb].each do |route, target| if match_this(route, path) value = target break end end end value end |
#match_begin_of_optional_components(defined_elt, optional) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/microframe/routing/mapper.rb', line 37 def match_begin_of_optional_components(defined_elt, optional) if defined_elt[-1] == "(" optional += 1 defined_elt.sub!("(", "") changed = true end return defined_elt, optional, changed end |
#match_components(defined_elt, incoming_elt) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/microframe/routing/mapper.rb', line 26 def match_components(defined_elt, incoming_elt) found = false if defined_elt == incoming_elt found = true elsif defined_elt[0] == ":" && defined_elt[-1] != ")" && defined_elt[-1] != "(" && !incoming_elt.nil? @placeholders[defined_elt[1..-1].to_sym] = incoming_elt found = true end return found end |
#match_end_of_optional_components(defined_elt, optional) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/microframe/routing/mapper.rb', line 46 def match_end_of_optional_components(defined_elt, optional) if defined_elt[-1] == ")" optional -= 1 defined_elt.sub!(")", "") changed = true end return defined_elt, optional, changed end |
#match_optional_components(match, optional, index) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/microframe/routing/mapper.rb', line 55 def match_optional_components(match, optional, index) if optional > 0 match = true index -= 1 end return match, index, match end |
#match_this(defined, incoming) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/microframe/routing/mapper.rb', line 63 def match_this(defined, incoming) match = []; index = 0; defined_route = defined.split("/") incoming_route = incoming.split("/") return false if defined_route.size < incoming_route.size defined_route.each do |route_elt| pending_match = true; matched = false; optional = 0; incoming_elt = incoming_route[index] while pending_match matched = match_components(route_elt, incoming_elt) pending_match = false route_elt, optional, pending_match = match_begin_of_optional_components(route_elt, optional) unless matched || pending_match route_elt, optional, pending_match = match_end_of_optional_components(route_elt, 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 |