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 (backward compatibility - returns first requirement).
-
#auth_requirements ⇒ Array<String>
Get all authentication requirements as an array Supports multiple strategies: auth=session,apikey,oauth.
-
#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.
-
#role_requirement ⇒ String?
Get role requirement for route-level authorization Supports single role or comma-separated roles (OR logic): role=admin,editor.
-
#role_requirements ⇒ Array<String>
Get all role requirements as an array Supports multiple roles with OR logic: role=admin,editor.
-
#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.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/otto/route_definition.rb', line 39 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.
16 17 18 |
# File 'lib/otto/route_definition.rb', line 16 def definition @definition end |
#keys ⇒ Array<String> (readonly)
Returns The parameter keys extracted from the path.
37 38 39 |
# File 'lib/otto/route_definition.rb', line 37 def keys @keys end |
#kind ⇒ Symbol (readonly)
Returns The invocation kind (:class, :instance, or :logic).
28 29 30 |
# File 'lib/otto/route_definition.rb', line 28 def kind @kind end |
#klass_name ⇒ String (readonly)
Returns The class name portion.
22 23 24 |
# File 'lib/otto/route_definition.rb', line 22 def klass_name @klass_name end |
#method_name ⇒ String (readonly)
Returns The method name portion.
25 26 27 |
# File 'lib/otto/route_definition.rb', line 25 def method_name @method_name end |
#options ⇒ Hash (readonly)
Returns The route options (auth, response, csrf, etc.).
31 32 33 |
# File 'lib/otto/route_definition.rb', line 31 def @options end |
#path ⇒ String (readonly)
Returns The URL path pattern.
13 14 15 |
# File 'lib/otto/route_definition.rb', line 13 def path @path end |
#pattern ⇒ Regexp (readonly)
Returns The compiled path pattern for matching.
34 35 36 |
# File 'lib/otto/route_definition.rb', line 34 def pattern @pattern end |
#target ⇒ String (readonly)
Returns The target class and method (e.g., “TestApp.index”).
19 20 21 |
# File 'lib/otto/route_definition.rb', line 19 def target @target end |
#verb ⇒ String (readonly)
Returns The HTTP verb (GET, POST, etc.).
10 11 12 |
# File 'lib/otto/route_definition.rb', line 10 def verb @verb end |
Instance Method Details
#auth_requirement ⇒ String?
Get authentication requirement (backward compatibility - returns first requirement)
78 79 80 |
# File 'lib/otto/route_definition.rb', line 78 def auth_requirement auth_requirements.first end |
#auth_requirements ⇒ Array<String>
Get all authentication requirements as an array Supports multiple strategies: auth=session,apikey,oauth
85 86 87 88 89 90 |
# File 'lib/otto/route_definition.rb', line 85 def auth_requirements auth = option(:auth) return [] unless auth auth.split(',').map(&:strip).reject(&:empty?) end |
#csrf_exempt? ⇒ Boolean
Check if CSRF is exempt for this route
117 118 119 |
# File 'lib/otto/route_definition.rb', line 117 def csrf_exempt? option(:csrf) == 'exempt' end |
#has_option?(key) ⇒ Boolean
Check if route has specific option
64 65 66 |
# File 'lib/otto/route_definition.rb', line 64 def has_option?(key) @options.key?(key.to_sym) end |
#inspect ⇒ String
Detailed inspection
162 163 164 |
# File 'lib/otto/route_definition.rb', line 162 def inspect "#<Otto::RouteDefinition #{self} options=#{@options.inspect}>" end |
#logic_route? ⇒ Boolean
Check if this is a Logic class route (no . or # in target)
123 124 125 |
# File 'lib/otto/route_definition.rb', line 123 def logic_route? kind == :logic end |
#option(key, default = nil) ⇒ Object
Get option value with optional default
72 73 74 |
# File 'lib/otto/route_definition.rb', line 72 def option(key, default = nil) @options.fetch(key.to_sym, default) end |
#response_type ⇒ String
Get response type
111 112 113 |
# File 'lib/otto/route_definition.rb', line 111 def response_type option(:response, 'default') end |
#role_requirement ⇒ String?
Get role requirement for route-level authorization Supports single role or comma-separated roles (OR logic): role=admin,editor
95 96 97 |
# File 'lib/otto/route_definition.rb', line 95 def role_requirement option(:role) end |
#role_requirements ⇒ Array<String>
Get all role requirements as an array Supports multiple roles with OR logic: role=admin,editor
102 103 104 105 106 107 |
# File 'lib/otto/route_definition.rb', line 102 def role_requirements role = option(:role) return [] unless role role.split(',').map(&:strip).reject(&:empty?) end |
#to_h ⇒ Hash
Convert to hash representation
139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/otto/route_definition.rb', line 139 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
156 157 158 |
# File 'lib/otto/route_definition.rb', line 156 def to_s "#{@verb} #{@path} #{@definition}" end |
#with_options(new_options) ⇒ RouteDefinition
Create a new RouteDefinition with modified options
130 131 132 133 134 135 |
# File 'lib/otto/route_definition.rb', line 130 def () = @options.merge() new_definition = [@target, *.map { |k, v| "#{k}=#{v}" }].join(' ') self.class.new(@verb, @path, new_definition, pattern: @pattern, keys: @keys) end |