Class: DataClassifier

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

Overview

Creates a Mongo Class mapping csv file

d=DataClassifier.new “/Users/carlobifulco/Dropbox/code/next_gen/hotspot2.csv” d.print_class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name = "test.csv") ⇒ DataClassifier

Returns a new instance of DataClassifier.



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/analyzer.rb', line 395

def initialize file_name="test.csv"

  @header_zip=self.cheap_headers file_name
 
  @file_name=file_name
  @class_name=make_class_name file_name
  #self.instance_variable_set "@#{@class_name}",make_mongo_class(@class_name)
  
  @template="""
class #{@class_name}
include MongoMapper::Document
include DataUtilities
safe
timestamps!
  """
end

Instance Attribute Details

#file_nameObject

Returns the value of attribute file_name.



394
395
396
# File 'lib/analyzer.rb', line 394

def file_name
  @file_name
end

#fs_lineObject

Returns the value of attribute fs_line.



394
395
396
# File 'lib/analyzer.rb', line 394

def fs_line
  @fs_line
end

#headersObject

Returns the value of attribute headers.



394
395
396
# File 'lib/analyzer.rb', line 394

def headers
  @headers
end

#keys_typesObject

Returns the value of attribute keys_types.



394
395
396
# File 'lib/analyzer.rb', line 394

def keys_types
  @keys_types
end

#templateObject

Returns the value of attribute template.



394
395
396
# File 'lib/analyzer.rb', line 394

def template
  @template
end

Instance Method Details

#cheap_headers(file_path) ⇒ Object



414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/analyzer.rb', line 414

def cheap_headers file_path
  index=0
  container=[]
  CSV.foreach(file_path) do |row|
    container<<row
    #puts row
    index+=1
    if index>2 then break end
  end
  container[0]=container[0].map{|x| x.gsub(".","_").downcase}
  zipped=container[0].zip container[1]
  zipped.select {|x| x[0]!="" and x[0]!=nil}
end

#mongo_typesObject



429
430
431
432
433
434
# File 'lib/analyzer.rb', line 429

def mongo_types
  @header_zip.each_with_object({})  do |hz,container|
    key=hz[0].gsub(" ","_")
    container[hz[0].to_sym]=StringToMongo.mongo_type hz[1]
  end
end

prints the class



437
438
439
440
441
442
443
# File 'lib/analyzer.rb', line 437

def print_class
  puts template
  self.mongo_types.sort_by{|k,v| k}.each do |r|
    puts "  key :#{r[0]}, #{r[1]} "
  end
  puts "end"
end