Class: Otto::RouteDefinition

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#definitionString (readonly)

Returns The original definition string.

Returns:

  • (String)

    The original definition string



14
15
16
# File 'lib/otto/route_definition.rb', line 14

def definition
  @definition
end

#keysArray<String> (readonly)

Returns The parameter keys extracted from the path.

Returns:

  • (Array<String>)

    The parameter keys extracted from the path



35
36
37
# File 'lib/otto/route_definition.rb', line 35

def keys
  @keys
end

#kindSymbol (readonly)

Returns The invocation kind (:class, :instance, or :logic).

Returns:

  • (Symbol)

    The invocation kind (:class, :instance, or :logic)



26
27
28
# File 'lib/otto/route_definition.rb', line 26

def kind
  @kind
end

#klass_nameString (readonly)

Returns The class name portion.

Returns:

  • (String)

    The class name portion



20
21
22
# File 'lib/otto/route_definition.rb', line 20

def klass_name
  @klass_name
end

#method_nameString (readonly)

Returns The method name portion.

Returns:

  • (String)

    The method name portion



23
24
25
# File 'lib/otto/route_definition.rb', line 23

def method_name
  @method_name
end

#optionsHash (readonly)

Returns The route options (auth, response, csrf, etc.).

Returns:

  • (Hash)

    The route options (auth, response, csrf, etc.)



29
30
31
# File 'lib/otto/route_definition.rb', line 29

def options
  @options
end

#pathString (readonly)

Returns The URL path pattern.

Returns:

  • (String)

    The URL path pattern



11
12
13
# File 'lib/otto/route_definition.rb', line 11

def path
  @path
end

#patternRegexp (readonly)

Returns The compiled path pattern for matching.

Returns:

  • (Regexp)

    The compiled path pattern for matching



32
33
34
# File 'lib/otto/route_definition.rb', line 32

def pattern
  @pattern
end

#targetString (readonly)

Returns The target class and method (e.g., “TestApp.index”).

Returns:

  • (String)

    The target class and method (e.g., “TestApp.index”)



17
18
19
# File 'lib/otto/route_definition.rb', line 17

def target
  @target
end

#verbString (readonly)

Returns The HTTP verb (GET, POST, etc.).

Returns:

  • (String)

    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_requirementString?

Get authentication requirement

Returns:

  • (String, nil)

    The auth requirement or nil



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

Returns:

  • (Boolean)


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

Parameters:

  • key (Symbol, String)

    Option key to check

Returns:

  • (Boolean)


62
63
64
# File 'lib/otto/route_definition.rb', line 62

def has_option?(key)
  @options.key?(key.to_sym)
end

#inspectString

Detailed inspection

Returns:

  • (String)


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)

Returns:

  • (Boolean)


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

Parameters:

  • key (Symbol, String)

    Option key

  • default (Object) (defaults to: nil)

    Default value if option not present

Returns:

  • (Object)


70
71
72
# File 'lib/otto/route_definition.rb', line 70

def option(key, default = nil)
  @options.fetch(key.to_sym, default)
end

#response_typeString

Get response type

Returns:

  • (String)

    The response type (defaults to ‘default’)



82
83
84
# File 'lib/otto/route_definition.rb', line 82

def response_type
  option(:response, 'default')
end

#to_hHash

Convert to hash representation

Returns:

  • (Hash)


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_sString

String representation for debugging

Returns:

  • (String)


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

Parameters:

  • new_options (Hash)

    Options to merge/override

Returns:



101
102
103
104
105
106
# File 'lib/otto/route_definition.rb', line 101

def with_options(new_options)
  merged_options = @options.merge(new_options)
  new_definition = [@target, *merged_options.map { |k, v| "#{k}=#{v}" }].join(' ')

  self.class.new(@verb, @path, new_definition, pattern: @pattern, keys: @keys)
end