Class: Kimurai::Base::Saver

Inherits:
Object
  • Object
show all
Defined in:
lib/kimurai/base/saver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, format:, position: true, append: false) ⇒ Saver

Returns a new instance of Saver.



9
10
11
12
13
14
15
16
17
18
19
20
# 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

Instance Attribute Details

#appendObject (readonly)

Returns the value of attribute append.



7
8
9
# File 'lib/kimurai/base/saver.rb', line 7

def append
  @append
end

#formatObject (readonly)

Returns the value of attribute format.



7
8
9
# File 'lib/kimurai/base/saver.rb', line 7

def format
  @format
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/kimurai/base/saver.rb', line 7

def path
  @path
end

#positionObject (readonly)

Returns the value of attribute position.



7
8
9
# File 'lib/kimurai/base/saver.rb', line 7

def position
  @position
end

Instance Method Details

#save(item) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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