Class: EnrollmentDataReader

Inherits:
Object
  • Object
show all
Defined in:
lib/udise_school_report_reader/enrollment_data_reader.rb

Constant Summary collapse

ALL_CATEGORIES =
[
  SOCIAL_CATEGORIES = [
    { key: 'gen', label: 'Gen' },
    { key: 'sc', label: 'SC' },
    { key: 'st', label: 'ST' },
    { key: 'obc', label: 'OBC' }
  ].freeze,
  RELIGION_CATEGORIES = [
    { key: 'musl', label: 'Musl' },
    { key: 'chris', label: 'Chris' },
    { key: 'sikh', label: 'Sikh' },
    { key: 'budd', label: 'Budd' },
    { key: 'parsi', label: 'Parsi' },
    { key: 'jain', label: 'Jain' },
    { key: 'others', label: 'Others' }
  ].freeze,
  OTHER_CATEGORIES = [
    { key: 'aadh', label: 'Aadh' },
    { key: 'bpl', label: 'BPL' },
    { key: 'rept', label: 'Rept' },
    { key: 'cwsn', label: 'CWSN' }
  ].freeze,
  AGE_CATEGORIES = (3..22).map do |age|
    { key: "age_#{age}", label: age == 3 ? '>3' : age.to_s }
  end.freeze,
].flatten.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(csv_path) ⇒ EnrollmentDataReader

Returns a new instance of EnrollmentDataReader.



31
32
33
34
35
# File 'lib/udise_school_report_reader/enrollment_data_reader.rb', line 31

def initialize(csv_path)
  @csv_path = csv_path
  @x_cutoff = 0
  @category_y_coords = {}
end

Class Method Details

.read(csv_path) ⇒ Object



29
# File 'lib/udise_school_report_reader/enrollment_data_reader.rb', line 29

def self.read(csv_path) = new(csv_path).read

Instance Method Details

#readObject



37
38
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/udise_school_report_reader/enrollment_data_reader.rb', line 37

def read
  # Initialize arrays for different row types
  grade_rows = []
  bg_rows = []
  category_rows = {}
  
  ALL_CATEGORIES.each do |category|
    category_rows[category[:key]] = []
  end

  # First pass to collect y-coordinates for categories
  CSV.foreach(@csv_path, headers: true) do |row|
    if row['page'] == '2' && (row['rect_x'].to_f - 27.0).abs < 5.0
      ALL_CATEGORIES.each do |category|
        if row['text'].downcase == category[:label].downcase
          @category_y_coords[category[:key]] = row['rect_y'].to_f
        end
      end
    end
  end

  CSV.foreach(@csv_path, headers: true) do |row|
    if row['page'] == '2'
      if row['text'] == "Total" && row['rect_y'].to_f == 778.0
        @x_cutoff = row['rect_x'].to_f
      end

      if ['Pre-Pr', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII'].include?(row['text'])
        if row['text_y'].to_f == 780.0
          grade_rows << row
        end
      elsif ['B', 'G'].include?(row['text'])
        if row['text_y'].to_f == 768.0
          bg_rows << row
        end
      elsif row['text'] =~ /^\d+$/
        y_coord = row['rect_y'].to_f
        ALL_CATEGORIES.each do |category|
          if @category_y_coords[category[:key]] && (y_coord - @category_y_coords[category[:key]]).abs < 5.0
            category_rows[category[:key]] << row
          end
        end
      end
    end
  end

  return nil if grade_rows.empty?

  # Sort and filter rows
  [grade_rows, bg_rows].each do |rows|
    rows.sort_by! { |row| row['text_x'].to_f }
    rows.reject! { |row| row['text_x'].to_f >= @x_cutoff }
  end

  category_rows.values.each do |rows|
    rows.sort_by! { |row| row['text_x'].to_f }
    rows.reject! { |row| row['text_x'].to_f >= @x_cutoff }
  end

  # Group B,G pairs
  bg_pairs = bg_rows.each_slice(2).map do |b, g|
    x_mid = (b['text_x'].to_f + g['text_x'].to_f) / 2
    [x_mid, [b, g]]
  end.to_h

  # Match numbers to pairs
  result = {
    grade_rows: grade_rows,
    bg_pairs: bg_pairs
  }

  ALL_CATEGORIES.each do |category|
    result["#{category[:key]}_numbers".to_sym] = match_numbers_to_pairs(category_rows[category[:key]], bg_pairs)
  end

  result
end