Module: Merb
- Defined in:
- merb-core/lib/merb-core/logger.rb,
merb-core/lib/merb-core.rb,
merb-core/lib/merb-core/rack.rb,
merb-core/lib/merb-core/test.rb,
merb-core/lib/merb-core/config.rb,
merb-core/lib/merb-core/server.rb,
merb-core/lib/merb-core/plugins.rb,
merb-core/lib/merb-core/version.rb,
merb-core/lib/merb-core/autoload.rb,
merb-core/lib/merb-core/autoload.rb,
merb-core/lib/merb-core/constants.rb,
merb-core/lib/merb-core/bootloader.rb,
merb-core/lib/merb-core/rack/adapter.rb,
merb-core/lib/merb-core/rack/helpers.rb,
merb-core/lib/merb-core/test/run_specs.rb,
merb-core/lib/merb-core/dispatch/router.rb,
merb-core/lib/merb-core/rack/middleware.rb,
merb-core/lib/merb-core/dispatch/worker.rb,
merb-core/lib/merb-core/controller/mime.rb,
merb-core/lib/merb-core/rack/adapter/irb.rb,
merb-core/lib/merb-core/rack/adapter/ebb.rb,
merb-core/lib/merb-core/dispatch/cookies.rb,
merb-core/lib/merb-core/dispatch/request.rb,
merb-core/lib/merb-core/rack/application.rb,
merb-core/lib/merb-core/dispatch/session.rb,
merb-core/lib/merb-core/rack/adapter/thin.rb,
merb-core/lib/merb-core/rack/mock_session.rb,
merb-core/lib/merb-core/rack/adapter/fcgi.rb,
merb-core/lib/merb-core/dispatch/dispatcher.rb,
merb-core/lib/merb-core/test/test_ext/rspec.rb,
merb-core/lib/merb-core/rack/adapter/runner.rb,
merb-core/lib/merb-core/rack/stream_wrapper.rb,
merb-core/lib/merb-core/rack/middleware/head.rb,
merb-core/lib/merb-core/rack/adapter/mongrel.rb,
merb-core/lib/merb-core/rack/adapter/webrick.rb,
merb-core/lib/merb-core/controller/exceptions.rb,
merb-core/lib/merb-core/dispatch/router/route.rb,
merb-core/lib/merb-core/rack/adapter/abstract.rb,
merb-core/lib/merb-core/rack/adapter/abstract.rb,
merb-core/lib/merb-core/rack/middleware/tracer.rb,
merb-core/lib/merb-core/rack/middleware/static.rb,
merb-core/lib/merb-core/test/helpers/cookie_jar.rb,
merb-core/lib/merb-core/rack/adapter/thin_turbo.rb,
merb-core/lib/merb-core/dispatch/session/memory.rb,
merb-core/lib/merb-core/dispatch/session/cookie.rb,
merb-core/lib/merb-core/rack/middleware/profiler.rb,
merb-core/lib/merb-core/dispatch/request_parsers.rb,
merb-core/lib/merb-core/dispatch/router/behavior.rb,
merb-core/lib/merb-core/test/helpers/route_helper.rb,
merb-core/lib/merb-core/dispatch/router/resources.rb,
merb-core/lib/merb-core/dispatch/session/container.rb,
merb-core/lib/merb-core/test/helpers/webrat_helper.rb,
merb-core/lib/merb-core/dispatch/session/memcached.rb,
merb-core/lib/merb-core/test/helpers/request_helper.rb,
merb-core/lib/merb-core/controller/mixins/responder.rb,
merb-core/lib/merb-core/dispatch/router/cached_proc.rb,
merb-core/lib/merb-core/rack/middleware/path_prefix.rb,
merb-core/lib/merb-core/rack/adapter/evented_mongrel.rb,
merb-core/lib/merb-core/controller/mixins/controller.rb,
merb-core/lib/merb-core/controller/abstract_controller.rb,
merb-core/lib/merb-core/rack/middleware/content_length.rb,
merb-core/lib/merb-core/test/helpers/controller_helper.rb,
merb-core/lib/merb-core/rack/middleware/conditional_get.rb,
merb-core/lib/merb-core/rack/adapter/swiftiplied_mongrel.rb,
merb-core/lib/merb-core/test/helpers/mock_request_helper.rb,
merb-core/lib/merb-core/dispatch/session/store_container.rb,
merb-action-args/lib/merb-action-args/version.rb,
merb-assets/lib/merb-assets/assets.rb,
merb-assets/lib/merb-assets/version.rb,
merb-assets/lib/merb-assets/assets_mixin.rb,
merb-slices/lib/merb-slices/module.rb,
merb-slices/lib/merb-slices/version.rb,
merb-slices/lib/merb-slices/module_mixin.rb,
merb-slices/lib/merb-slices/controller_mixin.rb,
merb-cache/lib/merb-cache/cache.rb,
merb-cache/lib/merb-cache/version.rb,
merb-cache/lib/merb-cache/cache_request.rb,
merb-gen/lib/merb-gen/version.rb,
merb-gen/lib/merb-gen/generator.rb,
merb-gen/lib/merb-gen/app_generator.rb,
merb-gen/lib/merb-gen/named_generator.rb,
merb-gen/lib/merb-gen/namespaced_generator.rb,
merb-haml/lib/merb-haml/version.rb,
merb-helpers/lib/merb-helpers.rb,
merb-helpers/lib/merb-helpers/version.rb,
merb-helpers/lib/merb-helpers/tag_helpers.rb,
merb-helpers/lib/merb-helpers/date_time_helpers.rb,
merb-mailer/lib/merb-mailer/mailer.rb,
merb-mailer/lib/merb-mailer/version.rb,
merb-mailer/lib/merb-mailer/mailer_mixin.rb,
merb-mailer/lib/merb-mailer/mail_controller.rb,
merb-param-protection/lib/merb-param-protection.rb,
merb-param-protection/lib/merb-param-protection/version.rb,
merb-exceptions/lib/merb-exceptions/version.rb,
merb-core/lib/merb-core/rack/handler/mongrel.rb
Overview
Why do we use Underscores?
In Merb, views are actually methods on controllers. This provides not-insignificant speed benefits, as well as preventing us from needing to copy over instance variables, which we think is proof that everything belongs in one class to begin with.
Unfortunately, this means that view helpers need to be included into the Controller class. To avoid causing confusion when your helpers potentially conflict with our instance methods, we use an _ to disambiguate. As long as you don't begin your helper methods with _, you only need to worry about conflicts with Merb methods that are part of the public API.
Filters
#before is a class method that allows you to specify before filters in
your controllers. Filters can either be a symbol or string that
corresponds to a method name to call, or a proc object. if it is a method
name that method will be called and if it is a proc it will be called
with an argument of self where self is the current controller object.
When you use a proc as a filter it needs to take one parameter.
#after is identical, but the filters are run after the action is invoked.
Examples
before :some_filter
before :authenticate, :exclude => [:login, :signup]
before :has_role, :with => ["Admin"], :exclude => [:index, :show]
before Proc.new { some_method }, :only => :foo
before :authorize, :unless => :logged_in?
You can use either :only => :actionname or :exclude => [:this, :that],
but not both at once. :only will only run before the listed actions and
:exclude will run for every action that is not listed.
Merb's before filter chain is very flexible. To halt the filter chain you
use throw :halt. If throw is called with only one
argument of :halt the return value of the method
filters_halted will be what is rendered to the view. You can
override filters_halted in your own controllers to control what
it outputs. But the throw construct is much more powerful than
just that.
throw :halt can also take a second argument. Here is what that
second argument can be and the behavior each type can have:
String: when the second argument is a string then that string will be what is rendered to the browser. Since merb's#rendermethod returns a string you can render a template or just use a plain string:throw :halt, "You don't have permissions to do that!" throw :halt, render(:action => :access_denied)
Symbol: If the second arg is a symbol, then the method named after that symbol will be calledthrow :halt, :must_click_disclaimer
Proc: If the second arg is a Proc, it will be called and its return value will be what is rendered to the browser:throw :halt, proc { access_denied } throw :halt, proc { Tidy.new(c.index) }
Filter Options (.before, .after, .add_filter, .if, .unless)
:only<Symbol, Array[Symbol]>:
A list of actions that this filter should apply to
:exclude<Symbol, Array[Symbol]>:
A list of actions that this filter should not apply to
:if<Symbol, Proc>:
Only apply the filter if the method named after the symbol or calling the proc evaluates to true
:unless<Symbol, Proc>:
Only apply the filter if the method named after the symbol or calling the proc evaluates to false
:with<Array[Object]>:
Arguments to be passed to the filter. Since we are talking method/proc calls,
filter method or Proc should to have the same arity
as number of elements in Array you pass to this option.
Types (shortcuts for use in this file)
Filter:: <Array[Symbol, (Symbol, String, Proc)]>
params[:action] and params[:controller] deprecated
params[:action] and params[:controller] have been deprecated as of
the 0.9.0 release. They are no longer set during dispatch, and
have been replaced by action_name and controller_name respectively.
Defined Under Namespace
Modules: Assets, AssetsMixin, AuthenticationMixin, Cache, ColorfulMessages, ConditionalGetMixin, Const, ControllerExceptions, ControllerMixin, CookiesMixin, Generators, GlobalHelpers, Helpers, InlineTemplates, MailerMixin, MemcacheStore, ParamProtection, ParamsFilter, Parse, Plugins, Rack, RenderMixin, ResponderMixin, Session, SessionMixin, Slices, System, Template, Test Classes: AbstractController, AcceptType, ActionArgs, BootLoader, Config, Controller, CookieSession, Cookies, Counter, Dispatcher, Exceptions, Haml, Logger, MailController, Mailer, MemcacheSession, MemorySession, MemorySessionStore, Request, ReservedError, Responder, Router, Server, SessionContainer, SessionStoreContainer, Worker
Constant Summary
- VERSION =
'1.2.0'.freeze
Class Attribute Summary (collapse)
- + (Object) adapter
- + (Object) environment (also: env)
- + (Object) environment_info private
-
+ (Object) exiting
Returns the value of attribute exiting.
-
+ (Object) klass_hashes
Set up default variables under Merb.
- + (Object) load_paths private
-
+ (Symbol) orm
Returns the default ORM for this application.
- + (Object) started (also: started?) private
-
+ (Symbol) template_engine
Returns the default template engine for this application.
-
+ (Symbol) test_framework
Returns the default test framework for this application.
Class Method Summary (collapse)
-
+ (Object) _root
private
Root path retrieval.
-
+ (Object) add_generators(*generators)
Add Generator load paths for plugin authors.
-
+ (nil) add_mime_type(key, transform_method, mimes, new_response_headers = {}, default_quality = 1, &block)
Any specific outgoing headers should be included here.
-
+ (Object) add_rakefiles(*rakefiles)
Add Rakefiles load path for plugins authors.
-
+ (Array) at_exit(&blk)
Register a proc to run when Merb is exiting gracefully.
-
+ (Array) at_exit_procs
private
List procs that are called on exit.
-
+ (Hash<String => Symbol>) available_accepts
A hash mapping Content-Type values to the mime type key of the appropriate entry in
#available_mime_types. -
+ (Hash<Symbol => Hash<Symbol => Object>>) available_mime_types
Returns a hash of the available mime types.
-
+ (Boolean) bundled?
Check if Merb is running as an application with bundled gems.
-
+ (Object) cache
A convinient way to get at Merb::Cache.
-
+ (Hash) config(&block)
If block was given configures using the block.
-
+ (RegExp) deferred_actions
Get the regular expression against which deferred actions are matched by Rack application handler.
-
+ (String) dir_for(type)
Get the directory for a given type.
-
+ (Object) disable(*components)
Disables the given core components, like a Gem for example.
-
+ (Boolean) disabled?(*components)
True if all components (or just one) are disabled.
-
+ (Array) disabled_components
All components that have been disabled.
-
+ (Object) disabled_components=(components)
Set disabled components.
-
+ (Boolean) env?(env)
Check if Merb is running in a specified environment.
-
+ (Object) exception(e)
Required to show exceptions in the log file.
-
+ (Object) fatal!(str, e = nil)
private
Perform a hard Exit.
- + (Boolean) forking_environment?
-
+ (String) framework_root(*args)
Get the path of root directory of the Merb framework.
-
+ (Array(String)) generators
Find out what paths generators are loaded from.
-
+ (String) glob_for(type)
Get the path glob for a given type.
-
+ (Object) load_config(options = {})
Load configuration and assign the logger.
-
+ (Object) load_dependencies(options = {})
Load all basic dependencies (selected BootLoaders only).
-
+ (String) log_path
Get the path to the directory containing the current log file.
-
+ (IO) log_stream(port = "main")
Get the IO object used for logging.
-
+ (Object) logger
Return the Merb Logger object for the current thread.
-
+ (Object) merge_env(env, use_db = false)
Merge environment settings.
-
+ (Symbol) mime_transform_method(key)
private
The transform method for the mime type, e.g.
- + (Boolean) on_jruby?
- + (Boolean) on_windows?
- + (Object) orm_generator_scope deprecated Deprecated.
-
+ (Object) print_colorized_backtrace(e)
private
Print a colorized backtrace to the merb logger.
-
+ (Object) push_path(type, path, file_glob = "**/*.rb")
Set up your application layout.
-
+ (Array(String)) rakefiles
Find out what paths Rakefiles are loaded from.
-
+ (Object) reload
Reload application and framework classes.
-
+ (Boolean, Hash<Symbol => Object>) remove_mime_type(key)
Removes a MIME-type from the mime-type list.
-
+ (Object) remove_paths(*args)
Removes given types of application components from load path Merb uses for autoloading.
-
+ (Object) reset_logger!
Removes the logger for the current thread (nil).
-
+ (String) root(*path)
Expand paths relative to merb's root path.
-
+ (Object) root=(value)
Set the Merb root path.
- + (Object) run_later(&blk)
- + (Boolean) running_irb? private
-
+ (Object) start(argv = ARGV)
Start Merb by setting up the Config and then starting the server.
-
+ (Object) start_environment(argv = ARGV)
Start the Merb environment, but only if it hasn't been loaded yet.
- + (Object) test_framework_generator_scope deprecated Deprecated.
-
+ (Boolean) testing?
Check if running in a testing environment.
-
+ (Object) trap(signal, &block)
Install a signal handler for a given signal unless signals have been disabled with Merb.disable(:signals).
-
+ (Boolean) verbose_logging?
Check if verbose logging is enabled.
Class Attribute Details
+ (Object) adapter
175 176 177 |
# File 'merb-core/lib/merb-core.rb', line 175 def adapter @adapter end |
+ (Object) environment Also known as: env
175 176 177 |
# File 'merb-core/lib/merb-core.rb', line 175 def environment @environment end |
+ (Object) environment_info
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
177 178 179 |
# File 'merb-core/lib/merb-core.rb', line 177 def environment_info @environment_info end |
+ (Object) exiting
Returns the value of attribute exiting
35 36 37 |
# File 'merb-core/lib/merb-core.rb', line 35 def exiting @exiting end |
+ (Object) klass_hashes
Set up default variables under Merb
460 461 462 |
# File 'merb-core/lib/merb-core.rb', line 460 def klass_hashes @klass_hashes end |
+ (Object) load_paths
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
177 178 179 |
# File 'merb-core/lib/merb-core.rb', line 177 def load_paths @load_paths end |
+ (Symbol) orm
Returns the default ORM for this application. For instance, :datamapper.
460 461 462 |
# File 'merb-core/lib/merb-core.rb', line 460 def orm @orm end |
+ (Object) started Also known as: started?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
177 178 179 |
# File 'merb-core/lib/merb-core.rb', line 177 def started @started end |
+ (Symbol) template_engine
Returns the default template engine for this application. For instance :haml.
460 461 462 |
# File 'merb-core/lib/merb-core.rb', line 460 def template_engine @template_engine end |
+ (Symbol) test_framework
Returns the default test framework for this application. For instance :rspec.
460 461 462 |
# File 'merb-core/lib/merb-core.rb', line 460 def test_framework @test_framework end |
Class Method Details
+ (Object) _root
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Root path retrieval
283 284 285 |
# File 'merb-core/lib/merb-core.rb', line 283 def _root Merb::Config[:merb_root] || File.(Dir.pwd) end |
+ (Object) add_generators(*generators)
Add Generator load paths for plugin authors.
707 708 709 710 |
# File 'merb-core/lib/merb-core.rb', line 707 def add_generators(*generators) @generators ||= [] @generators += generators end |
+ (nil) add_mime_type(key, transform_method, mimes, new_response_headers = {}, default_quality = 1, &block)
Any specific outgoing headers should be included here. These are not
the content-type header but anything in addition to it.
transform_method should be set to a symbol of the method used to
transform a resource into this mime type.
For example for the :xml mime type an object might be transformed by
calling :to_xml, or for the :js mime type, :to_json.
If there is no transform method, use nil.
Autogenerated Methods
Adding a mime-type adds a render_type method that sets the content type and calls render.
By default this does: def render_all def render_yaml def render_text def render_html def render_xml def render_js def render_yaml
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'merb-core/lib/merb-core/controller/mime.rb', line 76 def add_mime_type(key, transform_method, mimes, new_response_headers = {}, default_quality = 1, &block) enforce!(key => Symbol, mimes => Array) content_type = new_response_headers["Content-Type"] || mimes.first if charset = new_response_headers.delete(:charset) content_type += "; charset=#{charset}" end ResponderMixin::TYPES.update(key => {:accepts => mimes, :transform_method => transform_method, :content_type => content_type, :response_headers => new_response_headers, :default_quality => default_quality, :response_block => block }) mimes.each do |mime| ResponderMixin::MIMES.update(mime => key) end Merb::RenderMixin.class_eval <<-EOS, __FILE__, __LINE__ def render_#{key}(thing = nil, opts = {}) self.content_type = :#{key} render thing, opts end EOS nil end |
+ (Object) add_rakefiles(*rakefiles)
Add Rakefiles load path for plugins authors.
696 697 698 699 |
# File 'merb-core/lib/merb-core.rb', line 696 def add_rakefiles(*rakefiles) @rakefiles ||= [] @rakefiles += rakefiles end |
+ (Array) at_exit(&blk)
Register a proc to run when Merb is exiting gracefully. It will not be run when Merb exits quickly.
76 77 78 |
# File 'merb-core/lib/merb-core.rb', line 76 def at_exit(&blk) self.at_exit_procs << blk end |
+ (Array) at_exit_procs
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
List procs that are called on exit.
43 44 45 |
# File 'merb-core/lib/merb-core.rb', line 43 def at_exit_procs @at_exit_procs ||= [] end |
+ (Hash<String => Symbol>) available_accepts
A hash mapping Content-Type values to
the mime type key of the appropriate entry in #available_mime_types
32 33 34 |
# File 'merb-core/lib/merb-core/controller/mime.rb', line 32 def available_accepts ResponderMixin::MIMES end |
+ (Hash<Symbol => Hash<Symbol => Object>>) available_mime_types
Returns a hash of the available mime types.
Each entry corresponds to a call to add_mime_type, having the mime
type key (:html, :xml, :json, etc.) as the key and a hash
containing the following entries:
:accepts: Types that will be recognized by this entry- **
:transform_method:** The method called on an object to convert it to content of this type (such asto_json) :content_type: The value set to the "Content-Type" HTTP header when this mime is sent in a response:response_headers: Sent in a response using this content type:default_quality: The scale factor used in describing content type preference:response_block: The block to be called with the controller when a request responds to this mime type
24 25 26 |
# File 'merb-core/lib/merb-core/controller/mime.rb', line 24 def available_mime_types ResponderMixin::TYPES end |
+ (Boolean) bundled?
Check if Merb is running as an application with bundled gems.
Notes
Bundling required gems makes your application independent from the environment it runs in. It is a good practice to freeze application framework and gems and is very useful when application is run in some sort of sandbox, for instance, shared hosting with preconfigured gems.
515 516 517 |
# File 'merb-core/lib/merb-core.rb', line 515 def bundled? $BUNDLE || ENV.key?("BUNDLE") end |
+ (Object) cache
A convinient way to get at Merb::Cache
3 4 5 |
# File 'merb-cache/lib/merb-cache/cache.rb', line 3 def self.cache Merb::Cache end |
+ (Hash) config(&block)
If block was given configures using the block.
Merb.config do
beer "good"
hashish :foo => "bar"
environment "development"
log_level "debug"
use_mutex false
exception_details true
reload_classes true
reload_time 0.5
end
Notes
See Merb::GlobalHelpers.load_config for configuration options list.
633 634 635 636 |
# File 'merb-core/lib/merb-core.rb', line 633 def config(&block) Merb::Config.configure(&block) if block_given? Config end |
+ (RegExp) deferred_actions
Get the regular expression against which deferred actions are matched by Rack application handler.
Notes
Concatenates :deferred_actions configuration option values.
411 412 413 414 415 416 417 418 419 |
# File 'merb-core/lib/merb-core.rb', line 411 def deferred_actions @deferred ||= begin if Merb::Config[:deferred_actions].empty? /^\0$/ else /#{Merb::Config[:deferred_actions].join("|")}/ end end end |
+ (String) dir_for(type)
Get the directory for a given type
265 266 267 |
# File 'merb-core/lib/merb-core.rb', line 265 def dir_for(type) Merb.load_paths[type].first end |
+ (Object) disable(*components)
Disables the given core components, like a Gem for example.
643 644 645 |
# File 'merb-core/lib/merb-core.rb', line 643 def disable(*components) disabled_components.push(*components) end |
+ (Boolean) disabled?(*components)
Is the param description right? (Type)
True if all components (or just one) are disabled.
668 669 670 |
# File 'merb-core/lib/merb-core.rb', line 668 def disabled?(*components) components.all? { |c| disabled_components.include?(c) } end |
+ (Array) disabled_components
All components that have been disabled.
659 660 661 |
# File 'merb-core/lib/merb-core.rb', line 659 def disabled_components Merb::Config[:disabled_components] ||= [] end |
+ (Object) disabled_components=(components)
Set disabled components.
652 653 654 |
# File 'merb-core/lib/merb-core.rb', line 652 def disabled_components=(components) disabled_components.replace components end |
+ (Boolean) env?(env)
Check if Merb is running in a specified environment.
Merb.env #=> production
Merb.env?(:production) #=> true
Merb.env?(:development) #=> false
607 608 609 |
# File 'merb-core/lib/merb-core.rb', line 607 def env?(env) Merb.env == env.to_s end |
+ (Object) exception(e)
Required to show exceptions in the log file
335 336 337 338 |
# File 'merb-core/lib/merb-core/controller/exceptions.rb', line 335 def self.exception(e) "#{ e. } - (#{ e.class })\n" << "#{(e.backtrace or []).join("\n")}" end |
+ (Object) fatal!(str, e = nil)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Perform a hard Exit.
Print a backtrace to the merb logger before exiting if verbose is enabled.
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
# File 'merb-core/lib/merb-core.rb', line 426 def fatal!(str, e = nil) Merb::Config[:log_stream] = STDOUT if STDOUT.tty? Merb.reset_logger! Merb.logger.fatal! Merb.logger.fatal!("\e[1;31;47mFATAL: #{str}\e[0m") Merb.logger.fatal! print_colorized_backtrace(e) if e && Merb::Config[:verbose] if Merb::Config[:show_ugly_backtraces] raise e else exit(1) end end |
+ (Boolean) forking_environment?
726 727 728 |
# File 'merb-core/lib/merb-core.rb', line 726 def forking_environment? !on_windows? && !on_jruby? end |
+ (String) framework_root(*args)
Get the path of root directory of the Merb framework.
392 393 394 395 396 397 398 399 400 |
# File 'merb-core/lib/merb-core.rb', line 392 def framework_root(*args) @framework_root ||= File.dirname(__FILE__) if args.empty? @framework_root else File.join(@framework_root, *args) end end |
+ (Array(String)) generators
Find out what paths generators are loaded from.
686 687 688 |
# File 'merb-core/lib/merb-core.rb', line 686 def generators @generators ||= [] end |
+ (String) glob_for(type)
Get the path glob for a given type.
276 277 278 |
# File 'merb-core/lib/merb-core.rb', line 276 def glob_for(type) Merb.load_paths[type][1] end |
+ (Object) load_config(options = {})
Load configuration and assign the logger.
Some of these options come from command line on Merb application start, some of them are set in Merb init file or environment-specific.
559 560 561 562 |
# File 'merb-core/lib/merb-core.rb', line 559 def load_config( = {}) Merb::Config.setup(Merb::Config.defaults.merge()) Merb::BootLoader::Logger.run end |
+ (Object) load_dependencies(options = {})
Load all basic dependencies (selected BootLoaders only). This sets up Merb framework component paths (directories for models, controllers, etc) using framework.rb or default layout, loads init file and dependencies specified in it and runs before_app_loads hooks.
573 574 575 576 577 578 |
# File 'merb-core/lib/merb-core.rb', line 573 def load_dependencies( = {}) load_config() Merb::BootLoader::BuildFramework.run Merb::BootLoader::Dependencies.run Merb::BootLoader::BeforeAppLoads.run end |
+ (String) log_path
Get the path to the directory containing the current log file.
377 378 379 380 381 382 |
# File 'merb-core/lib/merb-core.rb', line 377 def log_path case Merb::Config[:log_file] when String then File.dirname(Merb::Config[:log_file]) else Merb.root("log") end end |
+ (IO) log_stream(port = "main")
Get the IO object used for logging.
If this Merb instance is not running as a daemon or with forced logging to file, this will return +STDOUT+.
Notes
When Merb.testing? the port is modified to become :test - this keeps this special environment situation from ending up in the memoized @streams just once, thereby never taking changes into account again. Now, it will be memoized as :test - and just logging to merb_test.log.
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'merb-core/lib/merb-core.rb', line 345 def log_stream(port = "main") port = :test if Merb.testing? @streams ||= {} @streams[port] ||= begin log = if Merb.testing? log_path / "merb_test.log" elsif !Merb::Config[:daemonize] && !Merb::Config[:force_logging] STDOUT else log_path / "merb.#{port}.log" end if log.is_a?(IO) stream = log elsif File.exist?(log) stream = File.open(log, (File::WRONLY | File::APPEND)) else FileUtils.mkdir_p(File.dirname(log)) stream = File.open(log, (File::WRONLY | File::APPEND | File::CREAT)) stream.write("#{Time.now.httpdate} #{Merb::Config[:log_delimiter]} " \ "info #{Merb::Config[:log_delimiter]} Logfile created\n") end stream.sync = true stream end end |
+ (Object) logger
Return the Merb Logger object for the current thread. Set it up if it does not exist.
320 321 322 |
# File 'merb-core/lib/merb-core.rb', line 320 def logger Thread.current[:merb_logger] ||= Merb::Logger.new end |
+ (Object) merge_env(env, use_db = false)
Merge environment settings.
This can allow you to, e.g., have a "localdev" environment that runs like your "development", or a "staging" environment that runs identical to your "production" environment. It is best used from any environment config file, e.g., development.rb, custom.rb, localdev.rb, staging.rb, etc.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'merb-core/lib/merb-core.rb', line 100 def merge_env(env,use_db=false) if Merb.environment_info.nil? Merb.environment_info = { :real_env => Merb.environment, :merged_envs => [], :db_env => Merb.environment } end #Only load if it hasn't been loaded unless Merb.environment_info[:merged_envs].member? env Merb.environment_info[:merged_envs] << env env_file = Merb.dir_for(:config) / "environments" / ("#{env}.rb") if File.exists?(env_file) load(env_file) else Merb.logger.warn! "Environment file does not exist! #{env_file}" end end # Mark specific environment to load when ORM loads, # if multiple environments are loaded, the last one # with use_db as TRUE will be loaded if use_db Merb.environment_info[:db_env] = env end end |
+ (Symbol) mime_transform_method(key)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The transform method for the mime type, e.g. :to_json.
131 132 133 134 |
# File 'merb-core/lib/merb-core/controller/mime.rb', line 131 def mime_transform_method(key) raise ArgumentError, ":#{key} is not a valid MIME-type" unless ResponderMixin::TYPES.key?(key) ResponderMixin::TYPES[key][:transform_method] end |
+ (Boolean) on_jruby?
731 732 733 |
# File 'merb-core/lib/merb-core.rb', line 731 def on_jruby? RUBY_PLATFORM =~ Merb::Const::JAVA_PLATFORM_REGEXP end |
+ (Boolean) on_windows?
736 737 738 |
# File 'merb-core/lib/merb-core.rb', line 736 def on_windows? RUBY_PLATFORM =~ Merb::Const::WIN_PLATFORM_REGEXP end |
+ (Object) orm_generator_scope
472 473 474 475 476 |
# File 'merb-core/lib/merb-core.rb', line 472 def orm_generator_scope Merb.logger.warn!("WARNING: Merb.orm_generator_scope is deprecated!") return :merb_default if Merb.orm == :none Merb.orm end |
+ (Object) print_colorized_backtrace(e)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Print a colorized backtrace to the merb logger.
446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'merb-core/lib/merb-core.rb', line 446 def print_colorized_backtrace(e) e.backtrace.map! do |line| line.gsub(/^#{Merb.framework_root}/, "\e[34mFRAMEWORK_ROOT\e[31m") end Merb.logger.fatal! "\e[34mFRAMEWORK_ROOT\e[0m = #{Merb.framework_root}" Merb.logger.fatal! Merb.logger.fatal! "\e[31m#{e.class}: \e[1;31;47m#{e.}\e[0m" e.backtrace.each do |line| Merb.logger.fatal! "\e[31m#{line}\e[0m" end end |
+ (Object) push_path(type, path, file_glob = "**/*.rb")
Set up your application layout.
There are three application layouts in Merb:
Regular app/:type layout of Ruby on Rails fame:
app/modelsfor modelsapp/mailersfor mailers (special type of controllers)app/partsfor parts, Merb componentsapp/viewsfor templatesapp/controllersfor controllerlibfor libraries
Flat application layout:
application.rbfor models, controllers, mailers, etcconfig/init.rbfor initialization and router configurationconfig/framework.rbfor framework and dependencies configurationviewsfor views
Camping-style "very flat" application layout, where the whole Merb application and configs are contained within a single file.
Autoloading for lib uses an empty glob by default. If you want to have your libraries under lib use autoload, add the following to Merb init file:
Merb.push_path(:lib, Merb.root / "lib", "**/*.rb") # glob set explicity.
Then lib/magicwand/lib/magicwand.rb with MagicWand module will
be autoloaded when you first access that constant.
234 235 236 237 |
# File 'merb-core/lib/merb-core.rb', line 234 def push_path(type, path, file_glob = "**/*.rb") enforce!(type => Symbol) load_paths[type] = [path, file_glob] end |
+ (Array(String)) rakefiles
Find out what paths Rakefiles are loaded from.
677 678 679 |
# File 'merb-core/lib/merb-core.rb', line 677 def rakefiles @rakefiles ||= [] end |
+ (Object) reload
Reload application and framework classes. See Merb::BootLoader::ReloadClasses for details.
584 585 586 |
# File 'merb-core/lib/merb-core.rb', line 584 def reload Merb::BootLoader::ReloadClasses.reload end |
+ (Boolean, Hash<Symbol => Object>) remove_mime_type(key)
Removes a MIME-type from the mime-type list.
Notes
:all is the key for /; It can't be removed.
119 120 121 122 |
# File 'merb-core/lib/merb-core/controller/mime.rb', line 119 def remove_mime_type(key) return false if key == :all ResponderMixin::TYPES.delete(key) end |
+ (Object) remove_paths(*args)
Removes given types of application components from load path Merb uses for autoloading.
This can for example be used to make Merb use app/models for mailers just like Ruby on Rails does, when used together with Merb::GlobalHelpers.push_path, by making your Merb application use legacy Rails application components:
Merb.root = "path/to/legacy/app/root"
Merb.remove_paths(:mailer)
Merb.push_path(:mailer, Merb.root / "app" / "models", "**/*.rb")
253 254 255 |
# File 'merb-core/lib/merb-core.rb', line 253 def remove_paths(*args) args.each {|arg| load_paths.delete(arg)} end |
+ (Object) reset_logger!
Removes the logger for the current thread (nil).
327 328 329 |
# File 'merb-core/lib/merb-core.rb', line 327 def reset_logger! Thread.current[:merb_logger] = nil end |
+ (String) root(*path)
Expand paths relative to merb's root path.
Given a relative path or a list of path components, returns an absolute path within the application.
Merb.root = "/home/merb/app"
Merb.root # => "/home/merb/app"
Merb.root("images") # => "/home/merb/app/images"
Merb.root("views", "admin") # => "/home/merb/app/views/admin"
303 304 305 |
# File 'merb-core/lib/merb-core.rb', line 303 def root(*path) File.join(_root, *path) end |
+ (Object) root=(value)
Set the Merb root path.
312 313 314 |
# File 'merb-core/lib/merb-core.rb', line 312 def root=(value) Merb::Config[:merb_root] = value end |
+ (Object) run_later(&blk)
740 741 742 |
# File 'merb-core/lib/merb-core.rb', line 740 def run_later(&blk) Merb::Dispatcher.work_queue << blk end |
+ (Boolean) running_irb?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
745 746 747 |
# File 'merb-core/lib/merb-core.rb', line 745 def running_irb? @running_irb end |
+ (Object) start(argv = ARGV)
Start Merb by setting up the Config and then starting the server. Set the Merb application environment and the root path.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'merb-core/lib/merb-core.rb', line 135 def start(argv = ARGV) Merb::Config[:original_log_stream] = Merb::Config[:log_stream] Merb::Config[:log_stream] ||= STDOUT if Hash === argv Merb::Config.setup(argv) elsif !argv.nil? Merb::Config.parse_args(argv) end # Keep information that we run inside IRB to guard it against overriding in init.rb @running_irb = Merb::Config[:adapter] == 'irb' Merb::Config[:log_stream] = STDOUT Merb.environment = Merb::Config[:environment] Merb.root = Merb::Config[:merb_root] case Merb::Config[:action] when :kill Merb::Server.kill(Merb::Config[:port], 2) when :kill_9 Merb::Server.kill(Merb::Config[:port], 9) when :fast_deploy Merb::Server.kill("main", "HUP") else Merb::Server.start(Merb::Config[:port], Merb::Config[:cluster]) @started = true end end |
+ (Object) start_environment(argv = ARGV)
Start the Merb environment, but only if it hasn't been loaded yet.
170 171 172 |
# File 'merb-core/lib/merb-core.rb', line 170 def start_environment(argv=ARGV) start(argv) unless (@started ||= false) end |
+ (Object) test_framework_generator_scope
488 489 490 491 |
# File 'merb-core/lib/merb-core.rb', line 488 def test_framework_generator_scope Merb.logger.warn!("WARNING: Merb.test_framework_generator_scope is deprecated") Merb.test_framework end |
+ (Boolean) testing?
Check if running in a testing environment.
594 595 596 |
# File 'merb-core/lib/merb-core.rb', line 594 def testing? $TESTING ||= env?(:test) || Merb::Config[:testing] end |
+ (Object) trap(signal, &block)
Install a signal handler for a given signal unless signals have been disabled with Merb.disable(:signals)
719 720 721 722 723 |
# File 'merb-core/lib/merb-core.rb', line 719 def trap(signal, &block) if Signal.list.include?(signal) Kernel.trap(signal, &block) unless Merb.disabled?(:signals) end end |
+ (Boolean) verbose_logging?
Check if verbose logging is enabled.
524 525 526 |
# File 'merb-core/lib/merb-core.rb', line 524 def verbose_logging? (ENV['DEBUG'] || $DEBUG || Merb::Config[:verbose]) && Merb.logger end |