Module: Merb

Defined in:
lib/merb-core/logger.rb,
lib/merb-core.rb,
lib/merb-core/rack.rb,
lib/merb-core/test.rb,
lib/merb-core/config.rb,
lib/merb-core/server.rb,
lib/merb-core/plugins.rb,
lib/merb-core/version.rb,
lib/merb-core/autoload.rb,
lib/merb-core/autoload.rb,
lib/merb-core/constants.rb,
lib/merb-core/bootloader.rb,
lib/merb-core/core_ext/set.rb,
lib/merb-core/rack/adapter.rb,
lib/merb-core/controller/mime.rb,
lib/merb-core/dispatch/router.rb,
lib/merb-core/dispatch/cookies.rb,
lib/merb-core/dispatch/request.rb,
lib/merb-core/dispatch/session.rb,
lib/merb-core/rack/adapter/ebb.rb,
lib/merb-core/rack/adapter/irb.rb,
lib/merb-core/rack/application.rb,
lib/merb-core/rack/adapter/fcgi.rb,
lib/merb-core/rack/adapter/thin.rb,
lib/merb-core/rack/adapter/runner.rb,
lib/merb-core/rack/adapter/mongrel.rb,
lib/merb-core/rack/adapter/webrick.rb,
lib/merb-core/controller/exceptions.rb,
lib/merb-core/dispatch/router/route.rb,
lib/merb-core/dispatch/session/cookie.rb,
lib/merb-core/dispatch/session/memory.rb,
lib/merb-core/dispatch/router/behavior.rb,
lib/merb-core/test/helpers/view_helper.rb,
lib/merb-core/test/helpers/route_helper.rb,
lib/merb-core/dispatch/session/memcached.rb,
lib/merb-core/controller/mixins/responder.rb,
lib/merb-core/dispatch/router/cached_proc.rb,
lib/merb-core/test/helpers/request_helper.rb,
lib/merb-core/controller/mixins/controller.rb,
lib/merb-core/rack/adapter/evented_mongrel.rb,
lib/merb-core/test/helpers/controller_helper.rb,
lib/merb-core/rack/handler/mongrel.rb

Overview

Most of this code is taken from bitsweat’s implementation in rails

Defined Under Namespace

Modules: Const, ControllerExceptions, ControllerMixin, GlobalHelpers, InlineTemplates, Plugins, Rack, RenderMixin, ResponderMixin, SessionMixin, Template, Test Classes: AcceptType, BootLoader, Config, Controller, CookieSession, Cookies, Dispatcher, Logger, MemCacheSession, MemorySession, MemorySessionContainer, Request, Responder, Router, Server, SimpleSet

Constant Summary collapse

VERSION =
'0.9.2'
RELEASE =

Merb::RELEASE meanings: ‘dev’ : unreleased ‘pre’ : pre-release Gem candidates

nil    : released

You should never check in to trunk with this changed. It should stay ‘dev’. Change it to nil in release tags.

nil

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.adapterObject

Returns the value of attribute adapter.



54
55
56
# File 'lib/merb-core.rb', line 54

def adapter
  @adapter
end

.environmentObject Also known as: env

Returns the value of attribute environment.



54
55
56
# File 'lib/merb-core.rb', line 54

def environment
  @environment
end

.frozenObject

Returns the value of attribute frozen.



184
185
186
# File 'lib/merb-core.rb', line 184

def frozen
  @frozen
end

.generator_scopeObject

Set up default variables under Merb



166
167
168
# File 'lib/merb-core.rb', line 166

def generator_scope
  @generator_scope
end

.klass_hashesObject

Set up default variables under Merb



166
167
168
# File 'lib/merb-core.rb', line 166

def klass_hashes
  @klass_hashes
end

.load_pathsObject

Returns the value of attribute load_paths.



54
55
56
# File 'lib/merb-core.rb', line 54

def load_paths
  @load_paths
end

.loggerObject

Logger settings



118
119
120
# File 'lib/merb-core.rb', line 118

def logger
  @logger
end

.registered_session_typesObject (readonly)

Returns the value of attribute registered_session_types.



170
171
172
# File 'lib/merb-core.rb', line 170

