define_constant_from_directory(from_mod, const_name, qualified_name, path_suffix)
click to toggle source
def define_constant_from_directory(from_mod, const_name, qualified_name, path_suffix)
return false unless Desert::Manager.directory_on_load_path?(path_suffix)
if autoloaded_constants.include?(qualified_name)
raise "Constant #{qualified_name} is already autoloaded but is not defined as a constant"
end
from_mod.const_set(const_name, Module.new)
autoloaded_constants << qualified_name
return true
end
define_constant_from_file(from_mod, const_name, qualified_name, path_suffix)
click to toggle source
def define_constant_from_file(from_mod, const_name, qualified_name, path_suffix)
files = Desert::Manager.files_on_load_path(path_suffix)
files.each do |file|
require_or_load file
loaded << file.gsub(/\.rb$/, '')
next if autoloaded_constants.include?(qualified_name)
next if load_once_path?(file)
autoloaded_constants << qualified_name
end
loaded << File.expand_path(path_suffix)
from_mod.const_defined?(const_name)
end
define_constant_with_desert_loading(from_mod, const_name, qualified_name, path_suffix)
click to toggle source
def define_constant_with_desert_loading(from_mod, const_name, qualified_name, path_suffix)
define_constant_from_file(from_mod, const_name, qualified_name, path_suffix) ||
define_constant_from_directory(from_mod, const_name, qualified_name, path_suffix)
end
depend_on_with_desert(file_name, swallow_load_errors = false)
click to toggle source
def depend_on_with_desert(file_name, swallow_load_errors = false)
successfully_loaded_in_plugin = false
Desert::Manager.files_on_load_path(file_name).each do |file|
require_or_load(file)
successfully_loaded_in_plugin = true
end
begin
unless successfully_loaded_in_plugin
require_or_load file_name
loaded << File.expand_path(file_name)
end
rescue LoadError
if !swallow_load_errors && !successfully_loaded_in_plugin
raise
end
end
end
guard_against_anonymous_module(from_mod)
click to toggle source
def guard_against_anonymous_module(from_mod)
return Object if from_mod.name.blank?
return from_mod
end
has_parent_module?(mod)
click to toggle source
def has_parent_module?(mod)
mod != Object
end
initialize_schema_migrations_table_with_plugins()
click to toggle source
def initialize_schema_migrations_table_with_plugins
initialize_schema_migrations_table_without_plugins
begin
smt = Desert::PluginMigrations::Migrator.schema_migrations_table_name
unless ActiveRecord::Base.connection.tables.include?(smt)
execute "CREATE TABLE #{smt} (plugin_name #{type_to_sql(:string)}, version #{type_to_sql(:string)})"
end
if Desert::PluginMigrations::Migrator.legacy_schema_table_exists?
plugins_and_versions = select_all("SELECT plugin_name, version from #{Desert::PluginMigrations::Migrator.schema_info_table_name}")
plugins_and_versions.each do |plugin_data|
plugin_name, version = plugin_data["plugin_name"], plugin_data["version"]
plugin = Desert::Manager.find_plugin(plugin_name)
migration_versions = Dir["#{plugin.migration_path}/*.rb"].map do |path|
File.basename(path, ".rb")
end.select do |migration|
version_from_table_stripped = version.to_s.sub(/^0*/, '')
migration_version_stripped = migration.split("_").first.sub(/^0*/, '')
Integer(migration_version_stripped) <= Integer(version_from_table_stripped)
end
migration_versions.each do |migration_version|
insert_sql = ActiveRecord::Base.send(:sanitize_sql, [
"INSERT INTO #{Desert::PluginMigrations::Migrator.schema_migrations_table_name}(plugin_name, version) VALUES(?, ?)",
plugin_name,
Integer(migration_version.split("_").first.sub(/^0*/, ''))
])
execute insert_sql
end
end
end
end
end
initialize_with_desert_plugins(*args)
click to toggle source
def initialize_with_desert_plugins(*args)
initialize_without_desert_plugins *args
Desert::Manager.plugins.reverse.each do |plugin|
view_paths << plugin.templates_path
end
end
load_missing_constant_with_desert(from_mod, const_name)
click to toggle source
def load_missing_constant_with_desert(from_mod, const_name)
from_mod = guard_against_anonymous_module(from_mod)
qualified_name = qualified_name_for from_mod, const_name
path_suffix = qualified_name.underscore
if define_constant_with_desert_loading(from_mod, const_name, qualified_name, path_suffix)
return from_mod.const_get(const_name)
end
if has_parent_module?(from_mod)
look_for_constant_in_parent_module(from_mod, const_name, qualified_name, path_suffix)
else
raise NameError, "Constant #{qualified_name} from #{path_suffix}.rb not found"
end
end
load_once_path?(path)
click to toggle source
def load_once_path?(path)
load_once_paths.any? { |base| File.expand_path(path).starts_with? File.expand_path(base) }
end
load_with_desert(file)
click to toggle source
def load_with_desert(file)
__each_matching_file(file) do |file|
load_without_desert file
end
end
look_for_constant_in_parent_module(from_mod, const_name, qualified_name, path_suffix)
click to toggle source
def look_for_constant_in_parent_module(from_mod, const_name, qualified_name, path_suffix)
return from_mod.parent.const_missing(const_name)
rescue NameError => e
raise NameError, "Constant #{qualified_name} from #{path_suffix}.rb not found\n#{e.message}"
end
require_with_desert(path)
click to toggle source
def require_with_desert(path)
relative_include_path = (path =~ /\.rb$/) ? path : "#{path}.rb"
if $".include?(relative_include_path)
return false
else
__each_matching_file(path) do |expanded_path|
require_without_desert expanded_path
end
$" << relative_include_path
return true
end
end