Module: PgKingdom::ColumnsHelper

Defined in:
lib/pgkingdom/columns_helper.rb

Overview

Internally used to extend Arrays of columns in tables.

Instance Method Summary collapse

Instance Method Details

#add(name, type, opts) ⇒ Array<Hash{Symbol=>(Numeric|Symbol|String|Hash)}>

Adds column definition hash to array.

Returns:

  • (Array<Hash{Symbol=>(Numeric|Symbol|String|Hash)}>)

    Array with Hashes describing table columns.



27
28
29
# File 'lib/pgkingdom/columns_helper.rb', line 27

def add(name, type, opts)
  self << { :name => name, :type => type, :options => opts }
end

#available?(type) ⇒ Boolean

Checks if provided type is valid PostgreSQL data type.

Returns:

  • (Boolean)


21
22
23
# File 'lib/pgkingdom/columns_helper.rb', line 21

def available?(type)
  PgKingdom.available_data_types.include? type
end

#defined?(name) ⇒ Boolean

Checks if column defined in table.

Returns:

  • (Boolean)


32
33
34
# File 'lib/pgkingdom/columns_helper.rb', line 32

def defined?(name)
  select { |a| a[:name] == name }.any?
end

#joinString

Joins columns for a statement.

Returns:

  • (String)

    columns definition with options.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/pgkingdom/columns_helper.rb', line 8

def join
  map do |col|
    opt_string = ""

    # Flags
    options = col[:options]
    options.extend OptionsHelper

    "#{col[:name]} #{col[:type].upcase}#{options.join}"
  end.join(",\n\s\s")
end