lib/ykutils/yamlop.rb in ykutils-0.1.7 vs lib/ykutils/yamlop.rb in ykutils-0.1.8

- old
+ new

@@ -10,11 +10,11 @@ include DataStructOp TEST_ID_NO_VALUE = 0 TEST_ID_LISTUP_ALL = 1 - def initialize(opts = {}, _argv = [], _debug = false) + def initialize(opts = {}, _argv = [], _debug: false) @fname = nil @check_flag = opts["check"] @ifname = opts["input_file"] @ofname = opts["output_file"] @reform_flag = opts["reform"] @@ -28,11 +28,11 @@ end def exec if @reform_flag reform - elsif @test_id > 0 + elsif @test_id.positive? case @test_id when TEST_ID_LISTUP_ALL test_listup end end @@ -102,25 +102,24 @@ @yamlstext.sort_by(@pstext) end def import(fname) ret = false - if File.file?(fname) and File.readable?(fname) - @ptext = load_plain_text_file(fname).collect { |it| it.chomp } + if File.file?(fname) && File.readable?(fname) + @ptext = load_plain_text_file(fname).collect(&:chomp) ret = false end ret end def importex(fname) ret = import(fname) if ret @ptext_hash = {} - i = 0 @ptext_hash = make_hash(@ptext, 0) do |l, i| - if l !~ /^\s/ and l !~ /^==/ - key, vale = l.split(":") + if l !~ (/^\s/) && l !~ (/^==/) + key, = l.split(":") [key, { "CONTENT" => l, "INDEX" => i }] end end ret = false end @@ -134,18 +133,18 @@ def listup_host @ptext.select { |l| l !~ /^\s/ and l !~ /^=/ and l !~ /^#/ } end def listup_domain - @ptext.select { |l| l =~ /^==/ } + @ptext.grep(/^==/) end def output_yaml(obj, fname) File.open(fname, "w") do |file| YAML.dump(obj, file) end - rescue StandardError => e + rescue YkutilsError => e pp e pp e.backtrace @valid = false end @@ -163,23 +162,23 @@ def dump_to_file(fname) File.open(fname, "w") do |file| @yamlstext.dump(file) end - rescue StandardError => e + rescue YkutilsError => e pp e pp e.backtrace @valid = false end def dump_ptext_to_file(fname) - file = File.open(fname, "w") do |file| + File.open(fname, "w") do |file| @ptext.each do |l| - file.write(l + "\n") + file.write("#{l}\n") end end - rescue StandardError => e + rescue YkutilsdError => e pp e pp e.backtrace @valid = false end @@ -188,28 +187,28 @@ @ptext_hash[key]["CONTENT"] = value end def reform ret = importex(@ifname) - if ret - ary = listup_host.collect do |it| - key, value = it.split(":") - key - end - str = ary.join(" , ") - new_host_list = "=-HOST_LIST: [#{str}]" - ary2 = listup_domain.collect do |it| - key, value = it.split(":") - key - end - str2 = ary2.join(" , ") - new_domain_list = "=-DOMAIN_LIST: [#{str2}]" - pp "==new_host_list" - pp new_host_list - exchange_ptext("=-HOST_LIST", new_host_list) - exchange_ptext("=-DOMAIN_LIST", new_domain_list) - dump_ptext_to_file(@ofname) + return unless ret + + ary = listup_host.collect do |it| + key, = it.split(":") + key end + str = ary.join(" , ") + new_host_list = "=-HOST_LIST: [#{str}]" + ary2 = listup_domain.collect do |it| + key, = it.split(":") + key + end + str2 = ary2.join(" , ") + new_domain_list = "=-DOMAIN_LIST: [#{str2}]" + pp "==new_host_list" + pp new_host_list + exchange_ptext("=-HOST_LIST", new_host_list) + exchange_ptext("=-DOMAIN_LIST", new_domain_list) + dump_ptext_to_file(@ofname) end def extract_value(ary) ary.collect do |x| x2 = x