Class: Pcrd::Schema::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/pcrd/schema/column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attnum:, name:, type_name:, formatted_type:, alignment:, fixed_size:, nullable:, default_expr:) ⇒ Column

alignment: Integer (1, 2, 4, or 8 bytes) fixed_size: Integer bytes, or nil for variable-length



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pcrd/schema/column.rb', line 11

def initialize(attnum:, name:, type_name:, formatted_type:,
               alignment:, fixed_size:, nullable:, default_expr:)
  @attnum         = attnum
  @name           = name
  @type_name      = type_name
  @formatted_type = formatted_type
  @alignment      = alignment
  @fixed_size     = fixed_size
  @nullable       = nullable
  @default_expr   = default_expr
end

Instance Attribute Details

#alignmentObject (readonly)

Returns the value of attribute alignment.



6
7
8
# File 'lib/pcrd/schema/column.rb', line 6

def alignment
  @alignment
end

#attnumObject (readonly)

Returns the value of attribute attnum.



6
7
8
# File 'lib/pcrd/schema/column.rb', line 6

def attnum
  @attnum
end

#default_exprObject (readonly)

Returns the value of attribute default_expr.



6
7
8
# File 'lib/pcrd/schema/column.rb', line 6

def default_expr
  @default_expr
end

#fixed_sizeObject (readonly)

Returns the value of attribute fixed_size.



6
7
8
# File 'lib/pcrd/schema/column.rb', line 6

def fixed_size
  @fixed_size
end

#formatted_typeObject (readonly)

Returns the value of attribute formatted_type.



6
7
8
# File 'lib/pcrd/schema/column.rb', line 6

def formatted_type
  @formatted_type
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/pcrd/schema/column.rb', line 6

def name
  @name
end

#nullableObject (readonly)

Returns the value of attribute nullable.



6
7
8
# File 'lib/pcrd/schema/column.rb', line 6

def nullable
  @nullable
end

#type_nameObject (readonly)

Returns the value of attribute type_name.



6
7
8
# File 'lib/pcrd/schema/column.rb', line 6

def type_name
  @type_name
end

Instance Method Details

#==(other) ⇒ Object



50
51
52
# File 'lib/pcrd/schema/column.rb', line 50

def ==(other)
  other.is_a?(Column) && name == other.name && type_name == other.type_name
end

#display_alignmentObject



46
47
48
# File 'lib/pcrd/schema/column.rb', line 46

def display_alignment
  "#{alignment}B"
end

#display_sizeObject



42
43
44
# File 'lib/pcrd/schema/column.rb', line 42

def display_size
  fixed_size ? fixed_size.to_s : "variable"
end

#display_typeObject

Human-readable type string, with common verbose PG names shortened.



32
33
34
35
36
37
38
39
40
# File 'lib/pcrd/schema/column.rb', line 32

def display_type
  formatted_type
    .sub("character varying", "varchar")
    .sub("character(", "char(")
    .sub("timestamp without time zone", "timestamp")
    .sub("timestamp with time zone", "timestamptz")
    .sub("time without time zone", "time")
    .sub("time with time zone", "timetz")
end

#fixed?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/pcrd/schema/column.rb', line 27

def fixed?
  !variable?
end

#inspectObject



54
55
56
# File 'lib/pcrd/schema/column.rb', line 54

def inspect
  "#<Pcrd::Schema::Column #{name}:#{display_type} align=#{alignment} size=#{display_size}>"
end

#variable?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/pcrd/schema/column.rb', line 23

def variable?
  fixed_size.nil?
end