lib/masterview/initializer.rb in masterview-0.2.2 vs lib/masterview/initializer.rb in masterview-0.2.3
- old
+ new
@@ -225,10 +225,31 @@
# within the template source directory.
#
# Default: <tt>'*.html'</tt>
attr_accessor :template_filename_pattern
+ # Regex pattern specifications for identifying the base directory
+ # on asset references in a template document to convert
+ # design-time assert references for images, stylesheets,
+ # and javascript files into relative references for use
+ # with the standard Rails asset helper functions.
+ #
+ # The patterns are a hash indexed by asset type.
+ # Asset types are :images, :stylesheets, :javascripts
+ #
+ # The standard patterns match path prefixes up through
+ # <code>public/<i>asset-type</i></code>. For example,
+ # an <code>mv:stylesheet_link</code> directive of the form:
+ #
+ # <link rel="stylesheet" type="text/css" href="../../../public/stylesheets/mystyles.css" mv:stylesheet_link="" />
+ #
+ # would match the standard base-dir prefix and result in:
+ #
+ # <%= stylesheet_link_tag "mystyles" %>
+ #
+ attr_accessor :template_asset_base_ref_pattern
+
# === Template Generation Options
# Path to the directory where Masterview template output (rhtml)
# will be generated.
#
@@ -413,10 +434,12 @@
# unpack the supported keyword args
app_root_path = params[:app_root_path]
rails_app_root_path = params[:rails_app_root_path]
env = params[:environment]
+ is_development = env == 'development'
+
program_root_path = File.expand_path( '.' )
#assert program_root_path == Dir.pwd
debug_TRACE_HACK = false #temp dev debug tracking for rails vs. non-rails init
@@ -485,10 +508,11 @@
# For a rails app, we have a point of view on where to find config files.
# A standalone client needs to proactively tell us where to find their settings.
self.config_dir_path = rails_app? ? "config/masterview" : nil
self.environment = on_rails? ? ::RAILS_ENV : env
self.directive_paths = [ builtin_directives_path ]
+ discover_standard_directive_path_additions()
#TODO: if rails_app? && File.exist?( "#{rails_root_path}/app/masterview/directives" ) THEN append it as well
self.rebuild_backups_tmp_dir_path = rails_app? ? File.join( rails_root_path, 'tmp/masterview/rebuild/backups') : nil
# use Log4r by default if available, otherwise Logger from standard ruby library
# find out if Kernel#require will succeed if we try to load Log4R
@@ -505,10 +529,15 @@
end
# template source options
self.template_src_dir_path = rails_app? ? 'app/views' : 'masterview/templates' # bolts down abs ref
self.template_filename_pattern = '*.html'
+ self.template_asset_base_ref_pattern = {
+ :images => /public\/images\/(.*)/,
+ :stylesheets => /public\/stylesheets\/(.*)/,
+ :javascripts => /public\/javascripts\/(.*)/,
+ }
STDOUT.puts "...template_src_dir_path=#{template_src_dir_path || 'nil'}" if debug_TRACE_HACK
# template generation options
self.template_dst_dir_path = rails_app? ? 'app/views' : 'masterview/output' # bolts down abs ref
@@ -536,12 +565,12 @@
self.inline_erb_substitution_regex = /\{\{\{(([^}]|\}[^}]|\}\}[^}])*)\}\}\}/
# Rails application options
self.parse_masterview_templates_at_startup = true
self.reparse_changed_masterview_templates = on_rails? ? (not ActionController::Base.perform_caching) : false
- self.enable_admin_pages = false
- self.enable_view_rhtml = false
+ self.enable_admin_pages = is_development
+ self.enable_view_rhtml = is_development
self.generate_rhtml_files = false
STDOUT.puts "...mv config initialized with default settings\n" if debug_TRACE_HACK
end
@@ -557,10 +586,20 @@
return if ! File.directory?(dir_path)
}
true
end
+ # automatically append directives in std app dir to mv builtins, if available
+ def discover_standard_directive_path_additions() #:nodoc:
+ return if ! rails_app? #?or can we take a point of view of std loc? e.g., 'masterview/directives'
+ app_directives_path = rails_app? ? "app/masterview/directives" : nil #??"masterview/directives"?
+ app_directives_path = File.join( root_path, app_directives_path )
+ if File.directory?(app_directives_path) #?and not empty?
+ directive_paths << app_directives_path #root_path already expanded
+ end
+ end
+
# The path to the application's config settings file.
# By default the file is at <tt>config/masterview/settings.rb</tt>.
def app_settings_path
config_dir_path ? "#{config_dir_path}/settings.rb" : nil
end
@@ -639,10 +678,11 @@
# Create a new Initializer instance that references the given Configuration
# instance.
def initialize(configuration) #:nodoc:
@configuration = configuration
+ @log_msg_q = [] # collect log messages that arrive prior to the logger
end
# Load the MasterView configuration settings
# and initialize the template engine.
def process
@@ -657,11 +697,10 @@
# Intended for use in testing.
def initialize_configuration
#?? return if MasterView.const_defined?(:ConfigSettings) ??
load_config_settings
ensure_valid_settings
- discover_standard_directive_path_additions
install_config_settings
# make a final check for running_rails? (in case config settings changed scripts spec)
configuration.decide_if_running_rails
# keep a permananent record of how we got started
configuration.freeze
@@ -693,36 +732,35 @@
return if not (config_file_path && File.file?(config_file_path))
config = configuration # define config var in the binding context of the settings eval
eval(IO.read(config_file_path), binding)
end
- def discover_standard_directive_path_additions #:nodoc:
- #TODO: check for config/masterview/directives and automatically
- # append to the configuration.directive_paths if found.
- # We have to wait until all the config settings are loaded so that
- # all references to root locations and client's own prefs for
- # locating directives.
- # [DJL 01-Jun-2006]
- #... something like:
- #return if not config_dir_path
- #config_directives_path = File.join(configuration.config_dir_path, 'directives')
- #if File.directory?(config_directives_path)
- # configuration.directivePaths << File.expand_path(config_directives_path unless it's already there
- #end
- end
-
# ensure that the requested configuration settings are consistent and valid
# before we actually install anything. Normalize representations as needed.
def ensure_valid_settings #:nodoc:
config = configuration
#??config.root_path = File.expand_path(config.root_path) if config.root_path #?? ensure bolted down firmly?
+ # ensure that the directive load path entries are clean and available
+ if not config.directive_paths.empty?
+ clean_paths = []
+ config.directive_paths.each { | dir |
+ if dir.nil? || ! File.directory?(dir)
+ err_msg = "Invalid directive load path directory: '#{dir}'"
+ #overzealous: raise InvalidPathError.new()
+ #but doesn't exist yet: Log.error err_mage
+ @log_msg_q << [ :error, err_msg ]
+ else
+ dir_path = File.expand_path( dir )
+ clean_paths << dir_path if ! clean_paths.include?(dir_path) # no dups
+ end
+ }
+ config.directive_paths = clean_paths
+ end
- ####if config.directive_paths.length > 1 then expand_path on user-appended entries??
-
# template source and generation options
if config.on_rails?
#TODO: ensure that the config.template_dst_dir_path is
# in the RAILS_ROOT/app/views directory
# (ActionController::Base.view_root)
@@ -799,10 +837,12 @@
# Complete installation of masterview after its code has been loaded
def complete_plugin_installation #:nodoc:
#?? return if MasterView.const_defined?(:Initialized) ??
initialize_mio
initialize_logger
+ #Back out experiment: causes load order problems
+ ##load_directives # held off on this until logger is installed
install_in_rails
# mark the module as fully loaded and configured
MasterView.const_set('Initialized', true)
after_initialize # callback to client's afer_initialize block
end
@@ -832,9 +872,22 @@
# Initialize MasterView::Log with a logger which emits to std output, default DEBUG level
def initialize_logger #:nodoc:
#?return if defined?(Log)
require 'masterview/extras/init_logger'
+ # Kick out any queued-up log messages yearning to be free
+ @log_msg_q.each { | msg_level, msg |
+ Log.send err_level, msg
+ }
+ @log_msg_q.clear
+ end
+
+ def load_directives #:nodoc:
+ # get the directives loaded prior to firing up any template parsing
+ return if ! configuration.on_rails? #ISSUE: causes problem for test cases; is this ever a good idea??
+ MasterView::DirectivesRegistry.process_directives_load_path(
+ configuration.directive_paths,
+ configuration.namespace_prefix )
end
def install_in_rails #:nodoc:
return if ! configuration.on_rails?
enable_mv_admin_pages