Class: ConventionalModels::Database

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Database

Returns a new instance of Database.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/conventional_models/database.rb', line 7

def initialize(config)
  if config.connection.nil?
    @connection = ::ActiveRecord::Base.connection
  else
    base_class_code = []
    base_class_code << "module ::#{config.module_name}"
    base_class_code << "  class Base < ActiveRecord::Base;"
    base_class_code << "    @abstract_class = true;"
    base_class_code << "  end;"
    base_class_code << "end"
    eval base_class_code.join
    @base_class = "::#{config.module_name}::Base".constantize
    @base_class.class_eval do
      establish_connection config.connection
    end
    config.base_class = @base_class
    @connection = @base_class.connection
  end
  @config = config
  @tables = []
  apply_conventions
end

Instance Attribute Details

#tablesObject

Returns the value of attribute tables.



5
6
7
# File 'lib/conventional_models/database.rb', line 5

def tables
  @tables
end

Instance Method Details

#codeObject



30
31
32
# File 'lib/conventional_models/database.rb', line 30

def code
  @tables.map{|t| t.code}.join("\n")
end

#code_for(table_name) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/conventional_models/database.rb', line 34

def code_for(table_name)
  table = @tables.select{|t| t.name == table_name}.first
  if table
    table.code
  else
    "#{table_name} not found"
  end
end