Skip to content

Commit cda7483

Browse files
committed
feat: Add Destinations::JsonArray.fields method
1 parent ad62dd0 commit cda7483

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

lib/kiba/extend/destinations/json_array.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,31 @@ def initialize(filename:)
3232
@filename = filename
3333
ensure_dir
3434
@json = []
35-
@file = File.open(filename, "w")
35+
end
36+
37+
# @return [Array<Symbol>]
38+
def fields
39+
return [] unless File.exist?(filename)
40+
41+
JSON.parse(File.read(filename))
42+
.map(&:keys)
43+
.flatten
44+
.uniq
3645
end
3746

3847
# @private
3948
def write(row)
40-
@json << row
49+
json << row
4150
end
4251

4352
# @private
4453
def close
45-
@file << @json.to_json
46-
@file.close
54+
File.open(filename, "w") { |f| f << json.to_json }
4755
end
4856

4957
private
5058

51-
attr_reader :filename
59+
attr_reader :filename, :json
5260
end
5361
end
5462
end

0 commit comments

Comments
 (0)