Class: PgPartitions::SQL::If

Inherits:
Struct
  • Object
show all
Defined in:
lib/pg_partitions/sql.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#conditionsObject

Returns the value of attribute conditions

Returns:

  • (Object)

    the current value of conditions



9
10
11
# File 'lib/pg_partitions/sql.rb', line 9

def conditions
  @conditions
end

Instance Method Details

#to_sqlObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pg_partitions/sql.rb', line 10

def to_sql
  if conditions.empty?
    raise ArgumentError, 'You must provide at least one condition'
  end

  lines = conditions.map do |opts|
    if opts.key? :if
      build_condition :if, opts
    elsif opts.key? :elsif
      build_condition :elsif, opts
    else opts.key? :else
      "ELSE\n  #{opts[:else]}"
    end
  end

  lines << 'END IF;'
  lines.join("\n") << "\n"
end