tennpipes-base 3.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +20 -0
  3. data/README.rdoc +294 -0
  4. data/Rakefile +1 -0
  5. data/bin/tennpipes +8 -0
  6. data/lib/tennpipes-base.rb +196 -0
  7. data/lib/tennpipes-base/application.rb +175 -0
  8. data/lib/tennpipes-base/application/application_setup.rb +202 -0
  9. data/lib/tennpipes-base/application/authenticity_token.rb +25 -0
  10. data/lib/tennpipes-base/application/flash.rb +229 -0
  11. data/lib/tennpipes-base/application/params_protection.rb +129 -0
  12. data/lib/tennpipes-base/application/routing.rb +1002 -0
  13. data/lib/tennpipes-base/application/show_exceptions.rb +50 -0
  14. data/lib/tennpipes-base/caller.rb +53 -0
  15. data/lib/tennpipes-base/cli/adapter.rb +33 -0
  16. data/lib/tennpipes-base/cli/base.rb +105 -0
  17. data/lib/tennpipes-base/cli/console.rb +20 -0
  18. data/lib/tennpipes-base/cli/launcher.rb +103 -0
  19. data/lib/tennpipes-base/cli/rake.rb +50 -0
  20. data/lib/tennpipes-base/cli/rake_tasks.rb +72 -0
  21. data/lib/tennpipes-base/command.rb +38 -0
  22. data/lib/tennpipes-base/ext/sinatra.rb +29 -0
  23. data/lib/tennpipes-base/filter.rb +52 -0
  24. data/lib/tennpipes-base/images/404.png +0 -0
  25. data/lib/tennpipes-base/images/500.png +0 -0
  26. data/lib/tennpipes-base/loader.rb +202 -0
  27. data/lib/tennpipes-base/logger.rb +492 -0
  28. data/lib/tennpipes-base/module.rb +58 -0
  29. data/lib/tennpipes-base/mounter.rb +308 -0
  30. data/lib/tennpipes-base/path_router.rb +119 -0
  31. data/lib/tennpipes-base/path_router/compiler.rb +110 -0
  32. data/lib/tennpipes-base/path_router/error_handler.rb +8 -0
  33. data/lib/tennpipes-base/path_router/matcher.rb +123 -0
  34. data/lib/tennpipes-base/path_router/route.rb +169 -0
  35. data/lib/tennpipes-base/reloader.rb +309 -0
  36. data/lib/tennpipes-base/reloader/rack.rb +26 -0
  37. data/lib/tennpipes-base/reloader/storage.rb +55 -0
  38. data/lib/tennpipes-base/router.rb +98 -0
  39. data/lib/tennpipes-base/server.rb +119 -0
  40. data/lib/tennpipes-base/tasks.rb +21 -0
  41. data/lib/tennpipes-base/version.rb +20 -0
  42. data/lib/tennpipes-base/version.rb~ +20 -0
  43. data/test/fixtures/app_gem/Gemfile +4 -0
  44. data/test/fixtures/app_gem/app/app.rb +3 -0
  45. data/test/fixtures/app_gem/app_gem.gemspec +17 -0
  46. data/test/fixtures/app_gem/lib/app_gem.rb +7 -0
  47. data/test/fixtures/app_gem/lib/app_gem/version.rb +3 -0
  48. data/test/fixtures/apps/complex.rb +32 -0
  49. data/test/fixtures/apps/demo_app.rb +7 -0
  50. data/test/fixtures/apps/demo_demo.rb +7 -0
  51. data/test/fixtures/apps/demo_project/api/app.rb +7 -0
  52. data/test/fixtures/apps/demo_project/api/lib/api_lib.rb +3 -0
  53. data/test/fixtures/apps/demo_project/app.rb +7 -0
  54. data/test/fixtures/apps/external_apps/fake_lib.rb +1 -0
  55. data/test/fixtures/apps/external_apps/fake_root.rb +2 -0
  56. data/test/fixtures/apps/helpers/class_methods_helpers.rb +4 -0
  57. data/test/fixtures/apps/helpers/instance_methods_helpers.rb +4 -0
  58. data/test/fixtures/apps/helpers/support.rb +1 -0
  59. data/test/fixtures/apps/helpers/system_helpers.rb +8 -0
  60. data/test/fixtures/apps/kiq.rb +3 -0
  61. data/test/fixtures/apps/lib/myklass.rb +2 -0
  62. data/test/fixtures/apps/lib/myklass/mysubklass.rb +4 -0
  63. data/test/fixtures/apps/models/child.rb +2 -0
  64. data/test/fixtures/apps/models/parent.rb +5 -0
  65. data/test/fixtures/apps/mountable_apps/rack_apps.rb +15 -0
  66. data/test/fixtures/apps/mountable_apps/static.html +1 -0
  67. data/test/fixtures/apps/precompiled_app.rb +19 -0
  68. data/test/fixtures/apps/simple.rb +32 -0
  69. data/test/fixtures/apps/static.rb +10 -0
  70. data/test/fixtures/apps/system.rb +13 -0
  71. data/test/fixtures/apps/system_class_methods_demo.rb +7 -0
  72. data/test/fixtures/apps/system_instance_methods_demo.rb +7 -0
  73. data/test/fixtures/dependencies/a.rb +9 -0
  74. data/test/fixtures/dependencies/b.rb +4 -0
  75. data/test/fixtures/dependencies/c.rb +1 -0
  76. data/test/fixtures/dependencies/circular/e.rb +13 -0
  77. data/test/fixtures/dependencies/circular/f.rb +2 -0
  78. data/test/fixtures/dependencies/circular/g.rb +2 -0
  79. data/test/fixtures/dependencies/d.rb +4 -0
  80. data/test/fixtures/reloadable_apps/external/app/app.rb +6 -0
  81. data/test/fixtures/reloadable_apps/external/app/controllers/base.rb +6 -0
  82. data/test/fixtures/reloadable_apps/main/app.rb +10 -0
  83. data/test/helper.rb +30 -0
  84. data/test/test_application.rb +185 -0
  85. data/test/test_core.rb +93 -0
  86. data/test/test_csrf_protection.rb +208 -0
  87. data/test/test_dependencies.rb +57 -0
  88. data/test/test_filters.rb +389 -0
  89. data/test/test_flash.rb +168 -0
  90. data/test/test_locale.rb +21 -0
  91. data/test/test_logger.rb +295 -0
  92. data/test/test_mounter.rb +302 -0
  93. data/test/test_params_protection.rb +195 -0
  94. data/test/test_reloader_complex.rb +74 -0
  95. data/test/test_reloader_external.rb +21 -0
  96. data/test/test_reloader_simple.rb +101 -0
  97. data/test/test_reloader_system.rb +113 -0
  98. data/test/test_restful_routing.rb +33 -0
  99. data/test/test_router.rb +281 -0
  100. data/test/test_routing.rb +2328 -0
  101. metadata +301 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cbcb2d5135e3229fa1a8c47913992afd6c80b665
