Module: Actir::Config
- Defined in:
- lib/actir/config.rb
Overview
读取yaml文件中配置的内容
@author: Hub
@Date: 2015-1-20
Class Attribute Summary collapse
-
.default_config_dir ⇒ Object
默认配置文件夹路径.
Class Method Summary collapse
-
.file(file_name, config_path = nil) ⇒ Object
配置文件路径.
-
.get(key, config_path = nil) ⇒ Hash
从config文件夹下的配置文件中读取对应的配置项,以hash形式返回.
-
.get_content(filepath) ⇒ OpenStruct
从yaml文件中读取出所有内容.
-
.get_modify_time(file_name, config_path = nil) ⇒ Object
获取配置文件的修改时间(只精确到日期,不考虑具体时间) 返回String,格式为:04-27…
-
.is_same_day?(file_name, config_path = nil) ⇒ Boolean
判断配置文件的上一次修改时间和当前时间是否一样.
-
.lock(file_name, config_path = nil) ⇒ Object
多进程操作文件时加锁.
-
.set(key, value, config_path = nil) ⇒ Object
更新配置文件中的key对应的value值.
Class Attribute Details
.default_config_dir ⇒ Object
默认配置文件夹路径
176 177 178 |
# File 'lib/actir/config.rb', line 176 def default_config_dir @default_config_dir end |
Class Method Details
.file(file_name, config_path = nil) ⇒ Object
配置文件路径
170 171 172 173 |
# File 'lib/actir/config.rb', line 170 def file(file_name, config_path = nil) config_dir = (config_path == nil) ? default_config_dir : config_path File.(File.join(config_dir, "/#{file_name}.yaml"), __FILE__) end |
.get(key, config_path = nil) ⇒ Hash
从config文件夹下的配置文件中读取对应的配置项,以hash形式返回
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/actir/config.rb', line 56 def get(key, config_path = nil) #按照点分割字符串 key_array = key.split(".") #先取出数组中的第一个元素当做配置文件名称,并从数组中移除此元素 file_name = key_array.shift #再取出第二个元素,指定配置项,并移除 cfg_name = key_array.shift hash = {} #加载yaml配置文件,加锁 lock(file_name, config_path) do hash = cfg_name ? load_file(file(file_name, config_path))[cfg_name] : load_file(file(file_name, config_path)) end #遍历key数组 until key_array.empty? do key = key_array.shift hash = hash[key] end hash end |
.get_content(filepath) ⇒ OpenStruct
从yaml文件中读取出所有内容
38 39 40 41 42 43 |
# File 'lib/actir/config.rb', line 38 def get_content(filepath) f ||= filepath if valid?(filepath) File.open(f) {|handle| @hash_content = YAML.load(handle)} content = OpenStruct.new(@hash_content) content end |
.get_modify_time(file_name, config_path = nil) ⇒ Object
获取配置文件的修改时间(只精确到日期,不考虑具体时间) 返回String,格式为:04-27… 12-29
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/actir/config.rb', line 140 def get_modify_time(file_name, config_path = nil) sh_str = "ls -l " + file(file_name, config_path) + " | awk '{print $6 \"-\" $7}'" stat_str = `#{sh_str}` #从中取出月份和日期 stat_str =~ /(\d+).*\-(\d+)/ month = $1 day = $2 #若果是1-9,则在前面加个0 if month == "" || day == "" || month == nil || day == nil return "" else month = "0" + month if month.to_i <= 9 && month.to_i >= 1 day = "0" + day if day.to_i <= 9 && day.to_i >= 1 month + "-" + day end end |
.is_same_day?(file_name, config_path = nil) ⇒ Boolean
判断配置文件的上一次修改时间和当前时间是否一样
132 133 134 135 136 |
# File 'lib/actir/config.rb', line 132 def is_same_day?(file_name, config_path = nil) now_date = Time.new.strftime("%m-%d") modify_date = get_modify_time(file_name, config_path) now_date == modify_date end |
.lock(file_name, config_path = nil) ⇒ Object
多进程操作文件时加锁
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/actir/config.rb', line 158 def lock(file_name, config_path = nil) File.open(file(file_name, config_path), 'r') do |f| begin f.flock File::LOCK_EX yield ensure f.flock File::LOCK_UN end end end |
.set(key, value, config_path = nil) ⇒ Object
更新配置文件中的key对应的value值
改文件加锁,解决多进程同时写文件的问题
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/actir/config.rb', line 87 def set(key, value, config_path = nil) #按照点分割字符串 key_array = key.split(".") #先取出数组中的第一个元素当做配置文件名称,并从数组中移除此元素 file_name = key_array.shift cfg_str = key_array.shift old_value = "" lock(file_name, config_path) do #先读出所有的内容 str_array = IO.readlines(file(file_name, config_path)) str_array.each_with_index do |line, index| if ( cfg_str != "" && line =~ /(\s*#{cfg_str}\:\s*)(.*)/ ) cfg_key = $1 old_value = $2 #找对了父节点,继续取下一个 if key_array.size > 0 cfg_str = key_array.shift else #只剩最后一个配置项了,说明找到了唯一的配置项,修改之 replace_str = cfg_key.chomp + value str_array[index] = replace_str cfg_str = "" end end end config_file = File.open(file(file_name, config_path), "w") str_array.each do |line| config_file.puts line end config_file.close end puts "Already set [" + key + "]'s value form " + old_value + " into " + value end |