Class: Rails::Routes::Prettier::Objects::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/routes/prettier/objects/route.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route) ⇒ Route

Returns a new instance of Route.



10
11
12
# File 'lib/rails/routes/prettier/objects/route.rb', line 10

def initialize(route)
  @route = route
end

Instance Attribute Details

#routeObject (readonly)

Returns the value of attribute route.



8
9
10
# File 'lib/rails/routes/prettier/objects/route.rb', line 8

def route
  @route
end

Instance Method Details

#actionObject



36
37
38
39
40
# File 'lib/rails/routes/prettier/objects/route.rb', line 36

def action
  return nil if redirect? || custom_router? || basic_auth?

  reqs.split('#').last.split(' ').first
end

#basic_auth?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/rails/routes/prettier/objects/route.rb', line 83

def basic_auth?
  reqs.start_with?('#<Rack::Auth::Basic')
end

#controllerObject



30
31
32
33
34
# File 'lib/rails/routes/prettier/objects/route.rb', line 30

def controller
  return nil if redirect? || custom_router? || basic_auth?

  reqs.split('#').first.split('/').map(&:camelize).join('::').concat('Controller')
end

#custom_router?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/rails/routes/prettier/objects/route.rb', line 87

def custom_router?
  reqs.start_with?('#<')
end

#inspectObject

rubocop:enable Layout/LineLength



75
76
77
# File 'lib/rails/routes/prettier/objects/route.rb', line 75

def inspect
  "##{self}"
end

#nameObject



14
15
16
# File 'lib/rails/routes/prettier/objects/route.rb', line 14

def name
  route[:name]
end

#pathObject



22
23
24
# File 'lib/rails/routes/prettier/objects/route.rb', line 22

def path
  route[:path].gsub(/(\(\.:format\))$/, '')
end

#redirect?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/rails/routes/prettier/objects/route.rb', line 79

def redirect?
  reqs.match?(/redirect(.*,|.*$)/)
end

#req_optionsObject

rubocop:disable Security/Eval



43
44
45
46
47
48
49
50
51
# File 'lib/rails/routes/prettier/objects/route.rb', line 43

def req_options
  return {} if redirect? || basic_auth?

  begin
    eval(reqs.split("#{action} ").last)
  rescue StandardError
    {}
  end
end

#reqsObject



26
27
28
# File 'lib/rails/routes/prettier/objects/route.rb', line 26

def reqs
  route[:reqs]
end

#to_hObject

rubocop:enable Security/Eval



54
55
56
57
58
59
60
61
62
63
# File 'lib/rails/routes/prettier/objects/route.rb', line 54

def to_h
  {
    name: name,
    verb: verb,
    path: path,
    controller: controller,
    action: action,
    req_options: req_options
  }
end

#to_json(*_args) ⇒ Object



65
66
67
# File 'lib/rails/routes/prettier/objects/route.rb', line 65

def to_json(*_args)
  to_h.to_json
end

#to_sObject

rubocop:disable Layout/LineLength



70
71
72
# File 'lib/rails/routes/prettier/objects/route.rb', line 70

def to_s
  "<Route name: '#{name}', verb: '#{verb}', path: '#{path}', controller: '#{controller}', action: '#{action}' req_options: #{req_options}>"
end

#verbObject



18
19
20
# File 'lib/rails/routes/prettier/objects/route.rb', line 18

def verb
  route[:verb]
end