Class: Jets::Engine

Inherits:
Turbine show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/jets/engine.rb,
lib/jets/engine/turbines.rb,
lib/jets/engine/configuration.rb

Direct Known Subclasses

Application

Defined Under Namespace

Classes: Configuration, Turbines

Constant Summary

Constants inherited from Turbine

Turbine::ABSTRACT_TURBINES

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Turbine

<=>, abstract_turbine?, #configure, configure, console, generators, #inspect, instance, on_exception, #on_exception_blocks, rake_tasks, runner, server, subclasses, turbine_name, #turbine_namespace

Methods included from Initializable

included, #initializers, #run_initializers

Constructor Details

#initializeEngine

Returns a new instance of Engine.



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/jets/engine.rb', line 91

def initialize
  @_all_autoload_paths = nil
  @_all_load_paths     = nil
  @app                 = nil
  @config              = nil
  @env_config          = nil
  @helpers             = nil
  @routes              = nil
  @app_build_lock      = Mutex.new
  super
end

Class Attribute Details

.called_fromObject

Returns the value of attribute called_from.



12
13
14
# File 'lib/jets/engine.rb', line 12

def called_from
  @called_from
end

.isolatedObject Also known as: isolated?

Returns the value of attribute isolated.



12
13
14
# File 'lib/jets/engine.rb', line 12

def isolated
  @isolated
end

Class Method Details

.endpoint(endpoint = nil) ⇒ Object



37
38
39
40
41
# File 'lib/jets/engine.rb', line 37

def endpoint(endpoint = nil)
  @endpoint ||= nil
  @endpoint = endpoint if endpoint
  @endpoint
end

.find(path) ⇒ Object

Finds engine with given path.



75
76
77
78
79
80
81
82
# File 'lib/jets/engine.rb', line 75

def find(path)
  expanded_path = File.expand_path path
  Jets::Engine.subclasses.each do |klass|
    engine = klass.instance
    return engine if File.expand_path(engine.root) == expanded_path
  end
  nil
end

.find_root(from) ⇒ Object



33
34
35
# File 'lib/jets/engine.rb', line 33

def find_root(from)
  find_root_with_flag "lib", from
end

.inherited(base) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jets/engine.rb', line 19

def inherited(base)
  unless base.abstract_turbine?
    Jets::Turbine::Configuration.eager_load_namespaces << base

    base.called_from = begin
      call_stack = caller_locations.map { |l| l.absolute_path || l.path }

      File.dirname(call_stack.detect { |p| !p.match?(%r[turbines[\w.-]*/lib/jets|rack[\w.-]*/lib/rack]) })
    end
  end

  super
end

.isolate_namespace(mod) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/jets/engine.rb', line 43

def isolate_namespace(mod)
  engine_name(generate_turbine_name(mod.name))

  routes.default_scope = { module: ActiveSupport::Inflector.underscore(mod.name) }
  self.isolated = true

  unless mod.respond_to?(:turbine_namespace)
    name, turbine = engine_name, self

    mod.singleton_class.instance_eval do
      define_method(:turbine_namespace) { turbine }

      unless mod.respond_to?(:table_name_prefix)
        define_method(:table_name_prefix) { "#{name}_" }
      end

      unless mod.respond_to?(:use_relative_model_naming?)
        class_eval "def use_relative_model_naming?; true; end", __FILE__, __LINE__
      end

      unless mod.respond_to?(:turbine_helpers_paths)
        define_method(:turbine_helpers_paths) { turbine.helpers_paths }
      end

      unless mod.respond_to?(:turbine_routes_url_helpers)
        define_method(:turbine_routes_url_helpers) { |include_path_helpers = true| turbine.routes.url_helpers(include_path_helpers) }
      end
    end
  end
end

Instance Method Details

#appObject

Returns the underlying Rack application for this engine.



170
171
172
173
174
175
176
177
178
# File 'lib/jets/engine.rb', line 170

def app
  @app || @app_build_lock.synchronize {
    @app ||= begin
      stack = default_middleware_stack
      config.middleware = build_middleware.merge_into(stack)
      config.middleware.build(endpoint)
    end
  }
end

#call(env) ⇒ Object

Define the Rack API for this engine.



188
189
190
191
# File 'lib/jets/engine.rb', line 188

def call(env)
  env.merge!(env_config)
  app.call(env) # to Jets::Middleware#app
