Class: Kiba::Extend::Destinations::JsonArray

Inherits:
Object
  • Object
show all
Extended by:
Destinationable
Defined in:
lib/kiba/extend/destinations/json_array.rb

Overview

Writes each row as a valid JSON object that is an element in a JSON array

This is simliar to the idea of, but not technically compliant with, JSON Lines

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Destinationable

as_source_class, is_destination?, special_options

Methods included from Registry::Fileable

#default_args, #default_file_options, #labeled_options, #options_key, #path_key, #requires_path?

Constructor Details

#initialize(filename:) ⇒ JsonArray

Returns a new instance of JsonArray.

Parameters:

  • filename (String)

    path for writing JSON file



43
44
45
46
# File 'lib/kiba/extend/destinations/json_array.rb', line 43

def initialize(filename:)
  @json = []
  @file = File.open(filename, "w")
end

Class Method Details

.as_source_classObject



17
18
19
# File 'lib/kiba/extend/destinations/json_array.rb', line 17

def as_source_class
  nil
end

.default_file_optionsObject



21
22
23
# File 'lib/kiba/extend/destinations/json_array.rb', line 21

def default_file_options
  nil
end

.options_keyObject



25
26
27
# File 'lib/kiba/extend/destinations/json_array.rb', line 25

def options_key
  nil
end

.path_keyObject



29
30
31
# File 'lib/kiba/extend/destinations/json_array.rb', line 29

def path_key
  :filename
end

.requires_path?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/kiba/extend/destinations/json_array.rb', line 33

def requires_path?
  true
end

.special_optionsObject



37
38
39
# File 'lib/kiba/extend/destinations/json_array.rb', line 37

def special_options
  []
end

Instance Method Details

#closeObject



54
55
56
57
# File 'lib/kiba/extend/destinations/json_array.rb', line 54

def close
  @file << @json.to_json
  @file.close
end

#write(row) ⇒ Object



49
50
51
# File 'lib/kiba/extend/destinations/json_array.rb', line 49

def write(row)
  @json << row
end