Class: Migsupo::Schema::TableDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/migsupo/schema/table_definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, columns: [], indexes: [], options: {}) ⇒ TableDefinition

Returns a new instance of TableDefinition.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/migsupo/schema/table_definition.rb', line 6

def initialize(name:, columns: [], indexes: [], options: {})
  @name    = name.to_s
  @columns = columns.freeze
  @indexes = indexes.freeze
  @options = options.transform_keys(&:to_sym).freeze

  @columns_by_name = columns.each_with_object({}) { |c, h| h[c.name] = c }.freeze
  @indexes_by_name = indexes.each_with_object({}) { |i, h| h[i.name] = i }.freeze

  freeze
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



4
5
6
# File 'lib/migsupo/schema/table_definition.rb', line 4

def columns
  @columns
end

#indexesObject (readonly)

Returns the value of attribute indexes.



4
5
6
# File 'lib/migsupo/schema/table_definition.rb', line 4

def indexes
  @indexes
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/migsupo/schema/table_definition.rb', line 4

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/migsupo/schema/table_definition.rb', line 4

def options
  @options
end

Instance Method Details

#column(col_name) ⇒ Object



18
19
20
# File 'lib/migsupo/schema/table_definition.rb', line 18

def column(col_name)
  @columns_by_name[col_name.to_s]
end

#index(idx_name) ⇒ Object



22
23
24
# File 'lib/migsupo/schema/table_definition.rb', line 22

def index(idx_name)
  @indexes_by_name[idx_name.to_s]
end

#to_hObject



26
27
28
# File 'lib/migsupo/schema/table_definition.rb', line 26

def to_h
  { name: name, columns: columns.map(&:to_h), indexes: indexes.map(&:to_h), options: options }
end