2010-01-23 16:30:17 -05:00
|
|
|
require 'rails/initializable'
|
|
|
|
require 'rails/configuration'
|
2010-03-01 21:52:07 -05:00
|
|
|
require 'active_support/inflector'
|
2010-01-23 16:30:17 -05:00
|
|
|
|
2009-12-31 16:11:54 -05:00
|
|
|
module Rails
|
|
|
|
class Railtie
|
2010-01-23 16:30:17 -05:00
|
|
|
autoload :Configurable, "rails/railtie/configurable"
|
|
|
|
autoload :Configuration, "rails/railtie/configuration"
|
2010-01-23 12:41:53 -05:00
|
|
|
|
2009-12-31 16:11:54 -05:00
|
|
|
include Initializable
|
|
|
|
|
2010-03-26 09:41:40 -04:00
|
|
|
ABSTRACT_RAILTIES = %w(Rails::Railtie Rails::Plugin Rails::Engine Rails::Application)
|
|
|
|
RAILTIES_TYPES = ABSTRACT_RAILTIES.map { |r| r.split('::').last }
|
2009-12-31 16:11:54 -05:00
|
|
|
|
2010-01-21 17:14:20 -05:00
|
|
|
class << self
|
2010-01-23 12:41:53 -05:00
|
|
|
def subclasses
|
|
|
|
@subclasses ||= []
|
2010-01-21 17:14:20 -05:00
|
|
|
end
|
2009-12-31 16:11:54 -05:00
|
|
|
|
2010-01-21 17:14:20 -05:00
|
|
|
def inherited(base)
|
2010-03-26 09:41:40 -04:00
|
|
|
unless base.abstract_railtie?
|
2010-01-26 08:58:00 -05:00
|
|
|
base.send(:include, self::Configurable)
|
2010-01-23 12:41:53 -05:00
|
|
|
subclasses << base
|
|
|
|
end
|
2010-01-21 17:14:20 -05:00
|
|
|
end
|
2009-12-31 16:11:54 -05:00
|
|
|
|
2010-01-24 06:23:21 -05:00
|
|
|
def railtie_name(railtie_name = nil)
|
|
|
|
@railtie_name = railtie_name if railtie_name
|
2010-03-01 21:52:07 -05:00
|
|
|
@railtie_name ||= default_name
|
2010-01-21 17:14:20 -05:00
|
|
|
end
|
2009-12-31 16:11:54 -05:00
|
|
|
|
2010-01-24 06:23:21 -05:00
|
|
|
def railtie_names
|
|
|
|
subclasses.map { |p| p.railtie_name }
|
2010-01-21 17:14:20 -05:00
|
|
|
end
|
2010-01-12 07:27:24 -05:00
|
|
|
|
2010-03-25 02:12:32 -04:00
|
|
|
def named(name)
|
|
|
|
subclasses.detect { |r| r.railtie_name == name }
|
|
|
|
end
|
|
|
|
|
2010-02-15 09:44:30 -05:00
|
|
|
def log_subscriber(log_subscriber)
|
|
|
|
Rails::LogSubscriber.add(railtie_name, log_subscriber)
|
2010-01-21 17:14:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def rake_tasks(&blk)
|
|
|
|
@rake_tasks ||= []
|
|
|
|
@rake_tasks << blk if blk
|
|
|
|
@rake_tasks
|
|
|
|
end
|
2009-12-31 16:11:54 -05:00
|
|
|
|
2010-01-21 17:14:20 -05:00
|
|
|
def generators(&blk)
|
|
|
|
@generators ||= []
|
|
|
|
@generators << blk if blk
|
|
|
|
@generators
|
|
|
|
end
|
2010-01-23 12:41:53 -05:00
|
|
|
|
2010-03-26 09:41:40 -04:00
|
|
|
def abstract_railtie?
|
|
|
|
ABSTRACT_RAILTIES.include?(name)
|
2010-01-23 12:41:53 -05:00
|
|
|
end
|
2010-03-01 21:52:07 -05:00
|
|
|
|
2010-03-26 09:41:40 -04:00
|
|
|
protected
|
|
|
|
|
2010-03-01 21:52:07 -05:00
|
|
|
def default_name
|
2010-03-26 09:41:40 -04:00
|
|
|
namespaces = name.split("::")
|
|
|
|
namespaces.pop if RAILTIES_TYPES.include?(namespaces.last)
|
|
|
|
ActiveSupport::Inflector.underscore(namespaces.last).to_sym
|
2010-03-01 21:52:07 -05:00
|
|
|
end
|
2010-01-19 12:43:09 -05:00
|
|
|
end
|
|
|
|
|
2009-12-31 16:11:54 -05:00
|
|
|
def rake_tasks
|
|
|
|
self.class.rake_tasks
|
|
|
|
end
|
|
|
|
|
2010-01-19 12:43:09 -05:00
|
|
|
def generators
|
|
|
|
self.class.generators
|
|
|
|
end
|
|
|
|
|
2009-12-31 16:11:54 -05:00
|
|
|
def load_tasks
|
|
|
|
rake_tasks.each { |blk| blk.call }
|
|
|
|
end
|
2010-01-19 12:43:09 -05:00
|
|
|
|
|
|
|
def load_generators
|
|
|
|
generators.each { |blk| blk.call }
|
|
|
|
end
|
2009-12-31 16:11:54 -05:00
|
|
|
end
|
|
|
|
end
|