Class: CSV::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/analyzer.rb,
lib/analyzer.rb,
lib/analyzer.rb

Overview

A modified CSV table class pp

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#data_classifierObject

Returns the value of attribute data_classifier.



126
127
128
# File 'lib/analyzer.rb', line 126

def data_classifier
  @data_classifier
end

#file_pathObject

Returns the value of attribute file_path.



126
127
128
# File 'lib/analyzer.rb', line 126

def file_path
  @file_path
end

Instance Method Details

#clean_names(names_col, diagnosis_col) ⇒ Object



148
149
150
151
152
# File 'lib/analyzer.rb', line 148

def clean_names names_col, diagnosis_col
  self.each do |row|
    row[diagnosis_col]=names_cleaner row[diagnosis_col],row[names_col]
  end
end

#decrypt(col_name) ⇒ Object



141
142
143
144
145
146
# File 'lib/analyzer.rb', line 141

def decrypt col_name
  if $e==nil then String.set_encryption end
  self.each do |row|
    row[col_name]=row[col_name].to_s.decrypt
  end
end

#encrypt(col_name) ⇒ Object



128
129
130
131
132
133
# File 'lib/analyzer.rb', line 128

def encrypt col_name
  if $e==nil then String.set_encryption end
  self.each do |row|
    row[col_name]=row[col_name].to_s.encrypt
  end
end

#encrypt_col_names(col_names_array) ⇒ Object



135
136
137
138
139
# File 'lib/analyzer.rb', line 135

def encrypt_col_names col_names_array
  col_names_array.each do |col_name|
    self.encrypt col_name
  end
end

#find_rows(col_name, regex, decrypt = false) ⇒ Object

z.find_rows :diagnosis_text, /.(Td)./i



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/analyzer.rb', line 170

def find_rows col_name, regex, decrypt=false
  new_table=[]<<self.headers
  if decrypt then self.decrypt col_name end
  self[col_name].each_with_index do |r,i|
    #puts r
    if r.match regex
      puts i
      new_table<<self[i]
    end
  end
  new_table.to_table
end

#md5(col_name) ⇒ Object



154
155
156
157
158
# File 'lib/analyzer.rb', line 154

def md5 col_name
  self.each do |row|
    row[col_name]=row[col_name].to_s.md5
  end
end

#mongo_codeObject Also known as: print_mongo



195
196
197
# File 'lib/analyzer.rb', line 195

def mongo_code
  self.data_classifier.print_class
end

#ppObject



656
657
658
659
660
661
# File 'lib/analyzer.rb', line 656

def pp
  puts self.headers.to_csv
  self.each do |r|
    puts r.to_csv
  end
end

#remove_duplicate(col) ⇒ Object

remove duplicates

takes col to search through



271
272
273
274
275
276
277
278
279
280
281
# File 'lib/analyzer.rb', line 271

def remove_duplicate  col
  col_all=self[col]
  self.each do |row|
    entry=row[col]
    if col_all.count(entry) >= 2
      # deletes entries from index and from table
      self.delete col_all.rindex(entry)
      col_all.delete_at col_all.rindex(entry)
    end
  end
end

#save(file_name) ⇒ Object



160
161
162
163
164
165
166
167
# File 'lib/analyzer.rb', line 160

def save file_name
  CSV.open(file_name, "wb") do |csv|
    csv << self.headers
    self.each do |line|
      csv << line.fields
    end
  end
end

#to_mongo(mongo_class) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/analyzer.rb', line 183

def to_mongo mongo_class
  self.each_with_index do |row,i|
    m=mongo_class.new
    self.headers.each do |header|
      puts "working on #{header} in row #{i}"
      m[header]=row[header]
    end
    m.save
    puts "#{i}: #{mongo_class.count}"
  end
end