Class: Radar::Application
- Inherits:
-
Object
- Object
- Radar::Application
- Defined in:
- lib/radar/application.rb
Overview
Represents an instance of Radar for a given application. Every application which uses Radar must instantiate an Application.
Constant Summary collapse
- @@registered =
{}
- @@mutex =
Mutex.new
Instance Attribute Summary collapse
-
#creation_location ⇒ Object
readonly
Returns the value of attribute creation_location.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.clear! ⇒ Object
Removes all registered applications.
-
.find(name) ⇒ Application
Looks up an application which was registered with the given name.
Instance Method Summary collapse
-
#config {|Config| ... } ⇒ Config
Configures the application by returning the configuration object.
-
#initialize(name, register = true) ⇒ Application
constructor
Creates a new application with the given name and registers it for lookup later.
-
#report(exception) ⇒ Object
Reports an exception.
-
#rescue_at_exit! ⇒ Object
Hooks this application into the ‘at_exit` handler so that application crashing exceptions are properly reported.
-
#to_hash ⇒ Hash
Converts application to a serialization-friendly hash.
Constructor Details
#initialize(name, register = true) ⇒ Application
Creates a new application with the given name and registers it for lookup later. If a block is given, it will be yielded with the new instantiated Radar::Application so you can also #config it all in one go.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/radar/application.rb', line 41 def initialize(name, register=true) @@mutex.synchronize do raise ApplicationAlreadyExists.new("'#{name}' already defined at '#{self.class.find(name).creation_location}'") if self.class.find(name) @name = name @creation_location = caller.first yield self if block_given? @@registered[name] = self if register end end |
Instance Attribute Details
#creation_location ⇒ Object (readonly)
Returns the value of attribute creation_location.
17 18 19 |
# File 'lib/radar/application.rb', line 17 def creation_location @creation_location end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
16 17 18 |
# File 'lib/radar/application.rb', line 16 def name @name end |
Class Method Details
.clear! ⇒ Object
Removes all registered applications. **This is only exposed for testing purposes.**
29 30 31 |
# File 'lib/radar/application.rb', line 29 def self.clear! @@registered.clear end |
.find(name) ⇒ Application
Looks up an application which was registered with the given name.
23 24 25 |
# File 'lib/radar/application.rb', line 23 def self.find(name) @@registered[name] end |
Instance Method Details
#config {|Config| ... } ⇒ Config
Configures the application by returning the configuration object. If a block is given, the configuration object will be passed into it, allowing for a cleaner way of configuring your applications.
$app = Radar::Application.new
$app.config do |config|
config.storage_directory = "foo"
end
64 65 66 67 68 |
# File 'lib/radar/application.rb', line 64 def config @_config ||= Config.new yield @_config if block_given? @_config end |
#report(exception) ⇒ Object
Reports an exception. This will send the exception on to the various reporters configured for this application.
74 75 76 77 78 79 80 81 |
# File 'lib/radar/application.rb', line 74 def report(exception) data = ExceptionEvent.new(self, exception) # Report the exception to each of the reporters config.reporters.each do |reporter| reporter.report(data) end end |
#rescue_at_exit! ⇒ Object
Hooks this application into the ‘at_exit` handler so that application crashing exceptions are properly reported.
85 86 87 |
# File 'lib/radar/application.rb', line 85 def rescue_at_exit! at_exit { report($!) if $! } end |
#to_hash ⇒ Hash
Converts application to a serialization-friendly hash.
92 93 94 |
# File 'lib/radar/application.rb', line 92 def to_hash { :name => name } end |