Class: Workarea::DataFile::Json

Inherits:
Format
  • Object
show all
Defined in:
app/models/workarea/data_file/json.rb

Instance Attribute Summary

Attributes inherited from Format

#operation

Instance Method Summary collapse

Methods inherited from Format

#assign_password, #clean_ignored_fields, #initialize, #model_class_for, #serialize, #slug, #unit

Constructor Details

This class inherits a constructor from Workarea::DataFile::Format

Instance Method Details

#export!Object



19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/workarea/data_file/json.rb', line 19

def export!
  tempfile.write("[\n")

  models.each_with_index do |model, i|
    tempfile.write(JSON.pretty_generate(serialize_model(model)))
    tempfile.write(',') unless i == models.count - 1
    tempfile.write("\n")
  end

  tempfile.write("]\n")
end

#import!Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/models/workarea/data_file/json.rb', line 4

def import!
  index = 1
  file_stream = File.open(file.path, 'r')
  streamer = ::Json::Streamer.parser(file_io: file_stream)

  streamer.get(nesting_level: 1) do |attrs|
    instance = find_updated_model_for(attrs)
    assign_password(instance, attrs['password'])
    instance.save

    log(index, instance)
    index += 1
  end
end

#serialize_model(model) ⇒ Object



31
32
33
34
35
36
37
# File 'app/models/workarea/data_file/json.rb', line 31

def serialize_model(model)
  clean_ignored_fields(
    model.as_json.reverse_merge(
      _type: model.class.name
    )
  )
end