Class: RubyLotus::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_lotus/route.rb

Constant Summary collapse

EXCLUDE_PATHS =
%w[rails assets cable historical_location].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, method:, name:, controller:, action:, params:, constraints:, id: nil) ⇒ Route

Returns a new instance of Route.



6
7
8
9
10
11
12
13
14
15
# File 'lib/ruby_lotus/route.rb', line 6

def initialize(path:, method:, name:, controller:, action:, params:, constraints:, id: nil)
  @path = path
  @method = method
  @name = name
  @controller = controller
  @action = action
  @params = params
  @constraints = constraints
  @id = id if id
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



4
5
6
# File 'lib/ruby_lotus/route.rb', line 4

def action
  @action
end

#constraintsObject (readonly)

Returns the value of attribute constraints.



4
5
6
# File 'lib/ruby_lotus/route.rb', line 4

def constraints
  @constraints
end

#controllerObject (readonly)

Returns the value of attribute controller.



4
5
6
# File 'lib/ruby_lotus/route.rb', line 4

def controller
  @controller
end

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/ruby_lotus/route.rb', line 4

def id
  @id
end

#methodObject (readonly)

Returns the value of attribute method.



4
5
6
# File 'lib/ruby_lotus/route.rb', line 4

def method
  @method
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/ruby_lotus/route.rb', line 4

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



4
5
6
# File 'lib/ruby_lotus/route.rb', line 4

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/ruby_lotus/route.rb', line 4

def path
  @path
end

Class Method Details

.from_rails_route(route, id:) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ruby_lotus/route.rb', line 22

def self.from_rails_route(route, id:)
  path = route.ast.to_s.gsub("(.:format)", "")
  return nil if path.match?(/#{EXCLUDE_PATHS.join('|')}/)

  method = route.verb
  name = route.name
  controller = route.defaults[:controller]
  action = route.defaults[:action]
  params = route.parts
  constraints = route.constraints

  new(path:, method:, name:, controller:, action:, params:, constraints:, id:)
end

.from_rails_routesObject



17
18
19
20
# File 'lib/ruby_lotus/route.rb', line 17

def self.from_rails_routes
  routes = Rails.application.routes.routes
  routes.each_with_index.map { |route, id| from_rails_route(route, id:) }.compact
end