lib/request_log_analyzer/database/base.rb in request-log-analyzer-1.13.1 vs lib/request_log_analyzer/database/base.rb in request-log-analyzer-1.13.3

- old
+ new

@@ -1,24 +1,23 @@ class RequestLogAnalyzer::Database::Base < ActiveRecord::Base - self.abstract_class = true def <=>(other) if (source_id.nil? && other.source_id.nil?) || (source_comparison = source_id <=> other.source_id) == 0 lineno <=> other.lineno else source_comparison end end - + # Handle format manually, because it is prohibidado in Rails 3.2.1 def format=(arg) - self.attributes[:format] = arg + attributes[:format] = arg end - - def format(arg) - self.attributes[:format] + + def format(_arg) + attributes[:format] end def line_type self.class.name.underscore.gsub(/_line$/, '').to_sym end @@ -30,50 +29,50 @@ klass.table_name = "#{definition.name}_lines" klass.line_definition = definition # Set relations with requests and sources table - klass.belongs_to :request, :class_name => RequestLogAnalyzer::Database::Request.name - klass.belongs_to :source, :class_name => RequestLogAnalyzer::Database::Source.name + klass.belongs_to :request, class_name: RequestLogAnalyzer::Database::Request.name + klass.belongs_to :source, class_name: RequestLogAnalyzer::Database::Source.name # Serialize complex fields into the database - definition.captures.select { |c| c.has_key?(:provides) }.each do |capture| + definition.captures.select { |c| c.key?(:provides) }.each do |capture| klass.send(:serialize, capture[:name], Hash) end - RequestLogAnalyzer::Database::Request.has_many "#{definition.name}_lines".to_sym - RequestLogAnalyzer::Database::Source.has_many "#{definition.name}_lines".to_sym + RequestLogAnalyzer::Database::Request.has_many "#{definition.name}_lines".to_sym + RequestLogAnalyzer::Database::Source.has_many "#{definition.name}_lines".to_sym - return klass + klass end def self.subclass_from_table(table, klass = Class.new(RequestLogAnalyzer::Database::Base)) - raise "Table #{table} not found!" unless database.connection.table_exists?(table) - + fail "Table #{table} not found!" unless database.connection.table_exists?(table) + klass.table_name = table if klass.column_names.include?('request_id') - klass.belongs_to :request, :class_name => RequestLogAnalyzer::Database::Request.name + klass.belongs_to :request, class_name: RequestLogAnalyzer::Database::Request.name RequestLogAnalyzer::Database::Request.has_many table.to_sym end if klass.column_names.include?('source_id') - klass.belongs_to :source, :class_name => RequestLogAnalyzer::Database::Source.name + klass.belongs_to :source, class_name: RequestLogAnalyzer::Database::Source.name RequestLogAnalyzer::Database::Source.has_many table.to_sym end - return klass + klass end def self.drop_table! - database.connection.remove_index(self.table_name, [:source_id]) rescue nil - database.connection.remove_index(self.table_name, [:request_id]) rescue nil - database.connection.drop_table(self.table_name) if database.connection.table_exists?(self.table_name) + database.connection.remove_index(table_name, [:source_id]) rescue nil + database.connection.remove_index(table_name, [:request_id]) rescue nil + database.connection.drop_table(table_name) if database.connection.table_exists?(table_name) end def self.create_table! - raise "No line_definition available to base table schema on!" unless self.line_definition + fail 'No line_definition available to base table schema on!' unless line_definition unless table_exists? database.connection.create_table(table_name.to_sym) do |t| # Default fields @@ -86,38 +85,37 @@ column_name = 'file_format' if column_name == 'format' # Add a field for every capture t.column(column_name, column_type(capture[:type])) # If the capture provides other field as well, create columns for them, too - capture[:provides].each { |field, field_type| t.column(field, column_type(field_type)) } if capture[:provides].kind_of?(Hash) + capture[:provides].each { |field, field_type| t.column(field, column_type(field_type)) } if capture[:provides].is_a?(Hash) end end - + # Add indices to table for more speedy querying - database.connection.add_index(self.table_name.to_sym, [:request_id]) # rescue - database.connection.add_index(self.table_name.to_sym, [:source_id]) # rescue + database.connection.add_index(table_name.to_sym, [:request_id]) # rescue + database.connection.add_index(table_name.to_sym, [:source_id]) # rescue end end # Function to determine the column type for a field # TODO: make more robust / include in file-format definition def self.column_type(type_indicator) case type_indicator - when :eval; :text - when :hash; :text - when :text; :text - when :string; :string - when :sec; :float - when :msec; :float - when :duration; :float - when :float; :float - when :double; :float - when :integer; :integer - when :int; :int - when :timestamp; :datetime - when :datetime; :datetime - when :date; :date + when :eval then :text + when :hash then :text + when :text then :text + when :string then :string + when :sec then :float + when :msec then :float + when :duration then :float + when :float then :float + when :double then :float + when :integer then :integer + when :int then :int + when :timestamp then :datetime + when :datetime then :datetime + when :date then :date else :string end end - -end \ No newline at end of file +end