Class: DutyFree::Column

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

Overview

Holds detail about each column as we recursively explore the scope of what to import

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, pre_prefix, prefix, prefix_assocs, obj_class, import_template_as) ⇒ Column

Returns a new instance of Column.



11
12
13
14
15
16
17
18
# File 'lib/duty_free/column.rb', line 11

def initialize(name, pre_prefix, prefix, prefix_assocs, obj_class, import_template_as)
  self.name = name
  self.pre_prefix = pre_prefix
  self.prefix = prefix
  self.prefix_assocs = prefix_assocs
  self.import_template_as = import_template_as
  self.obj_class = obj_class
end

Instance Attribute Details

#import_template_asObject

Returns the value of attribute import_template_as.



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

def import_template_as
  @import_template_as
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#obj_class=(value) ⇒ Object (writeonly)

Sets the attribute obj_class

Parameters:

  • value

    the value to set the attribute obj_class to.



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

def obj_class=(value)
  @obj_class = value
end

#pre_prefixObject

Returns the value of attribute pre_prefix.



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

def pre_prefix
  @pre_prefix
end

#prefixObject

Returns the value of attribute prefix.



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

def prefix
  @prefix
end

#prefix_assocsObject

Returns the value of attribute prefix_assocs.



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

def prefix_assocs
  @prefix_assocs
end

Instance Method Details

#pathObject



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

def path
  @path ||= ::DutyFree::Util._prefix_join([pre_prefix, prefix]).split('.').map(&:to_sym)
end

#titleizeObject



38
39
40
# File 'lib/duty_free/column.rb', line 38

def titleize
  @titleized ||= to_sym.titleize
end

#to_s(mapping = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/duty_free/column.rb', line 20

def to_s(mapping = nil)
  # Crap way:
  # sql_col = ::DutyFree::Util._prefix_join([prefix_assocs.last&.klass&.table_name, name])

  # Slightly less crap:
  # table_name = [prefix_assocs.first&.klass&.table_name]
  # alias_name = prefix_assocs.last&.plural_name&.to_s
  # table_name.unshift(alias_name) unless table_name.first == alias_name
  # sql_col = ::DutyFree::Util._prefix_join([table_name.compact.join('_'), name])

  # Foolproof way, using the AREL mapping:
  this_pre_prefix = pre_prefix.tr('.', '_')
  this_pre_prefix << '_' unless pre_prefix.blank?
  sql_col = ::DutyFree::Util._prefix_join([mapping["#{this_pre_prefix}#{prefix}_"], name])
  sym = to_sym.to_s
  sql_col == sym ? sql_col : "#{sql_col} AS #{sym}"
end

#to_symObject

The snake-cased column name to be used for building the full list of template_columns



47
48
49
50
51
52
# File 'lib/duty_free/column.rb', line 47

def to_sym
  @sym_string ||= ::DutyFree::Util._prefix_join(
    [pre_prefix, prefix, ::DutyFree::Util._clean_name(name, import_template_as)],
    '_'
  ).tr('.', '_')
end