Class: RubyRoutes::Route
- Inherits:
-
Object
- Object
- RubyRoutes::Route
- Defined in:
- lib/ruby_routes/route.rb,
lib/ruby_routes/route/small_lru.rb
Defined Under Namespace
Classes: SmallLru
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#constraints ⇒ Object
readonly
Returns the value of attribute constraints.
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
#methods ⇒ Object
readonly
Returns the value of attribute methods.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #collection? ⇒ Boolean
- #extract_params(request_path, parsed_qp = nil) ⇒ Object
-
#generate_path(params = {}) ⇒ Object
Optimized path generation with better caching and fewer allocations.
-
#initialize(path, options = {}) ⇒ Route
constructor
A new instance of Route.
- #match?(request_method, request_path) ⇒ Boolean
- #named? ⇒ Boolean
- #parse_query_params(path) ⇒ Object
-
#query_params(request_path) ⇒ Object
Fast query params method (cached and optimized).
- #resource? ⇒ Boolean
Constructor Details
#initialize(path, options = {}) ⇒ Route
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ruby_routes/route.rb', line 11 def initialize(path, = {}) @path = normalize_path(path) # Pre-normalize and freeze methods at creation time raw_methods = Array([:via] || :get) @methods = raw_methods.map { |m| normalize_method(m) }.freeze @methods_set = @methods.to_set.freeze @controller = extract_controller() @action = [:action] || extract_action([:to]) @name = [:as] @constraints = [:constraints] || {} # Pre-normalize defaults to string keys and freeze @defaults = ([:defaults] || {}).transform_keys(&:to_s).freeze # Pre-compile everything at initialization precompile_route_data validate_route! end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
9 10 11 |
# File 'lib/ruby_routes/route.rb', line 9 def action @action end |
#constraints ⇒ Object (readonly)
Returns the value of attribute constraints.
9 10 11 |
# File 'lib/ruby_routes/route.rb', line 9 def constraints @constraints end |
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
9 10 11 |
# File 'lib/ruby_routes/route.rb', line 9 def controller @controller end |
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
9 10 11 |
# File 'lib/ruby_routes/route.rb', line 9 def defaults @defaults end |
#methods ⇒ Object (readonly)
Returns the value of attribute methods.
9 10 11 |
# File 'lib/ruby_routes/route.rb', line 9 def methods @methods end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/ruby_routes/route.rb', line 9 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/ruby_routes/route.rb', line 9 def path @path end |
Instance Method Details
#collection? ⇒ Boolean
52 53 54 |
# File 'lib/ruby_routes/route.rb', line 52 def collection? !@is_resource end |
#extract_params(request_path, parsed_qp = nil) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/ruby_routes/route.rb', line 35 def extract_params(request_path, parsed_qp = nil) path_params = extract_path_params_fast(request_path) return EMPTY_HASH unless path_params # Use optimized param building build_params_hash(path_params, request_path, parsed_qp) end |
#generate_path(params = {}) ⇒ Object
Optimized path generation with better caching and fewer allocations
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ruby_routes/route.rb', line 61 def generate_path(params = {}) return ROOT_PATH if @path == ROOT_PATH # Fast path: empty params and no required params if params.empty? && @required_params.empty? return @static_path if @static_path end # Build merged params efficiently merged = build_merged_params(params) # Check required params (fast Set operation) missing_params = @required_params_set - merged.keys unless missing_params.empty? raise RubyRoutes::RouteNotFound, "Missing params: #{missing_params.to_a.join(', ')}" end # Check for nil values in required params nil_params = @required_params_set.select { |param| merged[param].nil? } unless nil_params.empty? raise RubyRoutes::RouteNotFound, "Missing or nil params: #{nil_params.to_a.join(', ')}" end # Cache lookup cache_key = build_cache_key_fast(merged) if (cached = @gen_cache.get(cache_key)) return cached end # Generate path using string buffer (avoid array allocations) path_str = generate_path_string(merged) @gen_cache.set(cache_key, path_str) path_str end |
#match?(request_method, request_path) ⇒ Boolean
29 30 31 32 33 |
# File 'lib/ruby_routes/route.rb', line 29 def match?(request_method, request_path) # Fast method check: use frozen Set for O(1) lookup return false unless @methods_set.include?(request_method.to_s.upcase) !!extract_path_params_fast(request_path) end |
#named? ⇒ Boolean
44 45 46 |
# File 'lib/ruby_routes/route.rb', line 44 def named? !@name.nil? end |
#parse_query_params(path) ⇒ Object
56 57 58 |
# File 'lib/ruby_routes/route.rb', line 56 def parse_query_params(path) query_params_fast(path) end |
#query_params(request_path) ⇒ Object
Fast query params method (cached and optimized)
97 98 99 |
# File 'lib/ruby_routes/route.rb', line 97 def query_params(request_path) query_params_fast(request_path) end |
#resource? ⇒ Boolean
48 49 50 |
# File 'lib/ruby_routes/route.rb', line 48 def resource? @is_resource end |