lib/strada.rb in strada-0.0.3 vs lib/strada.rb in strada-0.0.4
- old
+ new
@@ -26,22 +26,53 @@
# password = CFG.auth.password
# ssh_port = CFG.ssh.port
# ssh_hosts = CFG.ssh.hosts
class Strada
- CONFIG_FILE = "config"
- # 类对象属性
- attr_reader :cfg, :default, :file
- attr_accessor :system, :user
-
# 类方法
class << self
def cfg(*args)
new(*args).cfg
end
end
+ # 类变量 - 常量,配置保存文件名
+ CONFIG_FILE = "config"
+
+ # 类对象属性
+ attr_reader :cfg, :default, :file, :usr_dir, :sys_dir
+ attr_accessor :system, :user
+
+ # 类对象实例化函数入口
+ def initialize(opts = {})
+ # @param [Hash] opts options for Strada.new
+ # @option opts [String] :name name to use for strada (/etc/name/, ~/.config/name/) - autodetected if not defined
+ # @option opts [String] :adapter adapter to use 'yaml', 'json' or 'toml' for now
+ # @option opts [String] :usr_dir directory for storing user config ~/.config/name/ by default
+ # @option opts [String] :sys_dir directory for storing system config /etc/name/ by default
+ # @option opts [String] :cfg_file configuration filename, by default CONFIG_FILE
+ # @option opts [Hash] :default default settings to use
+ # @option opts [boolean] :load automatically load+merge system+user config with defaults in #cfg
+ # @option opts [boolean] :key_to_s convert keys to string by calling #to_s for keys
+ @name = opts.delete(:name) || meta_name
+ @adapter = opts.delete(:adapter) || "yaml"
+ @usr_dir = opts.delete(:usr_dir) || File.join(Dir.home, ".config", @name)
+ @sys_dir = opts.delete(:sys_dir) || File.join("/etc", @name)
+ @cfg_file = opts.delete(:cfg_file) || CONFIG_FILE
+
+ # 配置对象属性
+ @default = ConfigStruct.new opts.delete(:default)
+ @system = ConfigStruct.new
+ @user = ConfigStruct.new
+ @cfg = ConfigStruct.new
+ @load = true
+ @load = opts.delete(:load) if opts.has_key?(:load)
+ @key_to_s = opts.delete(:key_to_s)
+ raise UnknownOption, "option '#{opts}' not recognized" unless opts.empty?
+ load :all if @load
+ end
+
# When this is called, by default :system and :user are loaded from
# filesystem and merged with default, so that user overrides system which
# overrides default
#
# @param [Symbol] level which configuration level to load, by default :all
@@ -85,44 +116,16 @@
no_config = true if @system.empty? && @user.empty?
if no_config
src = instance_variable_get "@" + src.to_s
instance_variable_set("@" + dst.to_s, src.dup)
save dst
- load if opts.delete :load
+ load if opts.delete(:load)
end
no_config
end
private
- def initialize(opts = {})
- # @param [Hash] opts options for Strada.new
- # @option opts [String] :name name to use for strada (/etc/name/, ~/.config/name/) - autodetected if not defined
- # @option opts [String] :adapter adapter to use 'yaml', 'json' or 'toml' for now
- # @option opts [String] :usr_dir directory for storing user config ~/.config/name/ by default
- # @option opts [String] :sys_dir directory for storing system config /etc/name/ by default
- # @option opts [String] :cfg_file configuration filename, by default CONFIG_FILE
- # @option opts [Hash] :default default settings to use
- # @option opts [boolean] :load automatically load+merge system+user config with defaults in #cfg
- # @option opts [boolean] :key_to_s convert keys to string by calling #to_s for keys
- @name = opts.delete(:name) || meta_name
- @adapter = opts.delete(:adapter) || "yaml"
- @usr_dir = opts.delete(:usr_dir) || File.join(Dir.home, ".config", @name)
- @sys_dir = opts.delete(:sys_dir) || File.join("/etc", @name)
- @cfg_file = opts.delete(:cfg_file) || CONFIG_FILE
-
- # 配置对象属性
- @default = ConfigStruct.new opts.delete(:default)
- @system = ConfigStruct.new
- @user = ConfigStruct.new
- @cfg = ConfigStruct.new
- @load = true
- @load = opts.delete(:load) if opts.has_key?(:load)
- @key_to_s = opts.delete(:key_to_s)
- raise UnknownOption, "option '#{opts}' not recognized" unless opts.empty?
- load :all if @load
- end
-
# 加载配置文件
def load_cfg(dir)
@file = File.join dir, @cfg_file
file = File.read @file
ConfigStruct.new(from(@adapter, file), key_to_s: @key_to_s)
@@ -132,20 +135,22 @@
# 保存配置文件
def save_cfg(dir, config)
config = to(@adapter, config)
file = File.join dir, @cfg_file
- FileUtils.mkdir_p dir
+
+ # 判断目标文件夹是否存在,不存在则新建
+ FileUtils.mkdir_p dir unless Dir.exist? dir
File.write file, config
end
# 合并配置属性
def merge(*configs)
hash = {}
# 将相关配置依次迭代并转换为 RUBY HASH 对象
configs.each do |config|
- hash = hash._config_deep_merge config._config_to_hash
+ hash = hash._config_deep_merge(config._config_to_hash)
end
# 将合并后的 HASH 数据结构转换为配置对象
ConfigStruct.new hash
end
@@ -172,10 +177,10 @@
# 增加 HASH 方法
class Hash
def _config_deep_merge(new_hash)
merger = proc do |key, old_val, new_val|
- Hash === old_val && Hash === new_val ? old_val.merge(new_val, &merger) : new_val
+ (Hash === old_val && Hash === new_val) ? old_val.merge(new_val, &merger) : new_val
end
merge new_hash, &merger
end
end