Class: Otto::RouteDefinition
- Inherits:
-
Object
- Object
- Otto::RouteDefinition
- Defined in:
- lib/otto/route_definition.rb
Overview
Immutable data class representing a complete route definition This encapsulates all aspects of a route: path, target, and options
Instance Attribute Summary collapse
-
#definition ⇒ String
readonly
The original definition string.
-
#keys ⇒ Array<String>
readonly
The parameter keys extracted from the path.
-
#kind ⇒ Symbol
readonly
The invocation kind (:class, :instance, or :logic).
-
#klass_name ⇒ String
readonly
The class name portion.
-
#method_name ⇒ String
readonly
The method name portion.
-
#options ⇒ Hash
readonly
The route options (auth, response, csrf, etc.).
-
#path ⇒ String
readonly
The URL path pattern.
-
#pattern ⇒ Regexp
readonly
The compiled path pattern for matching.
-
#target ⇒ String
readonly
The target class and method (e.g., “TestApp.index”).
-
#verb ⇒ String
readonly
The HTTP verb (GET, POST, etc.).
Instance Method Summary collapse
-
#auth_requirement ⇒ String?
Get authentication requirement.
-
#csrf_exempt? ⇒ Boolean
Check if CSRF is exempt for this route.
-
#has_option?(key) ⇒ Boolean
Check if route has specific option.
-
#initialize(verb, path, definition, pattern: nil, keys: nil) ⇒ RouteDefinition
constructor
A new instance of RouteDefinition.
-
#inspect ⇒ String
Detailed inspection.
-
#logic_route? ⇒ Boolean
Check if this is a Logic class route (no . or # in target).
-
#option(key, default = nil) ⇒ Object
Get option value with optional default.
-
#response_type ⇒ String
Get response type.
-
#to_h ⇒ Hash
Convert to hash representation.
-
#to_s ⇒ String
String representation for debugging.
-
#with_options(new_options) ⇒ RouteDefinition
Create a new RouteDefinition with modified options.
Constructor Details
#initialize(verb, path, definition, pattern: nil, keys: nil) ⇒ RouteDefinition
Returns a new instance of RouteDefinition.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/otto/route_definition.rb', line 37 def initialize(verb, path, definition, pattern: nil, keys: nil) @verb = verb.to_s.upcase.to_sym @path = path @definition = definition @pattern = pattern @keys = keys || [] # Parse the definition into target and options parsed = parse_definition(definition) @target = parsed[:target] @options = parsed[:options].freeze # Parse the target into class, method, and kind target_parsed = parse_target(@target) @klass_name = target_parsed[:klass_name] @method_name = target_parsed[:method_name] @kind = target_parsed[:kind] # Freeze for immutability freeze end |
Instance Attribute Details
#definition ⇒ String (readonly)
Returns The original definition string.
14 15 16 |
# File 'lib/otto/route_definition.rb', line 14 def definition @definition end |
#keys ⇒ Array<String> (readonly)
Returns The parameter keys extracted from the path.
35 36 37 |
# File 'lib/otto/route_definition.rb', line 35 def keys @keys end |
#kind ⇒ Symbol (readonly)
Returns The invocation kind (:class, :instance, or :logic).
26 27 28 |
# File 'lib/otto/route_definition.rb', line 26 def kind @kind end |
#klass_name ⇒ String (readonly)
Returns The class name portion.
20 21 22 |
# File 'lib/otto/route_definition.rb', line 20 def klass_name @klass_name end |
#method_name ⇒ String (readonly)
Returns The method name portion.
23 24 25 |
# File 'lib/otto/route_definition.rb', line 23 def method_name @method_name end |
#options ⇒ Hash (readonly)
Returns The route options (auth, response, csrf, etc.).
29 30 31 |
# File 'lib/otto/route_definition.rb', line 29 def @options end |
#path ⇒ String (readonly)
Returns The URL path pattern.
11 12 13 |
# File 'lib/otto/route_definition.rb', line 11 def path @path end |
#pattern ⇒ Regexp (readonly)
Returns The compiled path pattern for matching.
32 33 34 |
# File 'lib/otto/route_definition.rb', line 32 def pattern @pattern end |
#target ⇒ String (readonly)
Returns The target class and method (e.g., “TestApp.index”).
17 18 19 |
# File 'lib/otto/route_definition.rb', line 17 def target @target end |
#verb ⇒ String (readonly)
Returns The HTTP verb (GET, POST, etc.).
8 9 10 |
# File 'lib/otto/route_definition.rb', line 8 def verb @verb end |
Instance Method Details
#auth_requirement ⇒ String?
Get authentication requirement
76 77 78 |
# File 'lib/otto/route_definition.rb', line 76 def auth_requirement option(:auth) end |
#csrf_exempt? ⇒ Boolean
Check if CSRF is exempt for this route
88 89 90 |
# File 'lib/otto/route_definition.rb', line 88 def csrf_exempt? option(:csrf) == 'exempt' end |
#has_option?(key) ⇒ Boolean
Check if route has specific option
62 63 64 |
# File 'lib/otto/route_definition.rb', line 62 def has_option?(key) @options.key?(key.to_sym) end |
#inspect ⇒ String
Detailed inspection
133 134 135 |
# File 'lib/otto/route_definition.rb', line 133 def inspect "#<Otto::RouteDefinition #{to_s} options=#{@options.inspect}>" end |
#logic_route? ⇒ Boolean
Check if this is a Logic class route (no . or # in target)
94 95 96 |
# File 'lib/otto/route_definition.rb', line 94 def logic_route? kind == :logic end |
#option(key, default = nil) ⇒ Object
Get option value with optional default
70 71 72 |
# File 'lib/otto/route_definition.rb', line 70 def option(key, default = nil) @options.fetch(key.to_sym, default) end |
#response_type ⇒ String
Get response type
82 83 84 |
# File 'lib/otto/route_definition.rb', line 82 def response_type option(:response, 'default') end |
#to_h ⇒ Hash
Convert to hash representation
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/otto/route_definition.rb', line 110 def to_h { verb: @verb, path: @path, definition: @definition, target: @target, klass_name: @klass_name, method_name: @method_name, kind: @kind, options: @options, pattern: @pattern, keys: @keys } end |
#to_s ⇒ String
String representation for debugging
127 128 129 |
# File 'lib/otto/route_definition.rb', line 127 def to_s "#{@verb} #{@path} #{@definition}" end |
#with_options(new_options) ⇒ RouteDefinition
Create a new RouteDefinition with modified options
101 102 103 104 105 106 |
# File 'lib/otto/route_definition.rb', line 101 def () = @options.merge() new_definition = [@target, *.map { |k, v| "#{k}=#{v}" }].join(' ') self.class.new(@verb, @path, new_definition, pattern: @pattern, keys: @keys) end |