Module: Ykutils::SpecFileOp

Includes:
DebugUtils, NKFUTIL
Included in:
StructuredText, YamlOp
Defined in:
lib/ykutils/specfileop.rb

Constant Summary

Constants included from NKFUTIL

NKFUTIL::CODE_TO_NAME

Instance Method Summary collapse

Methods included from NKFUTIL

assoc_equal, auto_config_from_inner, auto_config_to_inner, config, convert, get, guess_encoding, #nkf_utf8_file, #puts_sj, #puts_u, set

Methods included from DebugUtils

#clear_d, #d, #d_caller, #d_exit, #d_p, #d_pp, #d_puts, #d_puts_no_empty, #debug_utils_init, #error_exit, #puts_current_method, #puts_d, #puts_no_empty, #w1_puts, #w2_puts

Instance Method Details

#check_data_complement(item_ary, _data) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'lib/ykutils/specfileop.rb', line 129

def check_data_complement(item_ary, _data)
  mes_ary = []
  item_ary.each do |k|
    unless @data[k]
      mes_ary << "Specify #{k} "
      @valid = false
    end
  end
  mes_ary
end

#check_data_complement_print_message(item_ary, data) ⇒ Object



140
141
142
143
144
145
# File 'lib/ykutils/specfileop.rb', line 140

def check_data_complement_print_message(item_ary, data)
  mes_ary = check_data_complement(item_ary, data)
  mes_ary.each do |mes|
    puts mes
  end
end

#dump_yaml_fileobj(obj, fileobj) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/ykutils/specfileop.rb', line 34

def dump_yaml_fileobj(obj, fileobj)
  YAML.dump(obj, fileobj)
rescue StandardError => e
  pp e
  pp e.backtrace
  @valid = false
end

#expand_data(data, separator, except_ary = []) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
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
120
121
# File 'lib/ykutils/specfileop.rb', line 75

def expand_data(data, separator, except_ary = [])
  tm = TreeManager.new
  re = Regexp.new(separator + "([^#{separator[0, 1]}]+)" + separator)

  data.each do |k, v|
    next unless v

    v.scan(re).flatten.each do |it|
      if it
        tm.add(it, k)
        tm.tag(k, it)
      else
        tm.add(nil, k)
      end
    end
  end

  tm.tsort.reverse.each do |k|
    next unless data[k]

    tag = tm.tag(k)
    next unless tag

    tag.each do |it|
      ntag = Regexp.new(separator + it + separator)
      data[k] = data[k].sub(ntag, data[it]) if data[it]
    end
  end

  data.each do |k, v|
    next unless v

    ary = v.scan(re).flatten
    i = 0
    if ary && !ary.empty?
      except_ary.each do |it|
        i += 1 unless ary.index(it)
      end
    end
    if i.positive?
      puts "#{k} fails to exapnd data. value is #{v}"
      @valid = false
    end
  end

  data
end

#load_csv_file(fname) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/ykutils/specfileop.rb', line 147

def load_csv_file(fname)
  ary = []
  begin
    CSV.open(fname, "r") do |row|
      next unless row

      ary << row[0]
    end
  rescue StandardError => e
    pp e
    pp e.backtrace
    @valid = false
  end
  ary
end

#load_csv_file_ex(fname) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/ykutils/specfileop.rb', line 163

def load_csv_file_ex(fname)
  ary = []
  begin
    CSV.open(fname, "r") do |row|
      next unless row

      ary.concat(row)
    end
  rescue StandardError => e
    pp e
    pp e.backtrace
    @valid = false
  end
  ary
end

#load_plain_text_file(fname) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/ykutils/specfileop.rb', line 179

def load_plain_text_file(fname)
  ary = []

  begin
    ary0 = File.readlines(fname)
    NKFUTIL.auto_config_to_inner(ary0.join)

    ary = ary0.compact.collect do |x|
      if x.nil?
        ""
      else
        NKFUTIL.convert(x)
      end
    end
  rescue StandardError => e
    pp e
    pp e.backtrace
    @valid = false

    ary = []
  end
  ary
end

#load_yaml_file(fname) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ykutils/specfileop.rb', line 52

def load_yaml_file(fname)
  @data = nil
  begin
    @data = YAML.load_file(fname)
  rescue StandardError => e
    pp e
    pp e.backtrace
    @valid = false
  end
  @data
end

#make_data_complement(item_ary, data, common) ⇒ Object



123
124
125
126
127
# File 'lib/ykutils/specfileop.rb', line 123

def make_data_complement(item_ary, data, common)
  item_ary.each do |it|
    data[it] = common[it] unless data[it] && (data[it].strip != "")
  end
end

#open_for_write(fname) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/ykutils/specfileop.rb', line 23

def open_for_write(fname)
  begin
    fileobj = File.open(fname, "w")
  rescue StandardError => e
    pp e
    pp e.backtrace
    @valid = false
  end
  fileobj
end

#parse_yaml_file(fname) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/ykutils/specfileop.rb', line 64

def parse_yaml_file(fname)
  begin
    @data = YAML.parse_file(fname)
  rescue StandardError => e
    pp e
    pp e.backtrace
    @valid = false
  end
  @data
end

#save_yaml_file(obj, fname) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/ykutils/specfileop.rb', line 42

def save_yaml_file(obj, fname)
  File.open(fname, "w") do |fileobj|
    YAML.dump(obj, fileobj)
  end
rescue StandardError => e
  pp e
  pp e.backtrace
  @valid = false
end

#valid?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/ykutils/specfileop.rb', line 19

def valid?
  @valid
end