Module: Sequel::Extension::PgComment::CreateTableGeneratorMethods

Includes:
Sequel::Extension::PgComment
Defined in:
lib/sequel/extensions/pg_comment/create_table_generator_methods.rb

Overview

:nodoc: Enhancements to the standard schema modification methods in a block-form create_table method, to support setting comments via the :comment option.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commentsArray<SqlGenerator> (readonly)

An array of all the comments that this generator has seen fit to create.

Returns:



12
13
14
# File 'lib/sequel/extensions/pg_comment/create_table_generator_methods.rb', line 12

def comments
  @comments
end

Instance Method Details

#column(*args) ⇒ Object

Enhanced version of the column table definition method, which supports setting a comment on the column.

Parameters:

  • [String] (Hash)

    a customizable set of options



22
23
24
25
26
27
28
# File 'lib/sequel/extensions/pg_comment/create_table_generator_methods.rb', line 22

def column(*args)
	super

	if args.last.is_a?(Hash) && args.last[:comment]
		comments << SqlGenerator.create(:column, args.first, args.last[:comment])
	end
end

#composite_foreign_key(columns, opts) ⇒ Object

Enhanced version of the composite_foreign_key table definition method, which supports setting a comment on the FK constraint.

Parameters:

  • [String] (Hash)

    a customizable set of options



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sequel/extensions/pg_comment/create_table_generator_methods.rb', line 77

def composite_foreign_key(columns, opts)
	if opts.is_a?(Hash) and opts[:comment] and opts[:table]
		if opts[:name]
			comments << SqlGenerator.create(
			              :constraint,
			              opts[:name],
			              opts[:comment]
			            )
		else
			comments << SqlGenerator.create(
			              :constraint,
			              "#{opts[:table]}_#{columns.first}_fkey".to_sym,
			              opts[:comment]
			            )
		end
	end

	super
end

#composite_primary_key(columns, *args) ⇒ Object

Enhanced version of the composite_primary_key table definition method, which supports setting a comment on the primary key index.

Parameters:

  • [String] (Hash)

    a customizable set of options



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sequel/extensions/pg_comment/create_table_generator_methods.rb', line 56

def composite_primary_key(columns, *args)
	if args.last.is_a?(Hash) and args.last[:comment]
		if args.last[:name]
			comments << SqlGenerator.create(
			              :index,
			              args.last[:name],
			              args.last[:comment]
			            )
		else
			comments << PrefixSqlGenerator.new(:index, :_pkey, args.last[:comment])
		end
	end

	super
end

#constraint(name, *args, &block) ⇒ Object

Enhanced version of the constraint table definition method, which supports setting a comment on the constraint.

Parameters:

  • [String] (Hash)

    a customizable set of options



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/sequel/extensions/pg_comment/create_table_generator_methods.rb', line 147

def constraint(name, *args, &block)
	opts = name.is_a?(Hash) ? name : (args.last.is_a?(Hash) ? args.last : {})
	
	if opts[:comment]
		if name
			comments << SqlGenerator.create(:constraint, name, opts[:comment])
		else
			raise RuntimeError,
			      "Setting comments on unnamed or check constraints is not supported"
		end
	end
end

#index(columns, opts = OPTS) ⇒ Object

Enhanced version of the index table definition method, which supports setting a comment on the index.

Parameters:

  • [String] (Hash)

    a customizable set of options



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/sequel/extensions/pg_comment/create_table_generator_methods.rb', line 103

def index(columns, opts = OPTS)
	if opts[:comment]
		if opts[:name]
			comments << SqlGenerator.create(:index, opts[:name], opts[:comment])
		else
			comments << PrefixSqlGenerator.new(
			              :index,
			              ("_" + [columns].flatten.map(&:to_s).join('_') + "_index").to_sym,
			              opts[:comment]
			            )
		end
	end
	
	super
end

#primary_key(name, *args) ⇒ Object

Enhanced version of the primary_key table definition method, which supports setting a comment on either the column or constraint.

If the primary key is composite (name is an array), then the comment will be placed on the index. Otherwise, the comment will be set on the column itself.

Parameters:

  • [String] (Hash)

    a customizable set of options



40
41
42
43
44
45
46
47
48
49
# File 'lib/sequel/extensions/pg_comment/create_table_generator_methods.rb', line 40

def primary_key(name, *args)
	if args.last.is_a?(Hash) && args.last[:comment] and !name.is_a? Array
		# The composite primary key case will be handled by the
		# `composite_primary_key` method, so we don't have to deal with it
		# here.
		comments << SqlGenerator.create(:column, name, args.last[:comment])
	end
			
	super
end

#unique(columns, opts = OPTS) ⇒ Object

Enhanced version of the unique table definition method, which supports setting a comment on the unique index.

Parameters:

  • [String] (Hash)

    a customizable set of options



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/sequel/extensions/pg_comment/create_table_generator_methods.rb', line 125

def unique(columns, opts = OPTS)
	if opts[:comment]
		if opts[:name]
			comments << SqlGenerator.create(:index, opts[:name], opts[:comment])
		else
			comments << PrefixSqlGenerator.new(
			              :index,
			              ("_" + [columns].flatten.map(&:to_s).join('_') + "_key").to_sym,
			              opts[:comment]
			            )
		end
	end
	
	super
end