4
+ data.tar.gz: 2041d262a1202552fcd382bea5d0e4a7f7be8375
5
+ SHA512:
6
+ metadata.gz: 78733ce2ec65e8b5b2e2c0306e87110f5d2739462c86d7b3e3c381b7890bb739ee19a1c3ec39194a2891a13b30bdda8d3f6bf206712fba2bbf5a18649aacf3f2
7
+ data.tar.gz: 40272f77796040f4aa8d42af105f185a002bb4629cac7ed2e3b91ba9ce5f1aab40dcbf7f625d383fe4a3746432ff28a721df80fd6a339e9cba741ef35823302c
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Tennpipes
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,294 @@
1
+ = Tennpipes (tennpipes-base)
2
+
3
+ Tennpipes is the framework based on Sinatra.
4
+
5
+ == Preface
6
+
7
+ Tennpipes is a ruby framework built upon the excellent {Sinatra Microframework}[http://www.sinatrarb.com].
8
+ Sinatra is a DSL for creating simple web applications in Ruby with speed and minimal effort.
9
+ This framework tries hard to make it as fun and easy as possible to code much more advanced web applications by
10
+ building upon the Sinatra philosophies and foundation.
11
+
12
+ == Introduction
13
+
14
+ Many people love Sinatra's simplicity and lightweight but often quickly come to miss a great deal
15
+ of functionality provided by other web frameworks such as Rails when building non-trivial applications.
16
+
17
+ Our goal with this framework is to match the essence of Sinatra and at the same time create a standard library
18
+ of tools, helpers and components that will make Sinatra suitable for more complex applications.
19
+
20
+ Here is a brief overview of functionality provided by the Tennpipes framework:
21
+
22
+ Agnostic:: Full support for many popular testing, templating, mocking, and data storage choices.
23
+ Generators:: Create Tennpipes applications, models, controllers i.e: tennpipes-init project.
24
+ Mountable:: Unlike other ruby frameworks, principally designed for mounting multiple apps.
25
+ Routing:: Full url named routes, named params, before/after filter support.
26
+ Tag Helpers:: View helpers such as: tag, content_tag, input_tag.
27
+ Asset Helpers:: View helpers such as: link_to, image_tag, javascript_include_tag.
28
+ Form Helpers:: Builder support such as: form_tag, form_for, field_set_tag, text_field.
29
+ Text Helpers:: Useful formatting like: time_ago_in_words, js_escape_html, sanitize_html.
30
+ Mailer:: Fast and simple delivery support for sending emails (akin to ActionMailer).
31
+ Admin:: Builtin Admin interface (like Django)
32
+ Logging:: Provide a unified logger that can interact with your ORM or any library.
33
+ Reloading:: Automatically reloads server code during development.
34
+ Localization:: Full support of I18n language localization and can auto-set user's locale.
35
+
36
+ Keep in mind, the user will be able to pull in these components
37
+ {seperately into existing Sinatra applications}[http://www.tennpipesrb.com/guides/standalone-usage-in-sinatra]
38
+ or use them altogether for a comprehensive upgrade to Sinatra (a full-stack Tennpipes application).
39
+
40
+ == Installation
41
+
42
+ To install the tennpipes framework, simply grab the latest version from gemcutter:
43
+
44
+ $ sudo gem install tennpipes
45
+
46
+ This will install the necessary tennpipes gems to get you started.
47
+ Now you are ready to use this gem to enhance your sinatra projects or to create new Tennpipes applications.
48
+
49
+ For a more detailed look at Tennpipes installation,
50
+ check out the {Installation Guide}[http://www.tennpipesrb.com/guides/installation].
51
+
52
+ == Usage
53
+
54
+ Tennpipes is a framework which builds on the existing functionality and Sinatra and provides a variety of
55
+ additional tools and helpers to build upon that foundation. This README and Tennpipes documentation in general will focus
56
+ on the enhancements to the core Sinatra functionality. To use Tennpipes, one should be familiar with the basic
57
+ usage of Sinatra itself.
58
+
59
+ Please check out the
60
+ {Understanding Sinatra}[http://www.tennpipesrb.com/guides/underlying-sinatra-overview] guide
61
+ to learn more about these fundamentals.
62
+
63
+ For information on how to use a specific gem in isolation within an existing Sinatra project, checkout the guide for
64
+ {Using Tennpipes in Sinatra}[http://www.tennpipesrb.com/guides/standalone-usage-in-sinatra].
65
+
66
+ == Getting Started
67
+
68
+ Once a developer understands Sinatra, Tennpipes is quite easy to get comfortable with since Tennpipes is simply a superset
69
+ of existing Sinatra Functionality! Best way to get started with building Tennpipes applications is to read following resources:
70
+
71
+ * {Blog Tutorial}[http://www.tennpipesrb.com/guides/blog-tutorial] - Step-by-step guide to building a blog application with Tennpipes.
72
+ * {Quick Overview}[http://www.tennpipesrb.com/guides/basic-projects] - Outlines basic generation commands.
73
+ * {Tennpipes Examples}[http://www.tennpipesrb.com/guides/examples] - List of known Tennpipes applications which can serve as examples.
74
+
75
+ == Enhanced Base Application (tennpipes-base)
76
+
77
+ Sinatra has support for classes which can be extended to create an application: <tt>Sinatra::Base</tt> and <tt>Sinatra::Application</tt>
78
+ These classes can be extended in order to create a Sinatra web application. These classes provide support for all the basic
79
+ functionality afforded by Sinatra.
80
+
81
+ Tennpipes has support for an enhanced base application class <tt>Tennpipes::Application</tt>. <tt>Tennpipes::Application</tt>
82
+ expands the capabilities of Sinatra::Application and automatically provides the resulting application access to all of
83
+ the tennpipes framework's functionalities.
84
+
85
+ === Simple Application Definition
86
+
87
+ Let us first take a look at the simplest possible Tennpipes application:
88
+
89
+ # app.rb
90
+ TENNPIPES_ROOT = File.dirname(__FILE__) unless defined? TENNPIPES_ROOT
91
+ require 'tennpipes'
92
+ Tennpipes.load!
93
+
94
+ class SimpleApp < Tennpipes::Application
95
+ get '/' do
96
+ 'Hello world'
97
+ end
98
+
99
+ # and for read better we can divide with controllers
100
+ controller '/admin' do
101
+ get '/foo' do
102
+ 'Url is /admin/foo'
103
+ end
104
+ end
105
+ end
106
+
107
+ === Enhanced Route Definitions and Controllers
108
+
109
+ For a complete overview of the Tennpipes routing and controller system,
110
+ check out the {Routing and Controller guide}[http://www.tennpipesrb.com/guides/controllers].
111
+
112
+ Suppose we wanted to add additional routes to our Tennpipes application, and we want to organize the routes
113
+ within a more structured layout. Simply add a <tt>controllers</tt> or <tt>app/controllers</tt> folder and create a file as such:
114
+
115
+ # Simple Example
116
+ SimpleApp.controllers do
117
+ get "/test" do
118
+ "Text to return"
119
+ end
120
+ end
121
+
122
+ You can also do more complex route alias definitions:
123
+
124
+ # app/controllers/example.rb
125
+ SimpleApp.controllers :posts do
126
+ get :index do
127
+ ...
128
+ end
129
+
130
+ get :show, :with => :id do
131
+ # url generated is '/posts/show/:id'
132
+ # access params[:id]
133
+ end
134
+ end
135
+
136
+ as well as mapping the route aliases to an explicit url:
137
+
138
+ # app/controllers/example.rb
139
+ SimpleApp.controllers do
140
+ get :index, :map => '/index' do
141
+ ...
142
+ end
143
+
144
+ get :account, :map => '/the/accounts/:name/and/:id' do
145
+ # access params[:name] and params[:index]
146
+ end
147
+ end
148
+
149
+ and even configure the +provides+ for each route:
150
+
151
+ # app/controllers/example.rb
152
+ SimpleApp.controllers :admin do
153
+ get :show, :with => :id, :provides => :js do
154
+ "Url is /admin/show/#{params[:id]}.#{params[:format]}"
155
+ end
156
+
157
+ get :other, :with => [:id, :name], :provides => [:html, :json] do
158
+ case content_type
159
+ when :js then ... end
160
+ when :json then ... end
161
+ end
162
+ end
163
+ end
164
+
165
+ or auto lookup for current locale or content_type
166
+
167
+ # app/controllers/example.rb
168
+ SimpleApp.controllers :admin do
169
+ get :show, :with => :id, :provides => [:html, :js] do
170
+ render "admin/show"
171
+ end
172
+ end
173
+
174
+ When you visit :+show+ and your I18n.locale == :ru Tennpipes try to look for "admin/show.ru.js.*" if nothing match that path
175
+ they try "admin/show.ru.*" then "admin/show.js.*" if none match return "admin/show.erb" (or other engine i.e. haml)
176
+
177
+ For a complete overview of the routing and controller system, check out the
178
+ {Routing and Controller guide}[http://www.tennpipesrb.com/guides/controllers].
179
+
180
+ === Rendering
181
+
182
+ Unlike Sinatra, Tennpipes supports automatic template lookups such as:
183
+
184
+ # searches for 'account/index.{erb,haml,...}
185
+ render 'account/index'
186
+
187
+ This render does not require any template engine to be specified and will choose the first one that is discovered.
188
+ The existing render function works as well if an engine type should be specified:
189
+
190
+ # example.haml
191
+ render :haml, 'account/index'
192
+
193
+ For a complete overview of the Tennpipes rendering system, check out the
194
+ {Routing and Controller guide}[http://www.tennpipesrb.com/guides/controllers].
195
+
196
+ === Layout
197
+
198
+ With Tennpipes you can (like rails do) use for your custom layout, disable it
199
+
200
+ class SimpleApp < Tennpipes::Application
201
+
202
+ # Disable layouts
203
+ disable layout
204
+
205
+ # Use the layout located in views/layouts/custom.haml
206
+ layout :custom
207
+
208
+ For a complete overview of the layout functionality,
209
+ check out the {Routing and Controller guide}[http://www.tennpipesrb.com/guides/controllers].
210
+
211
+ === Mounting Applications
212
+
213
+ Tennpipes applications are all automatically mountable into other Tennpipes projects. This means that a given Tennpipes
214
+ project directory can easily mount multiple applications. This allows for better organization of complex applications,
215
+ re-usable applications that can be applied (i.e admin, auth, blog) and even more flexibility.
216
+
217
+ You can think of mountable applications as a 'full-featured' merb slice or rails engine. Instead of a separate construct,
218
+ any application can simply be packaged and mounted into another project.
219
+
220
+ Tennpipes stores application mounting information by default within <tt>config/apps.rb</tt>. This file is intended
221
+ to keep all information regarding what applications are mounted to which uri's.
222
+
223
+ For a complete look at mounting applications within a Tennpipes project,
224
+ check out the guide on {Mounting Applications}[http://www.tennpipesrb.com/guides/mounting-applications].
225
+
226
+ === Auto Load Paths
227
+
228
+ Tennpipes also intelligently supports requiring useful files within your application automatically and provides
229
+ functionality for easily splitting up your application into separate files. Tennpipes automatically requires <tt>config/database.rb</tt>
230
+ as a convention for establishing database connection. Also, any files within the <tt>lib</tt> folder will be required
231
+ automatically by Tennpipes.
232
+
233
+ For a complete overview of auto-loaded paths within Tennpipes,
234
+ check out the {Tennpipes Development Guide}[http://www.tennpipesrb.com/guides/development-and-terminal-commands].
235
+
236
+ === Application Logging
237
+
238
+ Tennpipes also supports robust logging capabilities. By default, logging information will
239
+ go to the STDOUT in development (for use in a console) and in an environment-specific log file <tt>log/development.log</tt>
240
+ in test and production environments.
241
+
242
+ To use the logger within a Tennpipes application, simply refer to the <tt>logger</tt> method accessible
243
+ within your app and any controller or views:
244
+
245
+ # controllers/example.rb
246
+ SimpleApp.controllers do
247
+ get("/test") { logger.info "This is a test" }
248
+ end
249
+
250
+ For a complete overview of Tennpipes logger functionality, check out the
251
+ {Tennpipes Development Guide}[http://www.tennpipesrb.com/guides/development-and-terminal-commands].
252
+
253
+ === Development Reloader
254
+
255
+ Tennpipes applications also have the enabled ability to automatically reload all changing application files without
256
+ the need to restart the server. Through the use of a customized Rack middleware, all files on the 'load path'
257
+ are monitored and reloaded whenever changes are applied.
258
+
259
+ This makes rapid development much easier and provides a better alternative to 'shotgun' or 'rerun'
260
+ which requires the application server to be restarted which makes requests take much longer to complete.
261
+
262
+ For a complete overview of code reloading in development,
263
+ check out the {Tennpipes Development Guide}[http://www.tennpipesrb.com/guides/development-and-terminal-commands].
264
+
265
+ === Terminal Commands
266
+
267
+ Tennpipes also comes equipped with multiple useful terminal commands which can be activated to perform
268
+ common tasks such as starting / stopping the application, executing the unit tests or activating an irb session.
269
+
270
+ The following commands are available:
271
+
272
+ # starts the app server (non-daemonized)
273
+ $ tennpipes start
274
+ # starts the app server (daemonized) with given port, environment and adapter
275
+ $ tennpipes start -d -p 3000 -e development -a thin
276
+
277
+ # Stops a daemonized app server
278
+ $ tennpipes stop
279
+
280
+ # Bootup the Tennpipes console (irb)
281
+ $ tennpipes console
282
+
283
+ # Run/List tasks
284
+ $ tennpipes rake
285
+
286
+ You can also create custom rake tasks as well. Using these commands can simplify common tasks
287
+ making development that much smoother.
288
+
289
+ For a complete overview of Tennpipes terminal commands, check out the
290
+ {Tennpipes Commands Guide}[http://www.tennpipesrb.com/guides/development-and-terminal-commands].
291
+
292
+ == Copyright
293
+
294
+ Copyright (c) 2011-2013 Tennpipes. See LICENSE for details.
@@ -0,0 +1 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../gem_rake_helper')
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ require 'bundler/setup' if %w(Gemfile .components).all? { |f| File.exist?(f) }
3
+
4
+ tennpipes_core_path = File.expand_path('../../lib', __FILE__)
5
+ $:.unshift(tennpipes_core_path) if File.directory?(tennpipes_core_path) && !$:.include?(tennpipes_core_path)
6
+
7
+ require 'tennpipes-base/cli/base'
8
+ Tennpipes::Cli::Base.start(ARGV)
@@ -0,0 +1,196 @@
1
+ require 'sinatra/base'
2
+ require 'tennpipes-base/version'
3
+ require 'tennpipes-assist'
4
+ require 'tennpipes-base/application'
5
+
6
+ require 'tennpipes-base/caller'
7
+ require 'tennpipes-base/command'
8
+ require 'tennpipes-base/loader'
9
+ require 'tennpipes-base/logger'
10
+ require 'tennpipes-base/mounter'
11
+ require 'tennpipes-base/reloader'
12
+ require 'tennpipes-base/router'
13
+ require 'tennpipes-base/server'
14
+ require 'tennpipes-base/tasks'
15
+ require 'tennpipes-base/module'
16
+
17
+ if ENV["TENNPIPES_ENV"] || defined?(TENNPIPES_ENV)
18
+ warn 'Environment variable TENNPIPES_ENV is deprecated. Please, use RACK_ENV.'
19
+ ENV["RACK_ENV"] ||= ENV["TENNPIPES_ENV"] ||= TENNPIPES_ENV
20
+ end
21
+ RACK_ENV = ENV["RACK_ENV"] ||= "development" unless defined?(RACK_ENV)
22
+ TENNPIPES_ROOT = ENV["TENNPIPES_ROOT"] ||= File.dirname(Tennpipes.first_caller) unless defined?(TENNPIPES_ROOT)
23
+
24
+ module Tennpipes
25
+ class ApplicationLoadError < RuntimeError # @private
26
+ end
27
+
28
+ extend Loader
29
+
30
+ class << self
31
+ ##
32
+ # Helper method for file references.
33
+ #
34
+ # @param [Array<String>] args
35
+ # The directories to join to {TENNPIPES_ROOT}.
36
+ #
37
+ # @return [String]
38
+ # The absolute path.
39
+ #
40
+ # @example
41
+ # # Referencing a file in config called settings.yml
42
+ # Tennpipes.root("config", "settings.yml")
43
+ # # returns TENNPIPES_ROOT + "/config/setting.yml"
44
+ #
45
+ def root(*args)
46
+ File.expand_path(File.join(TENNPIPES_ROOT, *args))
47
+ end
48
+
49
+ ##
50
+ # Helper method that return {RACK_ENV}.
51
+ #
52
+ # @return [Symbol]
53
+ # The Tennpipes Environment.
54
+ #
55
+ def env
56
+ @_env ||= RACK_ENV.to_s.downcase.to_sym
57
+ end
58
+
59
+ ##
60
+ # The resulting rack builder mapping each 'mounted' application.
61
+ #
62
+ # @return [Tennpipes::Router]
63
+ # The router for the application.
64
+ #
65
+ # @raise [ApplicationLoadError]
66
+ # No applications were mounted.
67
+ #
68
+ def application
69
+ warn 'WARNING! No apps are mounted. Please, mount apps in `config/apps.rb`' unless Tennpipes.mounted_apps.present?
70
+ router = Tennpipes::Router.new
71
+ Tennpipes.mounted_apps.each { |app| app.map_onto(router) }
72
+ middleware.present? ? add_middleware(router) : router
73
+ end
74
+
75
+ ##
76
+ # Configure Global Project Settings for mounted apps. These can be overloaded
77
+ # in each individual app's own personal configuration. This can be used like:
78
+ #
79
+ # @yield []
80
+ # The given block will be called to configure each application.
81
+ #
82
+ # @example
83
+ # Tennpipes.configure_apps do
84
+ # enable :sessions
85
+ # disable :raise_errors
86
+ # end
87
+ #
88
+ def configure_apps(&block)
89
+ return unless block_given?
90
+ global_configurations << block
91
+ end
92
+
93
+ ##
94
+ # Stores global configuration blocks.
95
+ #
96
+ def global_configurations
97
+ @_global_configurations ||= []
98
+ end
99
+
100
+ ##
101
+ # Set +Encoding.default_internal+ and +Encoding.default_external+
102
+ # to +Encoding::UFT_8+.
103
+ #
104
+ # Please note that in +1.9.2+ with some template engines like +haml+
105
+ # you should turn off Encoding.default_internal to prevent problems.
106
+ #
107
+ # @see https://github.com/rtomayko/tilt/issues/75
108
+ #
109
+ # @return [NilClass]
110
+ #
111
+ def set_encoding
112
+ Encoding.default_external = Encoding::UTF_8
113
+ Encoding.default_internal = Encoding::UTF_8
114
+ nil
115
+ end
116
+
117
+ ##
118
+ # Creates Rack stack with the router added to the middleware chain.
119
+ #
120
+ def add_middleware(router)
121
+ builder = Rack::Builder.new
122
+ middleware.each{ |mw,args,block| builder.use(mw, *args, &block) }
123
+ builder.run(router)
124
+ builder.to_app
125
+ end
126
+
127
+ ##
128
+ # A Rack::Builder object that allows to add middlewares in front of all
129
+ # Tennpipes applications.
130
+ #
131
+ # @return [Array<Array<Class, Array, Proc>>]
132
+ # The middleware classes.
133
+ #
134
+ def middleware
135
+ @middleware ||= []
136
+ end
137
+
138
+ ##
139
+ # Clears all previously configured middlewares.
140
+ #
141
+ # @return [Array]
142
+ # An empty array
143
+ #
144
+ def clear_middleware!
145
+ @middleware = []
146
+ end
147
+
148
+ ##
149
+ # Convenience method for adding a Middleware to the whole tennpipes app.
150
+ #
151
+ # @param [Class] m
152
+ # The middleware class.
153
+ #
154
+ # @param [Array] args
155
+ # The arguments for the middleware.
156
+ #
157
+ # @yield []
158
+ # The given block will be passed to the initialized middleware.
159
+ #
160
+ def use(mw, *args, &block)
161
+ middleware << [mw, args, block]
162
+ end
163
+
164
+ ##
165
+ # Registers a gem with tennpipes. This relieves the caller from setting up
166
+ # loadpaths by itself and enables Tennpipes to look up apps in gem folder.
167
+ #
168
+ # The name given has to be the proper gem name as given in the gemspec.
169
+ #
170
+ # @param [String] name
171
+ # The name of the gem being registered.
172
+ #
173
+ # @param [Module] main_module
174
+ # The main module of the gem.
175
+ #
176
+ # @returns The root path of the loaded gem
177
+ def gem(name, main_module)
178
+ _, spec = Gem.loaded_specs.find{|spec_pair| spec_pair[0] == name }
179
+ gems << spec
180
+ modules << main_module
181
+ spec.full_gem_path
182
+ end
183
+
184
+ ##
185
+ # @returns [Gem::Specification]
186
+ def gems
187
+ @gems ||= []
188
+ end
189
+
190
+ ##
191
+ # @returns [<Tennpipes::Module>]
192
+ def modules
193
+ @modules ||= []
194
+ end
195
+ end
196
+ end