lib/cliutils/prefs/pref.rb in cliutils-2.0.3 vs lib/cliutils/prefs/pref.rb in cliutils-2.1.0
- old
+ new
@@ -193,33 +193,33 @@
info(@pre[:message])
prompt('Press enter to continue')
if (@pre[:action])
action_obj = _init_action(@pre[:action][:name])
- action_obj.run
+ action_obj.run if action_obj
end
end
# Evaluates the post-prompt Hash and does the right thing. :)
# @return [void]
def _eval_post
info(@post[:message])
+ prompt('Press enter to continue')
if (@post[:action])
action_obj = _init_action(@post[:action][:name])
- action_obj.run
+ action_obj.run if action_obj
end
end
# Attempts to instantiate a Pre or Post Action based on name; if
# successful, the new object gets placed in @validator_objects
# @param [String] path_or_name The path to or name of the Action
# @return [void]
def _init_action(path_or_name)
obj = _load_asset(ASSET_TYPE_ACTION, path_or_name)
unless obj.nil?
- obj.pref = self
obj.parameters = @pre[:action][:parameters]
end
obj
end
@@ -228,11 +228,10 @@
# @param [Hash] behavior_hash The Behavior attributes
# @return [void]
def _init_and_add_behavior(behavior_hash)
obj = _load_asset(ASSET_TYPE_BEHAVIOR, behavior_hash[:name])
unless obj.nil?
- obj.pref = self
obj.parameters = behavior_hash[:parameters]
@behavior_objects << obj
end
end
@@ -241,41 +240,69 @@
# @param [String] path_or_name The path to or name of the Validator
# @return [void]
def _init_and_add_validator(path_or_name)
obj = _load_asset(ASSET_TYPE_VALIDATOR, path_or_name)
unless obj.nil?
- obj.pref = self
@validator_objects << obj
end
end
# General function to load an asset (a Validator or Behavior).
# Note that if an asset isn't found, the user is warned, but
# execution continues.
# @param [Integer] type ASSET_TYPE_BEHAVIOR or ASSET_TYPE_VALIDATOR
# @param [String] path_or_name The path to or name of the asset
- # @return [Object]
+ # @return [Object]
def _load_asset(type, path_or_name)
- if File.exist?(path_or_name)
+ asset_found = false
+ if File.file?(File.expand_path(path_or_name))
# If the file exists, we're assuming that the user
# passed a filepath.
- asset_path = File.expand_path(path_or_name) if path_or_name.start_with?('~')
- asset_path = "#{ path_or_name }_#{ @@asset_labels[type][:file_suffix] }"
+ asset_found = true
+ asset_path = File.expand_path(path_or_name) if path_or_name.start_with?('~')
asset_name = File.basename(path_or_name, '.*').camelize
- else
- # If it doesn't, we're assuming that the user
- # passed a class name.
+ end
+
+ unless asset_found
+ # If the file doesn't exist, look to see if it's been
+ # pre-registered.
+ symbol = File.basename(path_or_name, '.*').camelize.to_sym
+ case type
+ when Pref::ASSET_TYPE_ACTION
+ if CLIUtils::Prefs.registered_actions.key?(symbol)
+ asset_found = true
+ asset_path = CLIUtils::Prefs.registered_actions[symbol][:path]
+ asset_name = CLIUtils::Prefs.registered_actions[symbol][:class]
+ end
+ when Pref::ASSET_TYPE_BEHAVIOR
+ if CLIUtils::Prefs.registered_behaviors.key?(symbol)
+ asset_found = true
+ asset_path = CLIUtils::Prefs.registered_behaviors[symbol][:path] rescue ''
+ asset_name = CLIUtils::Prefs.registered_behaviors[symbol][:class] rescue ''
+ end
+ when Pref::ASSET_TYPE_VALIDATOR
+ if CLIUtils::Prefs.registered_validators.key?(symbol)
+ asset_found = true
+ asset_path = CLIUtils::Prefs.registered_validators[symbol][:path] rescue ''
+ asset_name = CLIUtils::Prefs.registered_validators[symbol][:class] rescue ''
+ end
+ end
+ end
+
+ unless asset_found
+ # If the file doesn't exist and there's no pre-registered
+ # asset, as a last check, look for it as a built-in asset.
_default = File.join(File.dirname(__FILE__), "pref_#{ @@asset_labels[type][:file_suffix] }s")
asset_path = File.join(_default, "#{ path_or_name }_#{ @@asset_labels[type][:file_suffix] }")
- asset_name = path_or_name.camelize
+ asset_name = "#{ path_or_name.camelize }#{ @@asset_labels[type][:class_suffix] }"
end
- # Try to load and instantiate the Action. If that fails, warn
+ # Try to load and instantiate the asset. If that fails, warn
# the user with a message and skip over it.
begin
- require asset_path
- Object.const_get("CLIUtils::#{ asset_name }#{ @@asset_labels[type][:class_suffix] }").new
- rescue LoadError
+ require File.expand_path(asset_path)
+ Object.const_get("CLIUtils::#{ asset_name }").new
+ rescue LoadError => e
messenger.warn("Skipping undefined Pref #{ @@asset_labels[type][:class_suffix] }: #{ path_or_name }")
nil
end
end
end