Class: ROM::SQL::Header

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/sql/header.rb

Constant Summary collapse

SEP_REGEX =
/_{2,3}/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(columns, table) ⇒ Header

Returns a new instance of Header.



11
12
13
14
# File 'lib/rom/sql/header.rb', line 11

def initialize(columns, table)
  @columns = columns
  @table = table
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



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

def columns
  @columns
end

#tableObject (readonly)

Returns the value of attribute table.



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

def table
  @table
end

Instance Method Details

#exclude(*names) ⇒ Object



32
33
34
# File 'lib/rom/sql/header.rb', line 32

def exclude(*names)
  self.class.new(columns.find_all { |col| !names.include?(col) }, table)
end

#namesObject



28
29
30
# File 'lib/rom/sql/header.rb', line 28

def names
  columns.map { |col| :"#{col.to_s.split(SEP_REGEX).last}" }
end

#prefix(col_prefix) ⇒ Object



56
57
58
# File 'lib/rom/sql/header.rb', line 56

def prefix(col_prefix)
  rename(Hash[columns.map { |col| [col, :"#{col_prefix}_#{col}"] }])
end

#project(*names) ⇒ Object



36
37
38
# File 'lib/rom/sql/header.rb', line 36

def project(*names)
  self.class.new(columns.find_all { |col| names.include?(col) }, table)
end

#qualifiedObject



40
41
42
# File 'lib/rom/sql/header.rb', line 40

def qualified
  self.class.new(columns.map { |col| :"#{table}__#{col}" }, table)
end

#rename(options) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rom/sql/header.rb', line 44

def rename(options)
  self.class.new(columns.map { |col|
    new_name = options[col]

    if new_name
      :"#{col}___#{new_name}"
    else
      col
    end
  }, table)
end

#to_aryObject Also known as: to_a



16
17
18
# File 'lib/rom/sql/header.rb', line 16

def to_ary
  columns
end

#to_hObject



21
22
23
24
25
26
# File 'lib/rom/sql/header.rb', line 21

def to_h
  columns.each_with_object({}) do |col, h|
    left, right = col.to_s.split('___')
    h[left.to_sym] = (right || left).to_sym
  end
end