Sha256: 4741f7a916e6f63f3a5f9f5083291f233b0b8fc42bba53039237a272e61a71d6

Contents?: true

Size: 1.71 KB

Versions: 5

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: false

module PGTrunk::Operations::Domains
  # @abstract
  # @private
  # Base class for operations with domain types
  class Base < PGTrunk::Operation
    # All attributes that can be used by domain-related commands
    attribute :collation, :pg_trunk_qualified_name
    attribute :constraints, :pg_trunk_array_of_hashes, default: []
    attribute :default_sql, :pg_trunk_multiline_text
    attribute :null, :boolean
    attribute :type, :pg_trunk_qualified_name, aliases: :as

    # Populate constraints from a block
    def constraint(check, name: nil)
      constraints << Constraint.new(check: check, name: name)
    end

    # Wrap constraint definitions to value objects
    after_initialize { constraints.map! { |c| Constraint.build(c) } }

    validates :if_not_exists, absence: true
    validates :name, presence: true
    validates :constraints, "PGTrunk/all_items_valid": true, allow_nil: true

    # Use comparison by name from pg_trunk operations base class (default)
    # Support name as the only positional argument (default)

    ruby_snippet do |s|
      s.ruby_param(name.lean) if name.present?
      s.ruby_param(as: type.lean) if type.present?
      s.ruby_param(to: new_name) if new_name.present?
      s.ruby_param(if_exists: true) if if_exists
      s.ruby_param(force: :cascade) if force == :cascade

      s.ruby_line(:collation, collation.lean) if collation.present?
      s.ruby_line(:default_sql, default_sql, from: from_default_sql) if default_sql
      s.ruby_line(:null, false) if null == false
      constraints.sort_by(&:name).each do |c|
        s.ruby_line(:constraint, c.check, **c.opts)
      end
      s.ruby_line(:comment, comment, from: from_comment) if comment
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pg_trunk-0.2.0 lib/pg_trunk/operations/domains/base.rb
pg_trunk-0.1.3 lib/pg_trunk/operations/domains/base.rb
pg_trunk-0.1.2 lib/pg_trunk/operations/domains/base.rb
pg_trunk-0.1.1 lib/pg_trunk/operations/domains/base.rb
pg_trunk-0.1.0 lib/pg_trunk/operations/domains/base.rb