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
# File 'lib/kimurai/base/saver.rb', line 9

def initialize(path, format:, position: true, append: false)
  raise "SimpleSaver: wrong type of format: #{format}" unless %i[json pretty_json compact_json jsonlines csv].include?(format)

  @path = path
  @format = format
  @format = :json if format == :pretty_json # :pretty_json is now an alias for :json
  @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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kimurai/base/saver.rb', line 21

def save(item)
  @mutex.synchronize do
    if item.is_a?(Array)
      item.each do |it|
        @index += 1
        it[:position] = @index if position

        save_item(it)
      end
    else
      @index += 1
      item[:position] = @index if position

      save_item(item)
    end
  end
end