Class: RouteGuard::Models::Route

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verb:, path:, original_path:, controller:, action:, name: nil, constraints: {}, file: nil, line: nil, defaults: {}) ⇒ Route

Returns a new instance of Route.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/route_guard/models/route.rb', line 8

def initialize(verb:, path:, original_path:, controller:, action:, name: nil, constraints: {}, file: nil, line: nil, defaults: {})
  @verb = verb.to_s.upcase
  @original_path = original_path.to_s
  @path = path.to_s
  @controller = controller&.to_s
  @action = action&.to_s
  @name = name&.to_s
  @constraints = constraints || {}
  @file = file
  @line = line ? line.to_i : nil
  @defaults = defaults || {}
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



6
7
8
# File 'lib/route_guard/models/route.rb', line 6

def action
  @action
end

#constraintsObject (readonly)

Returns the value of attribute constraints.



6
7
8
# File 'lib/route_guard/models/route.rb', line 6

def constraints
  @constraints
end

#controllerObject (readonly)

Returns the value of attribute controller.



6
7
8
# File 'lib/route_guard/models/route.rb', line 6

def controller
  @controller
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



6
7
8
# File 'lib/route_guard/models/route.rb', line 6

def defaults
  @defaults
end

#fileObject (readonly)

Returns the value of attribute file.



6
7
8
# File 'lib/route_guard/models/route.rb', line 6

def file
  @file
end

#lineObject (readonly)

Returns the value of attribute line.



6
7
8
# File 'lib/route_guard/models/route.rb', line 6

def line
  @line
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/route_guard/models/route.rb', line 6

def name
  @name
end

#original_pathObject (readonly)

Returns the value of attribute original_path.



6
7
8
# File 'lib/route_guard/models/route.rb', line 6

def original_path
  @original_path
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/route_guard/models/route.rb', line 6

def path
  @path
end

#verbObject (readonly)

Returns the value of attribute verb.



6
7
8
# File 'lib/route_guard/models/route.rb', line 6

def verb
  @verb
end

Instance Method Details

#internal?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/route_guard/models/route.rb', line 21

def internal?
  controller.to_s.start_with?("rails/") || path.start_with?("/rails/") || path == "/assets"
end

#locationObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/route_guard/models/route.rb', line 25

def location
  if file && line
    # Try to make path relative to pwd for better readability
    relative_file = file.sub(/\A#{Regexp.escape(Dir.pwd)}\//, "")
    "#{relative_file}:#{line}"
  elsif file
    file.sub(/\A#{Regexp.escape(Dir.pwd)}\//, "")
  else
    "unknown location"
  end
end

#to_sObject



37
38
39
# File 'lib/route_guard/models/route.rb', line 37

def to_s
  "#{verb} #{path}"
end