Class: BatchLoad::Import::Descriptors::QualitativeInterpreter

Inherits:
BatchLoad::Import show all
Defined in:
lib/batch_load/import/descriptors/qualitative_interpreter.rb

Instance Attribute Summary collapse

Attributes inherited from BatchLoad::Import

#create_attempted, #errors, #file, #file_errors, #import_level, #processed, #processed_rows, #project, #project_id, #successful_rows, #total_data_lines, #total_lines, #user, #user_header_map, #user_id

Instance Method Summary collapse

Methods inherited from BatchLoad::Import

#all_objects, #create, #create_attempted?, #import_level_ok?, #line_strict_level_ok?, #processed?, #ready_to_create?, #save_order, #sorted_processed_rows, #strict_level_ok?, #total_records_created, #user_map, #valid?, #valid_objects, #warn_level_ok?

Constructor Details

#initialize(**args) ⇒ QualitativeInterpreter

Returns a new instance of QualitativeInterpreter.

Parameters:

  • args (Hash)


9
10
11
12
13
# File 'lib/batch_load/import/descriptors/qualitative_interpreter.rb', line 9

def initialize(**args)
  @descriptors = {}
  @character_states = {}
  super(**args)
end

Instance Attribute Details

#character_statesObject

Returns the value of attribute character_states.



6
7
8
# File 'lib/batch_load/import/descriptors/qualitative_interpreter.rb', line 6

def character_states
  @character_states
end

#descriptorsObject

needed?



5
6
7
# File 'lib/batch_load/import/descriptors/qualitative_interpreter.rb', line 5

def descriptors
  @descriptors
end

Instance Method Details

#buildObject



82
83
84
85
86
87
# File 'lib/batch_load/import/descriptors/qualitative_interpreter.rb', line 82

def build
  if valid?
    build_descriptors
    @processed = true
  end
end

#build_descriptorsInteger

Returns:

  • (Integer)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/batch_load/import/descriptors/qualitative_interpreter.rb', line 39

def build_descriptors
  i = 0 
  csv.each do |row|
    parse_result = BatchLoad::RowParse.new

    parse_result.objects[:descriptor] = []
    parse_result.objects[:character_states] = []

    descriptor = nil
    descriptor_attributes = {
      name: row.shift,
      project_id: project_id,
      by: user_id
    }

    # THere are no validatinos on duplicate Descriptor::Qualitative yet
    # descriptor_match = Digest::SHA256.digest(descriptor_attributes.to_s)
    # descriptor = Descriptor::Qualitative.new(descriptor_attributes) # if descriptor.blank?

    parse_result.objects[:descriptor].push(descriptor)

    # build character states
    row.each_with_index do |n, i|
      break if n.blank?
      parse_result.objects[:character_states].push CharacterState.new(
        descriptor: descriptor,
        name: n,
        label: i,
        project_id: project_id,
        by: user_id
      )
    end

    # attach the results to the row
    @processed_rows[i] = parse_result

    i += 1
  end


  @total_lines = i - 1
end

#csvcsv?

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/batch_load/import/descriptors/qualitative_interpreter.rb', line 16

def csv
  begin
    @csv ||= CSV.parse(
      @file.tempfile.read.force_encoding('utf-8'), # force encoding is likely a very bad idea, but instructinos say "utf-8"
      headers: false,
      col_sep: "\t",
      encoding: 'UTF-8',
      skip_blanks: true)

  rescue Encoding::UndefinedConversionError => e

  rescue ArgumentError => e
    @processed = false
    @file_errors.push("error converting file. #{e}")
    return nil
  rescue CSV::MalformedCSVError => e
    @processed = false
    @file_errors.push("error converting file. #{e}")
    return nil
  end
end