Class: PryRailsDiffRoutes::DiffRoutes

Inherits:
Pry::ClassCommand
  • Object
show all
Defined in:
lib/pry_rails_diff_routes/commands/diff_routes.rb

Constant Summary collapse

R_MODE =
1
M_MODE =
2
N_MODE =
4
A_MODE =
7

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_previous_routesObject

Returns the value of attribute _previous_routes.



7
8
9
# File 'lib/pry_rails_diff_routes/commands/diff_routes.rb', line 7

def _previous_routes
  @_previous_routes
end

Instance Method Details

#options(opt) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/pry_rails_diff_routes/commands/diff_routes.rb', line 30

def options(opt)
  opt.on :S, "save", "Save current routes",
         as: String
  opt.on :R, "removed", "Show removed routes",
         as: String
  opt.on :M, "modified", "Show modified routes",
         as: String
  opt.on :N, "new", "Show new routes",
         as: String
end

#processObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pry_rails_diff_routes/commands/diff_routes.rb', line 41

def process
  Rails.application.reload_routes!
  all_routes = Rails.application.routes.routes

  return save_routes(all_routes) if opts[:S]

  removed_mode = opts[:R] ? R_MODE : 0
  modified_mode = opts[:M] ? M_MODE : 0
  new_mode = opts[:N] ? N_MODE : 0
  display_mode = removed_mode | modified_mode | new_mode

  output.puts process_diff(all_routes, display_mode)
end

#process_diff(current_routes, mode) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pry_rails_diff_routes/commands/diff_routes.rb', line 74

def process_diff(current_routes, mode)
  routes =
    RoutesDiffProcessor.new(
      DiffRoutes._previous_routes,
      wrap_routes(current_routes),
      mode
    )

  if routes.changed?
    routes
  else
    "No routes changed."
  end
end

#save_routes(routes) ⇒ Object

opts`.



56
57
58
59
# File 'lib/pry_rails_diff_routes/commands/diff_routes.rb', line 56

def save_routes(routes)
  DiffRoutes._previous_routes = wrap_routes(routes)
  nil
end

#wrap_routes(routes) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pry_rails_diff_routes/commands/diff_routes.rb', line 61

def wrap_routes(routes)
  prev_name = nil
  routes.reject(&:internal).map do |route|
    if route.name.nil?
      route.instance_variable_set(:@name, prev_name)
    else
      prev_name = route.name
    end

    RouteWrapper.new(route)
  end
end