class Kimurai::Base::Saver

Attributes

append[R]
format[R]
path[R]
position[R]

Public Class Methods

new(path, format:, position: true, append: false) click to toggle source
# File lib/kimurai/base/saver.rb, line 9
def initialize(path, format:, position: true, append: false)
  unless %i(json pretty_json jsonlines csv).include?(format)
    raise "SimpleSaver: wrong type of format: #{format}"
  end

  @path = path
  @format = format
  @position = position
  @index = 0
  @append = append
  @mutex = Mutex.new
end

Public Instance Methods

save(item) click to toggle source
# File lib/kimurai/base/saver.rb, line 22
def save(item)
  @mutex.synchronize do
    @index += 1
    item[:position] = @index if position

    case format
    when :json
      save_to_json(item)
    when :pretty_json
      save_to_pretty_json(item)
    when :jsonlines
      save_to_jsonlines(item)
    when :csv
      save_to_csv(item)
    end
  end
end

Private Instance Methods

flatten_hash(hash) click to toggle source
# File lib/kimurai/base/saver.rb, line 93
def flatten_hash(hash)
  hash.each_with_object({}) do |(k, v), h|
    if v.is_a? Hash
      flatten_hash(v).map { |h_k, h_v| h["#{k}.#{h_k}"] = h_v }
    else
      h[k&.to_s] = v
    end
  end
end
save_to_csv(item) click to toggle source
# File lib/kimurai/base/saver.rb, line 78
def save_to_csv(item)
  data = flatten_hash(item)

  if @index > 1 || append && File.exists?(path)
    CSV.open(path, "a+", force_quotes: true) do |csv|
      csv << data.values
    end
  else
    CSV.open(path, "w", force_quotes: true) do |csv|
      csv << data.keys
      csv << data.values
    end
  end
end
save_to_json(item) click to toggle source
# File lib/kimurai/base/saver.rb, line 42
def save_to_json(item)
  data = JSON.generate([item])

  if @index > 1 || append && File.exists?(path)
    file_content = File.read(path).sub(/\}\]\Z/, "\}\,")
    File.open(path, "w") do |f|
      f.write(file_content + data.sub(/\A\[/, ""))
    end
  else
    File.open(path, "w") { |f| f.write(data) }
  end
end
save_to_jsonlines(item) click to toggle source
# File lib/kimurai/base/saver.rb, line 68
def save_to_jsonlines(item)
  data = JSON.generate(item)

  if @index > 1 || append && File.exists?(path)
    File.open(path, "a") { |file| file.write("\n" + data) }
  else
    File.open(path, "w") { |file| file.write(data) }
  end
end
save_to_pretty_json(item) click to toggle source
# File lib/kimurai/base/saver.rb, line 55
def save_to_pretty_json(item)
  data = JSON.pretty_generate([item])

  if @index > 1 || append && File.exists?(path)
    file_content = File.read(path).sub(/\}\n\]\Z/, "\}\,\n")
    File.open(path, "w") do |f|
      f.write(file_content + data.sub(/\A\[\n/, ""))
    end
  else
    File.open(path, "w") { |f| f.write(data) }
  end
end