Class: Pgerd::Column

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/pgerd/column.rb

Constant Summary collapse

ID =
%w(id)
GROUPS =
%i(id other timestamps)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, data_type) ⇒ Column

Returns a new instance of Column.



8
9
10
11
# File 'lib/pgerd/column.rb', line 8

def initialize(name, data_type)
  @name = name
  @data_type = data_type
end

Instance Attribute Details

#data_typeObject

Returns the value of attribute data_type.



4
5
6
# File 'lib/pgerd/column.rb', line 4

def data_type
  @data_type
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/pgerd/column.rb', line 4

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ Object



13
14
15
# File 'lib/pgerd/column.rb', line 13

def <=>(other)
  to_a_for_sorting <=> other.to_a_for_sorting
end

#abbreviated_data_typeObject



31
32
33
34
35
36
37
38
# File 'lib/pgerd/column.rb', line 31

def abbreviated_data_type
  # The SQL standard requires that writing just timestamp be equivalent to timestamp without time zone,
  # and PostgreSQL honors that behavior.
  @data_type
    .gsub('timestamp without time zone', 'timestamp')
    .gsub(/\bwithout\b/, 'w/o')
    .gsub(/\bwith\b/, 'w/')
end

#groupObject



17
18
19
20
21
22
23
24
25
# File 'lib/pgerd/column.rb', line 17

def group
  if ID.include? @name
    :id
  elsif @data_type.include?("timestamp")
    :timestamps
  else
    :other
  end
end

#group_for_sortingObject



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

def group_for_sorting
  GROUPS.index(group)
end

#to_a_for_sortingObject



44
45
46
# File 'lib/pgerd/column.rb', line 44

def to_a_for_sorting
  [group_for_sorting, name, data_type]
end

#to_sObject



40
41
42
# File 'lib/pgerd/column.rb', line 40

def to_s
  "#{name} [label = <>]"
end