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
29
30
# File 'lib/conventional_models/database.rb', line 7

def initialize(config)
  if config.connection.nil?
    @connection = ::ActiveRecord::Base.connection
  end
  if config.module_name
    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



32
33
34
# File 'lib/conventional_models/database.rb', line 32

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

#code_for(table_name) ⇒ Object



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

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