Class: Traject::JsonWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/traject/json_writer.rb

Overview

A writer for Traject::Indexer, that just writes out all the output as Json. It’s newline delimitted json, but right now no checks to make sure there is no internal newlines as whitespace in the json. TODO, add that.

Not currently thread-safe (have to make sure whole object and newline get written without context switch. Can be made so.)

You can force pretty-printing with setting ‘json_writer.pretty_print’ of boolean true or string ‘true’. Useful mostly for human checking of output.

Output will be sent to settings string path, or else settings (ruby IO object), or else stdout.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argSettings) ⇒ JsonWriter

Returns a new instance of JsonWriter.



19
20
21
# File 'lib/traject/json_writer.rb', line 19

def initialize(argSettings)
  @settings = argSettings
end

Instance Attribute Details

#settingsObject (readonly)

Returns the value of attribute settings.



17
18
19
# File 'lib/traject/json_writer.rb', line 17

def settings
  @settings
end

Instance Method Details

#closeObject



47
48
49
# File 'lib/traject/json_writer.rb', line 47

def close 
  @output_file.close unless (@output_file.nil? || @output_file.tty?)
end

#output_fileObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/traject/json_writer.rb', line 33

def output_file
  unless defined? @output_file
    @output_file = 
      if settings["output_file"]
        File.open(settings["output_file"])
      elsif settings["output_stream"]
        settings["output_stream"]
      else
        $stdout
      end
  end
  return @output_file
end

#put(hash) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/traject/json_writer.rb', line 23

def put(hash)
  serialized = 
    if settings["json_writer.pretty_print"]
      JSON.pretty_generate(hash)
    else
      JSON.generate(hash)
    end
  output_file.puts(serialized)
end