Class: JsFromRoutes::Routes

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(routes, config) ⇒ Routes

Returns a new instance of Routes.



14
15
16
17
18
19
# File 'lib/js_from_routes/generator.rb', line 14

def initialize(routes, config)
  @config = config
  @routes = routes
    .uniq { |route| route.requirements.fetch(:action) }
    .map { |route| Route.new(route, config.helper_mappings) }
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



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

def routes
  @routes
end

Instance Method Details

#cache_keyObject

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



23
24
25
# File 'lib/js_from_routes/generator.rb', line 23

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

#evaluate(compiled_template) ⇒ Object

Internal: By performing the evaluation here, we ensure only “routes” is exposed to the ERB template as a local variable.



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

def evaluate(compiled_template)
  instance_eval(compiled_template)
end