Class: Intermodal::API
- Inherits:
-
Rails::Railtie
- Object
- Rails::Railtie
- Intermodal::API
- Includes:
- DSL::Controllers, DSL::Mapping, DSL::PresentationHelpers
- Defined in:
- lib/intermodal/api.rb,
lib/intermodal/api/configuration.rb
Defined Under Namespace
Classes: Configuration
Class Attribute Summary collapse
-
.called_from ⇒ Object
Returns the value of attribute called_from.
Class Method Summary collapse
- .endpoint(endpoint = nil) ⇒ Object
-
.generate_api! ⇒ Object
Setup presenters, acceptors, and controllers.
- .inherited(base) ⇒ Object
- .routes(&blk) ⇒ Object
Instance Method Summary collapse
-
#app ⇒ Object
Returns the underlying rack application for this engine.
-
#call(env) ⇒ Object
Define the Rack API for this engine.
-
#config ⇒ Object
Define the configuration object for the engine.
-
#eager_load! ⇒ Object
Eager load the application by loading all ruby files inside eager_load paths.
-
#endpoint ⇒ Object
Returns the endpoint for this engine.
-
#env_config ⇒ Object
Defines additional Rack env configuration that is added on each call.
-
#helpers ⇒ Object
Returns a module with all the helpers defined for the engine.
-
#initialize ⇒ API
constructor
A new instance of API.
-
#load_console(app = self) ⇒ Object
Load console and invoke the registered hooks.
-
#load_generators(app = self) ⇒ Object
Load Rails generators and invoke the registered hooks.
-
#load_runner(app = self) ⇒ Object
Load Rails runner and invoke the registered hooks.
-
#load_tasks(app = self) ⇒ Object
Load Rake, railties tasks and invoke the registered hooks.
- #railties ⇒ Object
-
#routes ⇒ Object
Defines the routes for this engine.
-
#routes? ⇒ Boolean
:nodoc:.
Methods included from DSL::PresentationHelpers
#attribute, #helper, #map_to, #maybe, #params, #presenter, #to_datetime, #to_presentation
Methods included from DSL::Controllers
#_controller_name, #_create_controller, #_module, #_unload_controller_if_exists, #controllers, #link_resources_from, #load_controllers!, #nested_resources, #resources
Methods included from DSL::Mapping
#acceptance_for, #acceptor_for, #acceptors, #accepts_resource, #load_presentations!, #map_data, #mapping_for, #model_name, #presentation_for, #presenter_for, #presenters, #presents_resource, #resource_name
Constructor Details
#initialize ⇒ API
Returns a new instance of API.
78 79 80 81 82 83 84 85 |
# File 'lib/intermodal/api.rb', line 78 def initialize @app = nil @config = nil @env_config = nil @helpers = nil @routes = nil super end |
Class Attribute Details
.called_from ⇒ Object
Returns the value of attribute called_from.
19 20 21 |
# File 'lib/intermodal/api.rb', line 19 def called_from @called_from end |
Class Method Details
.endpoint(endpoint = nil) ⇒ Object
52 53 54 55 56 |
# File 'lib/intermodal/api.rb', line 52 def endpoint(endpoint = nil) @endpoint ||= nil @endpoint = endpoint if endpoint @endpoint end |
.generate_api! ⇒ Object
Setup presenters, acceptors, and controllers
67 68 69 70 |
# File 'lib/intermodal/api.rb', line 67 def generate_api! self.load_presentations! self.load_controllers! end |
.inherited(base) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/intermodal/api.rb', line 26 def inherited(base) unless base.abstract_railtie? Rails::Railtie::Configuration.eager_load_namespaces << base base.called_from = begin call_stack = if Kernel.respond_to?(:caller_locations) caller_locations.map { |l| l.absolute_path || l.path } else # Remove the line number from backtraces making sure we don't leave anything behind caller.map { |p| p.sub(/:\d+.*/, '') } end File.dirname(call_stack.detect { |p| p !~ %r[railties[\w.-]*/lib/rails|rack[\w.-]*/lib/rack] }) end # Add another initializer each time this has been inherited # This covers a bug here, where the initializers declared in the API itself doesn't # seem to run for whatever reason. initializer "intermodal.load_#{base.name}", before: 'build_middleware_stack' do |app| base.generate_api! end end super end |
.routes(&blk) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/intermodal/api.rb', line 58 def routes(&blk) if block_given? instance.routes(&blk) else instance.routes end end |
Instance Method Details
#app ⇒ Object
Returns the underlying rack application for this engine.
141 142 143 144 145 146 |
# File 'lib/intermodal/api.rb', line 141 def app @app ||= begin config.middleware = config.middleware.merge_into(default_middleware_stack) config.middleware.build(endpoint) end end |
#call(env) ⇒ Object
Define the Rack API for this engine.
155 156 157 158 159 160 161 |
# File 'lib/intermodal/api.rb', line 155 def call(env) env.merge!(env_config) if env['SCRIPT_NAME'] env["ROUTES_#{routes.object_id}_SCRIPT_NAME"] = env['SCRIPT_NAME'].dup end app.call(env) end |
#config ⇒ Object
Define the configuration object for the engine.
179 180 181 |
# File 'lib/intermodal/api.rb', line 179 def config @config ||= API::Configuration.new end |
#eager_load! ⇒ Object
Eager load the application by loading all ruby files inside eager_load paths.
122 123 124 125 126 |
# File 'lib/intermodal/api.rb', line 122 def eager_load! self.load_presentations! self.class.load_controllers! return end |
#endpoint ⇒ Object
Returns the endpoint for this engine. If none is registered, defaults to an ActionDispatch::Routing::RouteSet.
150 151 152 |
# File 'lib/intermodal/api.rb', line 150 def endpoint self.class.endpoint || routes end |
#env_config ⇒ Object
Defines additional Rack env configuration that is added on each call.
164 165 166 167 168 |
# File 'lib/intermodal/api.rb', line 164 def env_config @env_config ||= { 'action_dispatch.routes' => routes } end |
#helpers ⇒ Object
Returns a module with all the helpers defined for the engine.
133 134 135 136 137 138 |
# File 'lib/intermodal/api.rb', line 133 def helpers # returns empty module (for now) @helpers ||= begin Module.new end end |
#load_console(app = self) ⇒ Object
Load console and invoke the registered hooks. Check Rails::Railtie.console
for more info.
89 90 91 92 93 94 |
# File 'lib/intermodal/api.rb', line 89 def load_console(app=self) require "rails/console/app" require "rails/console/helpers" run_console_blocks(app) self end |
#load_generators(app = self) ⇒ Object
Load Rails generators and invoke the registered hooks. Check Rails::Railtie.generators
for more info.
113 114 115 116 117 118 |
# File 'lib/intermodal/api.rb', line 113 def load_generators(app=self) require "rails/generators" run_generators_blocks(app) Rails::Generators.configure!(app.config.generators) self end |
#load_runner(app = self) ⇒ Object
Load Rails runner and invoke the registered hooks. Check Rails::Railtie.runner
for more info.
98 99 100 101 |
# File 'lib/intermodal/api.rb', line 98 def load_runner(app=self) run_runner_blocks(app) self end |
#load_tasks(app = self) ⇒ Object
Load Rake, railties tasks and invoke the registered hooks. Check Rails::Railtie.rake_tasks
for more info.
105 106 107 108 109 |
# File 'lib/intermodal/api.rb', line 105 def load_tasks(app=self) require "rake" run_tasks_blocks(app) self end |
#railties ⇒ Object
128 129 130 |
# File 'lib/intermodal/api.rb', line 128 def railties @railties ||= Railties.new end |
#routes ⇒ Object
Defines the routes for this engine. If a block is given to routes, it is appended to the engine.
172 173 174 175 176 |
# File 'lib/intermodal/api.rb', line 172 def routes @routes ||= ActionDispatch::Routing::RouteSet.new @routes.append(&Proc.new) if block_given? @routes end |
#routes? ⇒ Boolean
:nodoc:
217 218 219 |
# File 'lib/intermodal/api.rb', line 217 def routes? #:nodoc: @routes end |