class Ahnnotate::Column

Attributes

name[R]

Public Class Methods

new(name:, sql_type:, nullable:, primary_key:, default:) click to toggle source
# File lib/ahnnotate/column.rb, line 21
def initialize(name:, sql_type:, nullable:, primary_key:, default:)
  @name = name
  @sql_type = sql_type.to_s.downcase.gsub(/\(.*?\)/, "")
  @nullable = nullable
  @primary_key = primary_key
  @default = default
end
sql_type_map() click to toggle source
# File lib/ahnnotate/column.rb, line 5
def self.sql_type_map
  @sql_type_map ||=
    begin
      map = Hash.new { |_self, k| k }
      map["character varying"] = "varchar"
      map["character"] = "char"
      map["datetime"] = "timestamp"
      map["double precision"] = "double"
      map["time with time zone"] = "timetz"
      map["time without time zone"] = "time"
      map["timestamp with time zone"] = "timestamptz"
      map["timestamp without time zone"] = "timestamp"
      map
    end
end

Public Instance Methods

default() click to toggle source
# File lib/ahnnotate/column.rb, line 55
def default
  if @default.nil?
    return nil
  end

  if type == "boolean"
    default_is_false =
      if ActiveRecordVersion.five_and_up?
        ActiveModel::Type::Boolean::FALSE_VALUES.include?(@default)
      else
        ActiveRecord::ConnectionAdapters::Column::FALSE_VALUES.include?(@default)
      end

    return !default_is_false
  end

  @default
end
details() click to toggle source
# File lib/ahnnotate/column.rb, line 33
def details
  if @details
    return @details
  end

  details = []

  if !nullable?
    details.push("not null")
  end

  if has_default?
    details.push("default (#{default.inspect})")
  end

  if primary_key?
    details.push("primary key")
  end

  @details = details.join(", ")
end
has_default?() click to toggle source
# File lib/ahnnotate/column.rb, line 74
def has_default?
  !default.nil?
end
nullable?() click to toggle source
# File lib/ahnnotate/column.rb, line 78
def nullable?
  !!@nullable
end
primary_key?() click to toggle source
# File lib/ahnnotate/column.rb, line 82
def primary_key?
  !!@primary_key
end
type() click to toggle source
# File lib/ahnnotate/column.rb, line 29
def type
  self.class.sql_type_map[@sql_type]
end