def registered_session_types
  @registered_session_types
end

Class Method Details

.add_mime_type(key, transform_method, values, new_response_headers = {}) ⇒ Object

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, and def render_yaml

Parameters

key<Symbol>

The name of the mime-type. This is used by the provides API

transform_method<~to_s>

The associated method to call on objects to convert them to the appropriate mime-type. For instance, :json would use :to_json as its transform_method.

values<Array>

A list of possible values sent in the Accept header, such as text/html, that should be associated with this content-type.

new_response_headers<Hash>

The response headers to set for the the mime type.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/merb-core/controller/mime.rb', line 36

def add_mime_type(key, transform_method, values, new_response_headers = {}) 
  enforce!(key => Symbol, values => Array)
  ResponderMixin::TYPES.update(key => 
    {:request_headers   => values, 
     :transform_method  => transform_method,
     :response_headers  => new_response_headers })

  Merb::RenderMixin.class_eval <<-EOS, __FILE__, __LINE__
    def render_#{key}(thing = nil, opts = {})
      self.content_type = :#{key}
      render thing, opts
    end
  EOS
end

.add_rakefiles(*rakefiles) ⇒ Object

Parameters

*rakefiles

Rakefiles to add to the list of plugin Rakefiles.



288
289
290
291
# File 'lib/merb-core.rb', line 288

def add_rakefiles(*rakefiles)
  @rakefiles ||= ['merb-core/test/tasks/spectasks']
  @rakefiles += rakefiles
end

.available_mime_typesObject

Returns

Hash

The available mime types.



6
7
8
# File 'lib/merb-core/controller/mime.rb', line 6

def available_mime_types
  ResponderMixin::TYPES
end

.config(&block) ⇒ Object

If block was given configures using the block.

Parameters

&block

Configuration parameter block, see example below.

Returns

Hash

The current configuration.

Examples

Merb.config do
  beer               "good"
  hashish            :foo => "bar"
  environment        "development"
  log_level          "debug"
  use_mutex          false
  session_store      "cookie"
  session_secret_key "0d05a226affa226623eb18700"
  exception_details  true
  reload_classes     true
  reload_time        0.5 
end


249
250
251
252
# File 'lib/merb-core.rb', line 249

def config(&block)
  Merb::Config.configure(&block) if block_given?
  Config
end

.dir_for(type) ⇒ Object

Parameters

type<Symbol>

The type of path to retrieve directory for, e.g. :view.

Returns

String

The directory for the requested type.



84
# File 'lib/merb-core.rb', line 84

def dir_for(type)  Merb.load_paths[type].first end

.disable(*components) ⇒ Object

Disables the given core components, like a Gem for example.

Parameters

*args

One or more symbols of Merb internal components.



258
259
260
# File 'lib/merb-core.rb', line 258

def disable(*components)
  disabled_components.push *components
end

.disabled?(*components) ⇒ Boolean

Returns

Boolean

True if all components (or just one) are disabled.

Returns:

  • (Boolean)


276
277
278
# File 'lib/merb-core.rb', line 276

def disabled?(*components)
  components.all? { |c| disabled_components.include?(c) }
end

.disabled_componentsObject

Returns

Array

All components that have been disabled.



270
271
272
# File 'lib/merb-core.rb', line 270

def disabled_components
  Merb::Config[:disabled_components] ||= []
end

.disabled_components=(components) ⇒ Object

Parameters

Array

All components that should be disabled.



264
265
266
# File 'lib/merb-core.rb', line 264

def disabled_components=(components)
  disabled_components.replace components
end

.exception(e) ⇒ Object

Required to show exceptions in the log file

e<Exception>

The exception that a message is being generated for



252
253
254
255
# File 'lib/merb-core/controller/exceptions.rb', line 252

def self.exception(e) #:nodoc:
  "#{ e.message } - (#{ e.class })\n" <<  
  "#{(e.backtrace or []).join("\n")}" 
end

.flat!(framework = {}) ⇒ Object

Allows flat apps by setting no default framework directories and yielding a Merb::Router instance. This is optional since the router will automatically configure the app with default routes.

Block parameters