end

#configObject

Define the configuration object for the engine.



208
209
210
# File 'lib/jets/engine.rb', line 208

def config
  @config ||= Engine::Configuration.new(self.class.find_root(self.class.called_from))
end

#eager_load!Object



143
144
145
146
# File 'lib/jets/engine.rb', line 143

def eager_load!
  # Already done by Zeitwerk::Loader.eager_load_all. By now, we leave the
  # method as a no-op for backwards compatibility.
end

#endpointObject

Returns the endpoint for this engine. If none is registered, defaults to an ActionDispatch::Routing::RouteSet.



182
183
184
185
# File 'lib/jets/engine.rb', line 182

def endpoint
  # self.class.endpoint || routes
  self.class.endpoint || Jets::Controller::Middleware::Main
end

#env_configObject

Defines additional Rack env configuration that is added on each call.



194
195
196
# File 'lib/jets/engine.rb', line 194

def env_config
  @env_config ||= {}
end

#helpersObject

Returns a module with all the helpers defined for the engine.



153
154
155
156
157
158
159
160
161
162
# File 'lib/jets/engine.rb', line 153

def helpers
  @helpers ||= begin
    helpers = Module.new
    all = ActionController::Base.all_helpers_from_path(helpers_paths)
    ActionController::Base.modules_for_helpers(all).each do |mod|
      helpers.include(mod)
    end
    helpers
  end
end

#helpers_pathsObject

Returns all registered helpers paths.



165
166
167
# File 'lib/jets/engine.rb', line 165

def helpers_paths
  paths["app/helpers"].existent
end

#load_console(app = self) ⇒ Object

Load console and invoke the registered hooks. Check Jets::Turbine.console for more info.



105
106
107
108
109
110
# File 'lib/jets/engine.rb', line 105

def load_console(app = self)
  require "jets/console/app"
  require "jets/console/helpers"
  run_console_blocks(app)
  self
end

#load_generators(app = self) ⇒ Object

Load Jets generators and invoke the registered hooks. Check Jets::Turbine.generators for more info.



129
130
131
132
133
134
# File 'lib/jets/engine.rb', line 129

def load_generators(app = self)
  require "jets/generators"
  run_generators_blocks(app)
  Jets::Generators.configure!(app.config.generators)
  self
end

#load_runner(app = self) ⇒ Object

Load Jets runner and invoke the registered hooks. Check Jets::Turbine.runner for more info.



114
115
116
117
# File 'lib/jets/engine.rb', line 114

def load_runner(app = self)
  run_runner_blocks(app)
  self
end

#load_seedObject

Load data from db/seeds.rb file. It can be used in to load engines’ seeds, e.g.:

Blog::Engine.load_seed



216
217
218
219
# File 'lib/jets/engine.rb', line 216

def load_seed
  seed_file = paths["db/seeds.rb"].existent.first
  run_callbacks(:load_seed) { load(seed_file) } if seed_file
end

#load_server(app = self) ⇒ Object

Invoke the server registered hooks. Check Jets::Turbine.server for more info.



138
139
140
141
# File 'lib/jets/engine.rb', line 138

def load_server(app = self)
  run_server_blocks(app)
  self
end

#load_tasks(app = self) ⇒ Object

Load Rake and turbines tasks, and invoke the registered hooks. Check Jets::Turbine.rake_tasks for more info.



121
122
123
124
125
# File 'lib/jets/engine.rb', line 121

def load_tasks(app = self)
  require "rake"
  run_tasks_blocks(app) # loads tasks like db:migrate
  self
end

#routes(&block) ⇒ Object

Defines the routes for this engine. If a block is given to routes, it is appended to the engine.



200
201
202
203
204
205
# File 'lib/jets/engine.rb', line 200

def routes(&block)
  # @routes ||= ActionDispatch::Routing::RouteSet.new_with_config(config)
  @routes ||= Jets::Router::RouteSet.new(self)
  @routes.append(&block) if block_given?
  @routes
end

#routes?Boolean

:nodoc:

Returns:

  • (Boolean)


315
316
317
# File 'lib/jets/engine.rb', line 315

def routes? # :nodoc:
  @routes
end

#turbinesObject



148
149
150
# File 'lib/jets/engine.rb', line 148

def turbines
  @turbines ||= Turbines.new
end