From c0b0cc867061d52765be89d617353f45ea5052aa Mon Sep 17 00:00:00 2001 From: Matthew McGarvey Date: Wed, 5 Aug 2020 15:59:42 -0500 Subject: [PATCH 1/4] temp --- src/lucky_router/part_processor.cr | 71 +++++++++--------------------- 1 file changed, 21 insertions(+), 50 deletions(-) diff --git a/src/lucky_router/part_processor.cr b/src/lucky_router/part_processor.cr index 0e1c331..cec8470 100644 --- a/src/lucky_router/part_processor.cr +++ b/src/lucky_router/part_processor.cr @@ -11,56 +11,27 @@ class LuckyRouter::PartProcessor(T) def run unless parts.empty? - process_parts + current_part = parts.first + next_parts = parts.skip(1) + + if current_part.starts_with?(":") + dynamic_part = fragment.dynamic_part ||= Fragment::DynamicFragment.new( + name: current_part.gsub(":", ""), + fragment: Fragment(T).new + ) + dynamic_part.fragment.process_parts(next_parts, method, payload) + + if next_parts.empty? + dynamic_part.fragment.method_to_payload[method] = payload + end + else + new_fragment = fragment.static_parts[current_part] ||= Fragment(T).new + new_fragment.process_parts(next_parts, method, payload) + + if next_parts.empty? + new_fragment.method_to_payload[method] = payload + end + end end end - - private def process_parts - if dynamic_part? - add_dynamic_part - else - add_static_part - end - end - - private def dynamic_part? - current_part.starts_with?(":") - end - - private def current_part - parts.first - end - - private def next_parts - parts.skip(1) - end - - private def add_dynamic_part - fragment.dynamic_part ||= Fragment::DynamicFragment.new( - name: current_part.gsub(":", ""), - fragment: Fragment(T).new - ) - fragment.dynamic_part.not_nil!.fragment.process_parts(next_parts, method, payload) - - if on_last_part? - add_payload_to_dynamic_part - end - end - - def on_last_part? - next_parts.empty? - end - - private def add_static_part - fragment.static_parts[current_part] ||= Fragment(T).new - fragment.static_parts[current_part].process_parts(next_parts, method, payload) - - if next_parts.empty? - fragment.static_parts[current_part].method_to_payload[method] = payload - end - end - - private def add_payload_to_dynamic_part - fragment.dynamic_part.not_nil!.fragment.method_to_payload[method] = payload - end end From 8cc4b71c67ab2d11c66ca81d25402da1538fae75 Mon Sep 17 00:00:00 2001 From: Matthew McGarvey Date: Wed, 5 Aug 2020 16:18:47 -0500 Subject: [PATCH 2/4] Completely removed PartProcessor --- src/lucky_router/fragment.cr | 20 ++++++++++++---- src/lucky_router/part_processor.cr | 37 ------------------------------ 2 files changed, 16 insertions(+), 41 deletions(-) delete mode 100644 src/lucky_router/part_processor.cr diff --git a/src/lucky_router/fragment.cr b/src/lucky_router/fragment.cr index 06e988b..ea1443c 100644 --- a/src/lucky_router/fragment.cr +++ b/src/lucky_router/fragment.cr @@ -59,12 +59,24 @@ class LuckyRouter::Fragment(T) # the final step to finding the payload is to search for a matching request method getter method_to_payload = Hash(String, T).new + def find(parts : Array(String), method : String) : Match(T) | NoMatch + MatchFinder(T).new(self, parts: parts, method: method).run + end + def process_parts(parts : Array(String), method : String, payload : T) - PartProcessor(T).new(self, parts: parts, method: method, payload: payload).run - self + leaf_fragment = parts.reduce(self) { |fragment, part| fragment.add_part(part) } + leaf_fragment.method_to_payload[method] = payload end - def find(parts : Array(String), method : String) : Match(T) | NoMatch - MatchFinder(T).new(self, parts: parts, method: method).run + def add_part(part : String) : Fragment(T) + if part.starts_with?(":") + dynamic_fragment = self.dynamic_part ||= DynamicFragment(T).new( + name: part.gsub(":", ""), + fragment: Fragment(T).new + ) + return dynamic_fragment.fragment + end + + static_parts[part] ||= Fragment(T).new end end diff --git a/src/lucky_router/part_processor.cr b/src/lucky_router/part_processor.cr deleted file mode 100644 index cec8470..0000000 --- a/src/lucky_router/part_processor.cr +++ /dev/null @@ -1,37 +0,0 @@ -class LuckyRouter::PartProcessor(T) - private getter fragment, payload, method, parts - - @fragment : Fragment(T) - @payload : T - @parts : Array(String) - @method : String - - def initialize(@fragment, @parts, @method, @payload) - end - - def run - unless parts.empty? - current_part = parts.first - next_parts = parts.skip(1) - - if current_part.starts_with?(":") - dynamic_part = fragment.dynamic_part ||= Fragment::DynamicFragment.new( - name: current_part.gsub(":", ""), - fragment: Fragment(T).new - ) - dynamic_part.fragment.process_parts(next_parts, method, payload) - - if next_parts.empty? - dynamic_part.fragment.method_to_payload[method] = payload - end - else - new_fragment = fragment.static_parts[current_part] ||= Fragment(T).new - new_fragment.process_parts(next_parts, method, payload) - - if next_parts.empty? - new_fragment.method_to_payload[method] = payload - end - end - end - end -end From 3ce0fb1ff92f9b4c1784a64a8ef6752eaade94ff Mon Sep 17 00:00:00 2001 From: Matthew McGarvey Date: Wed, 5 Aug 2020 16:23:31 -0500 Subject: [PATCH 3/4] Minor optimization --- src/lucky_router/fragment.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lucky_router/fragment.cr b/src/lucky_router/fragment.cr index ea1443c..9ea55ab 100644 --- a/src/lucky_router/fragment.cr +++ b/src/lucky_router/fragment.cr @@ -71,7 +71,7 @@ class LuckyRouter::Fragment(T) def add_part(part : String) : Fragment(T) if part.starts_with?(":") dynamic_fragment = self.dynamic_part ||= DynamicFragment(T).new( - name: part.gsub(":", ""), + name: part[1...], fragment: Fragment(T).new ) return dynamic_fragment.fragment From e8bf69d76d577173ddd326fa6f2a8f6ca9d3a436 Mon Sep 17 00:00:00 2001 From: Matthew McGarvey Date: Mon, 10 Aug 2020 15:53:45 -0500 Subject: [PATCH 4/4] clarify adding parts --- src/lucky_router/fragment.cr | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/lucky_router/fragment.cr b/src/lucky_router/fragment.cr index 9ea55ab..3fff425 100644 --- a/src/lucky_router/fragment.cr +++ b/src/lucky_router/fragment.cr @@ -70,13 +70,18 @@ class LuckyRouter::Fragment(T) def add_part(part : String) : Fragment(T) if part.starts_with?(":") - dynamic_fragment = self.dynamic_part ||= DynamicFragment(T).new( - name: part[1...], - fragment: Fragment(T).new - ) - return dynamic_fragment.fragment + dynamic_fragment = self.dynamic_part ||= create_dynamic_fragment(part) + dynamic_fragment.fragment + else + static_parts[part] ||= Fragment(T).new end + end - static_parts[part] ||= Fragment(T).new + private def create_dynamic_fragment(part : String) : DynamicFragment(T) + part_without_colon = part[1...] + DynamicFragment(T).new( + name: part_without_colon, + fragment: Fragment(T).new + ) end end