class RightPublish::Profile
Constants
- DEFAULT_VERBOSE
- SUBSTITUTION
- USER_PROFILE_DIR
Location where per-user profiles are stored
Attributes
settings[RW]
Public Class Methods
config()
click to toggle source
# File lib/right_publish/profile.rb, line 13 def self.config() return Profile.instance.settings end
log(s, level=:info)
click to toggle source
# File lib/right_publish/profile.rb, line 17 def self.log(s, level=:info) puts(s) if Profile.config[:verbose] || level != :debug end
Private Class Methods
symbolize_profile(args)
click to toggle source
# File lib/right_publish/profile.rb, line 59 def self.symbolize_profile(args) if args.is_a? Hash Hash[ args.collect do |key, value| [key.to_sym, symbolize_profile(value)] end ] else args end end
Public Instance Methods
load(path)
click to toggle source
Load a YML profile from disk. Profiles can be identified by their path on disk, or by the name of a YML file located in USER_PROFILE_DIR
(without YML extension).
@param [String] path absolute/relative path to file, or the name of per-user profile @return [true] always returns true
@example per-user profile
Profile.instance.load("gem")
@example profile on disk
Profile.instance.load("config/publish/nightly.yml")
@see USER_PROFILE_DIR
# File lib/right_publish/profile.rb, line 34 def load(path) if File.exists?(path) @settings ||= Profile.symbolize_profile(YAML.load_file(path)) validate_profile elsif (path = File.join(USER_PROFILE_DIR, "#{path}.yml")) && File.exists?(path) @settings ||= Profile.symbolize_profile(YAML.load_file(path)) validate_profile else raise LoadError, "Missing profile '#{path}'" end true rescue LoadError raise rescue Exception => e raise LoadError, "Cannot load profile '#{path}' - #{e}" end
register_section(section_key, section_options)
click to toggle source
# File lib/right_publish/profile.rb, line 52 def register_section(section_key, section_options) @config_map ||= {} @config_map[section_key] ||= section_options end
Private Instance Methods
check_section(section, attrs)
click to toggle source
# File lib/right_publish/profile.rb, line 71 def check_section(section, attrs) provider = (attrs[:provider] && attrs[:provider]) || section raise LoadError.new("Unknown provider #{attrs[:provider]} for: #{section}") unless @config_map[provider] @settings[section] ||= {} @config_map[provider].each_pair do |attr, default| setting = @settings[section][attr] if setting.nil? && (default != :attr_optional) if default == :attr_needed raise LoadError.new("Missing attribute #{section}:#{attr}") end @settings[section][attr] = default end # Perform environment-variable substitition if the setting is a leaf node if setting.is_a?(String) while match = SUBSTITUTION.match(setting) if ENV.key?(match[1]) setting = setting.gsub(match[0], ENV[match[1]]) else raise LoadError.new("Missing environment variable #{match[1]}.") end end end @settings[section][attr] = setting end end
validate_profile()
click to toggle source
# File lib/right_publish/profile.rb, line 101 def validate_profile() @settings[:verbose] = DEFAULT_VERBOSE unless @settings[:verbose] @settings.each_pair { |section, attrs| check_section(section, attrs) if attrs.is_a? Hash } end