r<Merb::Router::Behavior>

The root behavior upon which new routes can be added.



156
157
158
159
160
161
162
163
# File 'lib/merb-core.rb', line 156

def flat!(framework = {})
  Merb::Config[:framework] = framework

  Merb::Router.prepare do |r|
    yield(r) if block_given?
    r.default_routes
  end
end

.framework_rootObject

Returns

String

The root directory of the Merb framework.



147
# File 'lib/merb-core.rb', line 147

def framework_root()  @framework_root ||= File.dirname(__FILE__)  end

.frozen!Object

Used by script/frozen-merb and other freezers to mark Merb as frozen.



193
194
195
# File 'lib/merb-core.rb', line 193

def frozen!
  @frozen = true
end

.frozen?Boolean

Returns

Boolean

True if Merb is running via script/frozen-merb or other freezer.

Returns:

  • (Boolean)


188
189
190
# File 'lib/merb-core.rb', line 188

def frozen?
  @frozen
end

.glob_for(type) ⇒ Object

Parameters

type<Symbol>

The type of path to retrieve glob for, e.g. :view.

Returns
String

The pattern with which to match files within the type directory.



91
# File 'lib/merb-core.rb', line 91

def glob_for(type) Merb.load_paths[type][1]    end

.load_config(options = {}) ⇒ Object

Load configuration and assign logger.

Parameters

options<Hash>

Options to pass on to the Merb config.



201
202
203
204
# File 'lib/merb-core.rb', line 201

def load_config(options = {})
  Merb::Config.setup({ :log_file => STDOUT, :log_level => :warn, :log_auto_flush => true }.merge(options))
  Merb::BootLoader::Logger.run
end

.load_dependencies(options = {}) ⇒ Object

Load all basic dependencies (selected BootLoaders only).

Parameters

options<Hash>

Options to pass on to the Merb config.



210
211
212
213
214
215
# File 'lib/merb-core.rb', line 210

def load_dependencies(options = {})
  load_config(options)
  Merb::BootLoader::BuildFramework.run
  Merb::BootLoader::Dependencies.run
  Merb::BootLoader::BeforeAppRuns.run
end

.log_fileObject

Returns

String

The path to the log file. If this Merb instance is running as a daemon this will return STDOUT.



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/merb-core.rb', line 124

def log_file
  if Merb::Config[:log_file]
    Merb::Config[:log_file]
  elsif Merb.testing?
    log_path / "merb_test.log"
  elsif !(Merb::Config[:daemonize] || Merb::Config[:cluster])
    STDOUT
  else
    log_path / "merb.#{Merb::Config[:port]}.log"
  end
end

.log_pathObject

Returns

String

The directory that contains the log file.



138
139
140
141
142
143
# File 'lib/merb-core.rb', line 138

def log_path
  case Merb::Config[:log_file]
  when String then File.dirname(Merb::Config[:log_file])
  else Merb.root_path("log")
  end
end

.mime_by_request_header(header) ⇒ Object

The mime-type for a particular inbound Accepts header.

Parameters

header<String>

The name of the header to find the mime-type for.

Returns

Hash

The mime type information.



83
84
85
# File 'lib/merb-core/controller/mime.rb', line 83

def mime_by_request_header(header)
  available_mime_types.find {|key,info| info[request_headers].include?(header)}.first
end

.mime_transform_method(key) ⇒ Object

Parameters

key<Symbol>

The key that represents the mime-type.

Returns

Symbol

The transform method for the mime type, e.g. :to_json.

Raises

ArgumentError

The requested mime type is not valid.

Raises:

  • (ArgumentError)


71
72
73
74
# File 'lib/merb-core/controller/mime.rb', line 71

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

.push_path(type, path, file_glob = "**/*.rb") ⇒ Object

This is the core mechanism for setting up your application layout merb-core won’t set a default application layout, but merb-more will use the app/:type layout that is in use in Merb 0.5.

Parameters

type<Symbol>

The type of path being registered (i.e. :view)

path<String>

The full path

file_glob<String>

A glob that will be used to autoload files under the path. Defaults to “*/.rb”.



70
71
72
73
# File 'lib/merb-core.rb', line 70

