Class: Oracle::Model::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/oracle/model/generator.rb

Constant Summary collapse

VERSION =

The version of the oracle-model-generator library

'0.5.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Generator

Creates and returns a new Oracle::Model::Generator object. It accepts an Oracle::Connection object, which is what OCI8.new returns.

Example:

connection = Oracle::Connection.new(user, password, database)
ogenerator = Oracle::Model::Generator.new(connection)
ogenerator.generate('users')


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/oracle/model/generator.rb', line 49

def initialize(connection)
  @connection   = connection
  @constraints  = []
  @primary_keys = []
  @foreign_keys = []
  @dependencies = []
  @belongs_to   = []
  @column_info  = []
  @table        = nil
  @model        = nil
end

Instance Attribute Details

#belongs_toObject (readonly)

An array of parent tables that the table has a foreign key relationship with.



20
21
22
# File 'lib/oracle/model/generator.rb', line 20

def belongs_to
  @belongs_to
end

#column_infoObject (readonly)

An array of raw OCI::Metadata::Column objects.



35
36
37
# File 'lib/oracle/model/generator.rb', line 35

def column_info
  @column_info
end

#connectionObject (readonly)

The raw OCI8 connection.



10
11
12
# File 'lib/oracle/model/generator.rb', line 10

def connection
  @connection
end

#constraintsObject (readonly)

An array of hashes that contain per-column constraint information.



13
14
15
# File 'lib/oracle/model/generator.rb', line 13

def constraints
  @constraints
end

#dependenciesObject (readonly)

A list of dependencies for the table.



32
33
34
# File 'lib/oracle/model/generator.rb', line 32

def dependencies
  @dependencies
end

#foreign_keysObject (readonly)

An array of foreign key names.



16
17
18
# File 'lib/oracle/model/generator.rb', line 16

def foreign_keys
  @foreign_keys
end

#modelObject (readonly)

The name of the active record model to be generated.



26
27
28
# File 'lib/oracle/model/generator.rb', line 26

def model
  @model
end

#primary_keysObject (readonly)

An array of primary keys for the column. May contain one or more values.



38
39
40
# File 'lib/oracle/model/generator.rb', line 38

def primary_keys
  @primary_keys
end

#tableObject (readonly)

The table name associated with the generator.



23
24
25
# File 'lib/oracle/model/generator.rb', line 23

def table
  @table
end

#viewObject (readonly)

Boolean indicating whether the generator is for a regular table or a view.



29
30
31
# File 'lib/oracle/model/generator.rb', line 29

def view
  @view
end

Instance Method Details

#generate(table, view = false) ⇒ Object

Generates an Oracle::Model::Generator object for table. If this is a view (materialized or otherwise), set the view argument to true.

This method does not actually generate a file of any sort. It merely sets instance variables which you can then use in your own class/file generation programs. – Makes a best guess as to the model name. I’m not going to put too much effort into this. It’s much easier for you to hand edit a class name than it is for me to parse English.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/oracle/model/generator.rb', line 72

def generate(table, view = false)
  @table = table.upcase
  @model = table.split('_').map{ |e| e.downcase.capitalize }.join
  @view  = view

  # Remove trailing 's'
  @model.chop! if @model[-1].chr.upcase == 'S'

  unless view
    get_constraints
    get_foreign_keys
    get_column_info
  end

  get_primary_keys
  get_dependencies
end