Class: JsFromRoutes::ControllerRoutes

Inherits:
Object
  • Object
show all
Defined in:
lib/js_from_routes/generator.rb

Overview

Internal: Helper class used as a presenter for the routes template.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, routes, config) ⇒ ControllerRoutes

Returns a new instance of ControllerRoutes.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/js_from_routes/generator.rb', line 15

def initialize(controller, routes, config)
  @controller, @config = controller, config
  @routes = routes
    .reject { |route| route.requirements[:action] == "update" && route.verb == "PUT" }
    .group_by { |route| route.requirements.fetch(:action) }
    .flat_map { |action, routes|
      routes.each_with_index.map { |route, index|
        Route.new(route, mappings: config.helper_mappings, index:, controller:)
      }
    }
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



13
14
15
# File 'lib/js_from_routes/generator.rb', line 13

def routes
  @routes
end

Instance Method Details

#basenameObject

Internal: The base name of the JS file to be written.



54
55
56
# File 'lib/js_from_routes/generator.rb', line 54

def basename
  "#{@controller.camelize}#{@config.file_suffix}".tr_s(":", "/")
end

#cache_keyObject

Public: Used to check whether the file should be generated again, changes based on the configuration, and route definition.



29
30
31
# File 'lib/js_from_routes/generator.rb', line 29

def cache_key
  routes.map(&:inspect).join + [File.read(@config.template_path), @config.helper_mappings.inspect, @config.client_library].join
end

#client_libraryObject

Public: Exposes the preferred import library to the generator.



34
35
36
# File 'lib/js_from_routes/generator.rb', line 34

def client_library
  @config.client_library
end

#filenameObject

Internal: Name of the JS file with helpers for the the given controller.



39
40
41
# File 'lib/js_from_routes/generator.rb', line 39

def filename
  @config.output_folder.join(basename)
end

#import_filenameObject

Public: Name of the JS file with helpers for the the given controller.



44
45
46
# File 'lib/js_from_routes/generator.rb', line 44

def import_filename
  filename.relative_path_from(@config.output_folder).to_s.sub(/\.\w+$/, "")
end

#js_nameObject

Public: Name of the file as a valid JS variable.



49
50
51
# File 'lib/js_from_routes/generator.rb', line 49

def js_name
  @controller.camelize(:lower).tr(":", "")
end