def push_path(type, path, file_glob = "**/*.rb")
  enforce!(type => Symbol)
  load_paths[type] = [path, file_glob]
end

.rakefilesObject

Returns

Array

All Rakefiles for plugins.



282
283
284
# File 'lib/merb-core.rb', line 282

def rakefiles
  @rakefiles ||= ['merb-core/test/tasks/spectasks']
end

.register_session_type(name, file, description = nil) ⇒ Object

Parameters

name<~to_s>

Name of the session type to register.

file<String>

The file that defines this session type.

description<String>

An optional description of the session type.



176
177
178
179
180
181
182
# File 'lib/merb-core.rb', line 176

def register_session_type(name, file, description = nil)
  @registered_session_types ||= Dictionary.new
  @registered_session_types[name] = {
    :file => file,
    :description => (description || "Using #{name} sessions")
  }
end

.reloadObject

Reload the framework.



218
219
220
# File 'lib/merb-core.rb', line 218

def reload
  Merb::BootLoader::ReloadClasses.reload
end

.remove_mime_type(key) ⇒ Object

Removes a MIME-type from the mime-type list.

Parameters

key<Symbol>

The key that represents the mime-type to remove.

Note

:all is the key for /; It can’t be removed.



58
59
60
61
# File 'lib/merb-core/controller/mime.rb', line 58

def remove_mime_type(key)
  return false if key == :all
  ResponderMixin::TYPES.delete(key)
end

.remove_paths(*args) ⇒ Object



75
76
77
# File 'lib/merb-core.rb', line 75

def remove_paths(*args)
  args.each {|arg| load_paths.delete(arg)}
end

.restart_environment(argv = {}) ⇒ Object

Restart the Merb environment explicitly.

Parameters

argv<String, Hash>

The config arguments to restart Merb with. Defaults to Merb::Config.



49
50
51
52
# File 'lib/merb-core.rb', line 49

def restart_environment(argv={})
  @started = false
  start_environment(Merb::Config.to_hash.merge(argv))
end

.rootObject

Returns

String

The Merb root path.



95
# File 'lib/merb-core.rb', line 95

def root()          @root || Merb::Config[:merb_root] || Dir.pwd  end

.root=(value) ⇒ Object

Parameters

value<String>

Path to the root directory.



99
# File 'lib/merb-core.rb', line 99

def root=(value)    @root = value                                 end

.root_path(*path) ⇒ Object

Parameters

*path

The relative path (or list of path components) to a directory under the root of the application.

Returns

String

The full path including the root.

Examples

Merb.root = "/home/merb/app"
Merb.path("images") # => "/home/merb/app/images"
Merb.path("views", "admin") # => "/home/merb/app/views/admin"



115
# File 'lib/merb-core.rb', line 115

def root_path(*path) File.join(root, *path)                       end

.start(argv = ARGV) ⇒ Object

Startup Merb by setting up the Config and starting the server.

Parameters

argv<String, Hash>

The config arguments to start Merb with. Defaults to ARGV.



21
22
23
24
25
26
27
28
29
30
# File 'lib/merb-core.rb', line 21

def start(argv=ARGV)
  if Hash === argv
    Merb::Config.setup(argv)
  else
    Merb::Config.parse_args(argv)
  end
  Merb.environment = Merb::Config[:environment]
  Merb.root = Merb::Config[:merb_root]
  Merb::Server.start(Merb::Config[:port], Merb::Config[:cluster])
end

.start_environment(argv = ARGV) ⇒ Object

Start the Merb environment, but only if it hasn’t been loaded yet.

Parameters

argv<String, Hash>

The config arguments to start Merb with. Defaults to ARGV.



37
38
39
40
41
42
# File 'lib/merb-core.rb', line 37

def start_environment(argv=ARGV)
  unless (@started ||= false)
    start(argv)
    @started = true
  end
end

.testing?Boolean

Returns

Boolean

True if Merb is running via spec_helper.rb or other TEST scenario.

Returns:

  • (Boolean)


224
225
226
# File 'lib/merb-core.rb', line 224

def testing?
  $TESTING || Merb::Config[:testing]
end