Class: BatchFileLoad::Import

Inherits:
Object
  • Object
show all
Defined in:
lib/batch_file_load/import.rb

Direct Known Subclasses

Import::Otus::SimpleInterpreter

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Ignored) initialize(project_id: nil, user_id: nil, files: nil, import_level: :warn)

Parameters:

  • project_id (Integer)
  • user_id (Integer)
  • files (Array)
  • import_level (Symbol)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/batch_file_load/import.rb', line 15

def initialize(project_id: nil, user_id: nil, files: nil, import_level: :warn)
  @project_id = project_id
  @user = User.find(user_id)
  @files = files
  @import_level = import_level

  @processed = false

  # WARNING: Beware of files with the same name, the content within them may be different
  # thus why filenames can NOT be used as keys for this reason
  # We also can't modify filenames by appending numbers at the end or whatever to
  # fix the previous issue in case the filename contains metadata for the file
  # in which this would break the metadata
  @processed_files = { names: [], objects: [] }

  @filenames = []
  @file_contents = []

  @files.each do |file|
    @filenames.push(file.original_filename)

    # WARNING: Once you call ".tempfile.read.force_encoding('utf-8')" on a tempfile as shown below,
    # the next time you call ".tempfile.read.force_encoding('utf-8')" on the same tempfile
    # an empty string will be returned!
    @file_contents.push(file.tempfile.read.force_encoding('utf-8'))
  end

  @errors = []
  @file_errors = []

  @total_records_created = 0

  build
end

Instance Attribute Details

- (Object) errors (readonly)

Returns the value of attribute errors



6
7
8
# File 'lib/batch_file_load/import.rb', line 6

def errors
  @errors
end

- (Object) file_contents (readonly)

Returns the value of attribute file_contents



9
10
11
# File 'lib/batch_file_load/import.rb', line 9

def file_contents
  @file_contents
end

- (Object) file_errors (readonly)

Returns the value of attribute file_errors



7
8
9
# File 'lib/batch_file_load/import.rb', line 7

def file_errors
  @file_errors
end

- (Object) filenames (readonly)

Returns the value of attribute filenames



8
9
10
# File 'lib/batch_file_load/import.rb', line 8

def filenames
  @filenames
end

- (Object) processed (readonly)

Returns the value of attribute processed



5
6
7
# File 'lib/batch_file_load/import.rb', line 5

def processed
  @processed
end

- (Object) processed_files (readonly)

Returns the value of attribute processed_files



4
5
6
# File 'lib/batch_file_load/import.rb', line 4

def processed_files
  @processed_files
end

Instance Method Details

- (Ignored) build (protected)

Returns:

  • (Ignored)


123
124
125
# File 'lib/batch_file_load/import.rb', line 123

def build
  raise 'This method must be provided in the respective subclass.'
end

- (Ignored) create

Returns:

  • (Ignored)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/batch_file_load/import.rb', line 52

def create
  if ready_to_create?
    @total_records_created = 0

    get_all_objects.each do |object|
      if object.save
        @total_records_created += 1
      end
    end
  else
    @errors << "Import level #{@import_level} has prevented creation." unless import_level_ok?
    @errors << 'One of project_id, user_id or files has not been provided.' unless valid?
  end
end

- (Boolean) file_object_strict_level_ok?

Returns:

  • (Boolean)


115
116
117
# File 'lib/batch_file_load/import.rb', line 115

def file_object_strict_level_ok?
  file_strict_level_ok? && object_strict_level_ok?
end

- (Boolean) file_strict_level_ok?

Returns:

  • (Boolean)


99
100
101
102
103
# File 'lib/batch_file_load/import.rb', line 99

def file_strict_level_ok?
  @filenames.each_with_index do |filename, index|
    return false if filename != @processed_files[:names][index] || @processed_files[:objects][index].empty?
  end
end

- (Array) get_all_objects (private)

Returns:

  • (Array)


154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/batch_file_load/import.rb', line 154

def get_all_objects
  all_objects = []

  @processed_files[:objects].each do |hash|
    hash.each_value do |objects|
      objects.each do |object|
        all_objects.push(object)
      end
    end
  end

  all_objects
end

- (Boolean) import_level_ok? (private)

Returns:

  • (Boolean)


137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/batch_file_load/import.rb', line 137

def import_level_ok?
  case @import_level.to_sym
    when :warn
      warn_level_ok?
    when :file_strict
      file_strict_level_ok?
    when :object_strict
      object_strict_level_ok?
    when :file_object_strict
      file_object_strict_level_ok?
    else
      false
  end
end

- (Boolean) object_strict_level_ok?

Returns:

  • (Boolean)


107
108
109
110
111
# File 'lib/batch_file_load/import.rb', line 107

def object_strict_level_ok?
  get_all_objects.each do |object|
    return false if !object.valid?
  end
end

- (Boolean) ready_to_create? (private)

Returns:

  • (Boolean)


131
132
133
# File 'lib/batch_file_load/import.rb', line 131

def ready_to_create?
  valid? && @processed && import_level_ok?
end

- (Integer) total_files_processed

Returns:

  • (Integer)


81
82
83
# File 'lib/batch_file_load/import.rb', line 81

def total_files_processed
  @processed_files[:names].length
end

- (Integer) total_records_created

Returns:

  • (Integer)


69
70
71
# File 'lib/batch_file_load/import.rb', line 69

def total_records_created
  @total_records_created
end

- (Integer) total_records_processed

Returns:

  • (Integer)


75
76
77
# File 'lib/batch_file_load/import.rb', line 75

def total_records_processed
  get_all_objects.length
end

- (Boolean) valid?

Returns:

  • (Boolean)


87
88
89
# File 'lib/batch_file_load/import.rb', line 87

def valid?
  @project_id && @user && @filenames && @file_contents
end

- (Boolean) warn_level_ok?

Returns:

  • (Boolean)


93
94
95
# File 'lib/batch_file_load/import.rb', line 93

def warn_level_ok?
  true
end