Class: Insight::Parser

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

Instance Method Summary collapse

Constructor Details

#initialize(config, db) ⇒ Parser

Returns a new instance of Parser.



72
73
74
75
76
77
78
79
80
81
# File 'lib/insight.rb', line 72

def initialize(config, db)
  @rules = []
  @config = Psych.load( File.open(config)  )

  initdb(db)

  @config.each_key{|key|
    @rules.push(Rule.new(@db, key, @config[key]))
  }
end

Instance Method Details

#initdb(db) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/insight.rb', line 64

def initdb(db)
  begin
    @db = SQLite3::Database.new db
  rescue SQLite3::Exception => e
    puts "Exception opening #{db}"
    puts e
  end
end

#parse(file) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/insight.rb', line 82

def parse(file)
  @db.execute "BEGIN TRANSACTION;"

  @rulesToApply = []
  @rules.each{|rule|
    if(rule.applies?(file))
      @rulesToApply.push(rule)
    end
  } 

  File.open(file, 'r+') do |file|
    file.each_line { |line| 
      @rulesToApply.each{|rule|  
        rule.line(line)
      }
    }
  end

  @db.execute("COMMIT TRANSACTION;")
end