Class: ROM::SQL::Header
- Inherits:
-
Object
- Object
- ROM::SQL::Header
- Defined in:
- lib/rom/sql/header.rb
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
-
#initialize(columns, table) ⇒ Header
constructor
A new instance of Header.
- #names ⇒ Object
- #prefix(col_prefix) ⇒ Object
- #project(*names) ⇒ Object
- #qualified ⇒ Object
- #rename(options) ⇒ Object
- #to_ary ⇒ Object (also: #to_a)
- #to_h ⇒ Object
Constructor Details
#initialize(columns, table) ⇒ Header
Returns a new instance of Header.
9 10 11 12 |
# File 'lib/rom/sql/header.rb', line 9 def initialize(columns, table) @columns = columns @table = table end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
7 8 9 |
# File 'lib/rom/sql/header.rb', line 7 def columns @columns end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
7 8 9 |
# File 'lib/rom/sql/header.rb', line 7 def table @table end |
Instance Method Details
#names ⇒ Object
26 27 28 |
# File 'lib/rom/sql/header.rb', line 26 def names columns.map { |col| :"#{col.to_s.split('___').last}" } end |
#prefix(col_prefix) ⇒ Object
50 51 52 |
# File 'lib/rom/sql/header.rb', line 50 def prefix(col_prefix) rename(Hash[columns.map { |col| [col, :"#{col_prefix}_#{col}"] }]) end |
#project(*names) ⇒ Object
30 31 32 |
# File 'lib/rom/sql/header.rb', line 30 def project(*names) self.class.new(columns.find_all { |col| names.include?(col) }, table) end |
#qualified ⇒ Object
34 35 36 |
# File 'lib/rom/sql/header.rb', line 34 def qualified self.class.new(columns.map { |col| :"#{table}__#{col}" }, table) end |
#rename(options) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rom/sql/header.rb', line 38 def rename() self.class.new(columns.map { |col| new_name = [col] if new_name :"#{col}___#{new_name}" else col end }, table) end |
#to_ary ⇒ Object Also known as: to_a
14 15 16 |
# File 'lib/rom/sql/header.rb', line 14 def to_ary columns end |
#to_h ⇒ Object
19 20 21 22 23 24 |
# File 'lib/rom/sql/header.rb', line 19 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 |