Class: RequestLogAnalyzer::Database
- Inherits:
-
Object
- Object
- RequestLogAnalyzer::Database
- Includes:
- Connection
- Defined in:
- lib/request_log_analyzer/database.rb
Defined Under Namespace
Modules: Connection Classes: Base, Request, Source, Warning
Instance Attribute Summary collapse
-
#file_format ⇒ Object
Returns the value of attribute file_format.
-
#line_classes ⇒ Object
readonly
Returns the value of attribute line_classes.
Instance Method Summary collapse
-
#create_database_schema! ⇒ Object
Creates the database schema and related ActiveRecord::Base subclasses that correspond to the file format definition.
- #default_classes ⇒ Object
-
#drop_database_schema! ⇒ Object
Drops the table of all the ORM classes, and unregisters the classes.
- #fileformat_classes ⇒ Object
-
#get_class(line_type) ⇒ Object
Returns the ORM class for the provided line type.
-
#initialize(connection_identifier = nil) ⇒ Database
constructor
A new instance of Database.
-
#load_activerecord_class(linedefinition_or_table) ⇒ Object
Loads an ActiveRecord-based class that correspond to the given parameter, which can either be a table name or a LineDefinition instance.
-
#load_database_schema! ⇒ Object
Loads the ORM classes by inspecting the tables in the current database.
-
#orm_classes ⇒ Object
Returns an array of all the ActiveRecord-bases ORM classes for this database.
-
#register_default_orm_classes! ⇒ Object
Registers the default ORM classes in the default namespace.
-
#remove_orm_classes! ⇒ Object
Unregisters every ORM class constant.
Methods included from Connection
#connect, #connection, #disconnect, from_string
Constructor Details
#initialize(connection_identifier = nil) ⇒ Database
Returns a new instance of Database.
11 12 13 14 15 |
# File 'lib/request_log_analyzer/database.rb', line 11 def initialize(connection_identifier = nil) @line_classes = [] RequestLogAnalyzer::Database::Base.database = self connect(connection_identifier) end |
Instance Attribute Details
#file_format ⇒ Object
Returns the value of attribute file_format.
8 9 10 |
# File 'lib/request_log_analyzer/database.rb', line 8 def file_format @file_format end |
#line_classes ⇒ Object (readonly)
Returns the value of attribute line_classes.
9 10 11 |
# File 'lib/request_log_analyzer/database.rb', line 9 def line_classes @line_classes end |
Instance Method Details
#create_database_schema! ⇒ Object
Creates the database schema and related ActiveRecord::Base subclasses that correspond to the file format definition. These ORM classes will later be used to create records in the database.
70 71 72 |
# File 'lib/request_log_analyzer/database.rb', line 70 def create_database_schema! fileformat_classes.each { |klass| klass.create_table! } end |
#default_classes ⇒ Object
23 24 25 |
# File 'lib/request_log_analyzer/database.rb', line 23 def default_classes [RequestLogAnalyzer::Database::Request, RequestLogAnalyzer::Database::Source, RequestLogAnalyzer::Database::Warning] end |
#drop_database_schema! ⇒ Object
Drops the table of all the ORM classes, and unregisters the classes
75 76 77 78 |
# File 'lib/request_log_analyzer/database.rb', line 75 def drop_database_schema! file_format ? fileformat_classes.map(&:drop_table!) : orm_classes.map(&:drop_table!) remove_orm_classes! end |
#fileformat_classes ⇒ Object
62 63 64 65 66 |
# File 'lib/request_log_analyzer/database.rb', line 62 def fileformat_classes fail 'No file_format provided!' unless file_format line_classes = file_format.line_definitions.map { |(_name, definition)| load_activerecord_class(definition) } default_classes + line_classes end |
#get_class(line_type) ⇒ Object
Returns the ORM class for the provided line type
18 19 20 21 |
# File 'lib/request_log_analyzer/database.rb', line 18 def get_class(line_type) line_type = line_type.name if line_type.respond_to?(:name) Object.const_get("#{line_type}_line".camelize) end |
#load_activerecord_class(linedefinition_or_table) ⇒ Object
Loads an ActiveRecord-based class that correspond to the given parameter, which can either be a table name or a LineDefinition instance.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/request_log_analyzer/database.rb', line 46 def load_activerecord_class(linedefinition_or_table) case linedefinition_or_table when String, Symbol klass_name = linedefinition_or_table.to_s.singularize.camelize klass = RequestLogAnalyzer::Database::Base.subclass_from_table(linedefinition_or_table) when RequestLogAnalyzer::LineDefinition klass_name = "#{linedefinition_or_table.name}_line".camelize klass = RequestLogAnalyzer::Database::Base.subclass_from_line_definition(linedefinition_or_table) end Object.const_set(klass_name, klass) klass = Object.const_get(klass_name) @line_classes << klass klass end |
#load_database_schema! ⇒ Object
Loads the ORM classes by inspecting the tables in the current database
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/request_log_analyzer/database.rb', line 28 def load_database_schema! connection.tables.map do |table| case table.to_sym when :warnings then RequestLogAnalyzer::Database::Warning when :sources then RequestLogAnalyzer::Database::Source when :requests then RequestLogAnalyzer::Database::Request else load_activerecord_class(table) end end end |
#orm_classes ⇒ Object
Returns an array of all the ActiveRecord-bases ORM classes for this database
40 41 42 |
# File 'lib/request_log_analyzer/database.rb', line 40 def orm_classes default_classes + line_classes end |
#register_default_orm_classes! ⇒ Object
Registers the default ORM classes in the default namespace
81 82 83 84 85 |
# File 'lib/request_log_analyzer/database.rb', line 81 def register_default_orm_classes! Object.const_set('Request', RequestLogAnalyzer::Database::Request) Object.const_set('Source', RequestLogAnalyzer::Database::Source) Object.const_set('Warning', RequestLogAnalyzer::Database::Warning) end |
#remove_orm_classes! ⇒ Object
Unregisters every ORM class constant
88 89 90 91 92 93 94 95 |
# File 'lib/request_log_analyzer/database.rb', line 88 def remove_orm_classes! orm_classes.each do |klass| if klass.respond_to?(:name) && !klass.name.blank? klass_name = klass.name.split('::').last Object.send(:remove_const, klass_name) if Object.const_defined?(klass_name) end end end |