Class: Sequel::Schema::Generator
- Inherits:
-
Object
- Object
- Sequel::Schema::Generator
show all
- Defined in:
- lib/sequel/schema/schema_generator.rb
Instance Method Summary
collapse
Constructor Details
#initialize(db, &block) ⇒ Generator
Returns a new instance of Generator.
4
5
6
7
8
9
10
|
# File 'lib/sequel/schema/schema_generator.rb', line 4
def initialize(db, &block)
@db = db
@columns = []
@indexes = []
@primary_key = nil
instance_eval(&block) if block
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(type, name = nil, opts = {}) ⇒ Object
12
13
14
|
# File 'lib/sequel/schema/schema_generator.rb', line 12
def method_missing(type, name = nil, opts = {})
name ? column(name, type, opts) : super
end
|
Instance Method Details
#column(name, type, opts = {}) ⇒ Object
33
34
35
36
|
# File 'lib/sequel/schema/schema_generator.rb', line 33
def column(name, type, opts = {})
@columns << {:name => name, :type => type}.merge(opts)
index(name) if opts[:index]
end
|
#create_info ⇒ Object
57
58
59
60
61
62
|
# File 'lib/sequel/schema/schema_generator.rb', line 57
def create_info
if @primary_key && !has_column?(@primary_key[:name])
@columns.unshift(@primary_key)
end
[@columns, @indexes]
end
|
#foreign_key(name, opts = {}) ⇒ Object
38
39
40
41
|
# File 'lib/sequel/schema/schema_generator.rb', line 38
def foreign_key(name, opts = {})
@columns << {:name => name, :type => :integer}.merge(opts)
index(name) if opts[:index]
end
|
#has_column?(name) ⇒ Boolean
43
44
45
46
|
# File 'lib/sequel/schema/schema_generator.rb', line 43
def has_column?(name)
@columns.each {|c| return true if c[:name] == name}
false
end
|
#index(columns, opts = {}) ⇒ Object
48
49
50
51
|
# File 'lib/sequel/schema/schema_generator.rb', line 48
def index(columns, opts = {})
columns = [columns] unless columns.is_a?(Array)
@indexes << {:columns => columns}.merge(opts)
end
|
#primary_key(name, *args) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/sequel/schema/schema_generator.rb', line 20
def primary_key(name, *args)
@primary_key = @db.serial_primary_key_options.merge({:name => name})
if opts = args.pop
opts = {:type => opts} unless opts.is_a?(Hash)
if type = args.pop
opts.merge!(:type => type)
end
@primary_key.merge!(opts)
end
@primary_key
end
|
#primary_key_name ⇒ Object
16
17
18
|
# File 'lib/sequel/schema/schema_generator.rb', line 16
def primary_key_name
@primary_key ? @primary_key[:name] : nil
end
|
#unique(columns, opts = {}) ⇒ Object
53
54
55
|
# File 'lib/sequel/schema/schema_generator.rb', line 53
def unique(columns, opts = {})
index(columns, {:unique => true}.merge(opts))
end
|