Class: LegacyData::TableDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/legacy_data/table_definition.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ TableDefinition

Returns a new instance of TableDefinition.



5
6
7
# File 'lib/legacy_data/table_definition.rb', line 5

def initialize(options)
  options.each {|key, value| self.send("#{key}=", value) }
end

Instance Attribute Details

#class_nameObject

Returns the value of attribute class_name.



3
4
5
# File 'lib/legacy_data/table_definition.rb', line 3

def class_name
  @class_name
end

#columnsObject

Returns the value of attribute columns.



3
4
5
# File 'lib/legacy_data/table_definition.rb', line 3

def columns
  @columns
end

#constraintsObject

Returns the value of attribute constraints.



3
4
5
# File 'lib/legacy_data/table_definition.rb', line 3

def constraints
  @constraints
end

#primary_keyObject

Returns the value of attribute primary_key.



3
4
5
# File 'lib/legacy_data/table_definition.rb', line 3

def primary_key
  @primary_key
end

#relationsObject

Returns the value of attribute relations.



3
4
5
# File 'lib/legacy_data/table_definition.rb', line 3

def relations
  @relations
end

#table_nameObject

Returns the value of attribute table_name.



3
4
5
# File 'lib/legacy_data/table_definition.rb', line 3

def table_name
  @table_name
end

Class Method Details

.class_name_for(table_name) ⇒ Object



48
49
50
# File 'lib/legacy_data/table_definition.rb', line 48

def self.class_name_for table_name
  LegacyData::TableClassNameMapper.class_name_for(table_name)
end

Instance Method Details

#[](key) ⇒ Object



9
10
11
# File 'lib/legacy_data/table_definition.rb', line 9

def [] key
  self.send(key)
end

#belongs_to_relationsObject



137
138
139
140
# File 'lib/legacy_data/table_definition.rb', line 137

def belongs_to_relations
  return {} if relations.nil? or relations[:belongs_to].nil?
  relations[:belongs_to]
end

#belongs_to_tablesObject



142
143
144
145
# File 'lib/legacy_data/table_definition.rb', line 142

def belongs_to_tables
  return [] if belongs_to_relations == {}
  belongs_to_relations.keys
end

#constraints_to_sObject



68
69
70
71
72
73
74
# File 'lib/legacy_data/table_definition.rb', line 68

def constraints_to_s
  alphabetized_constraints_types = constraints.keys.map(&:to_s).sort
  constraints_text = alphabetized_constraints_types.map do |constraint_type|
    self.send("#{constraint_type}_constraints_to_s") unless constraints[constraint_type.to_sym].blank?
  end
  constraints_text.flatten.reject(&:nil?).join("\n  ")
end

#convert_has_many_to_habtm(join_table) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/legacy_data/table_definition.rb', line 147

def convert_has_many_to_habtm(join_table)
  other_table_name = join_table.belongs_to_tables.detect {|table_name| table_name != self.table_name}
  relations[:has_and_belongs_to_many][other_table_name] = { :foreign_key            =>join_table.belongs_to_relations[table_name][:foreign_key], 
                                                            :association_foreign_key=>join_table.belongs_to_relations[other_table_name][:foreign_key],
                                                            :join_table             =>join_table.table_name.to_sym }
  relations[:has_many].delete(join_table.table_name)                                                          
end

#custom_constraints_to_sObject



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/legacy_data/table_definition.rb', line 116

def custom_constraints_to_s
  constraints[:custom].keys.map do |name|
    <<-OUTPUT
  validate #{"validate_#{name}".to_sym.inspect }
  def validate_#{name}
# TODO: validate this SQL constraint
<<-SQL
  #{constraints[:custom][name]}
SQL
  end
    OUTPUT
  end
end

#inclusion_of_constraints_to_sObject



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/legacy_data/table_definition.rb', line 92

def inclusion_of_constraints_to_s
  constraints[:inclusion_of].keys.map do |col|
    <<-OUTPUT
  def self.possible_values_for_#{col}
#{constraints[:inclusion_of][col].inspect}
  end
  validates_inclusion_of #{col.to_sym.inspect},
                     :in      => possible_values_for_#{col}, 
                     :message => "is not one of (\#{possible_values_for_#{col}.join(', ')})"
    OUTPUT
  end
end

#join_table?Boolean

Returns:

  • (Boolean)


130
131
132
133
134
135
# File 'lib/legacy_data/table_definition.rb', line 130

def join_table?
  return false unless (columns.size == 2) and relations[:belongs_to] and (relations[:belongs_to].values.size == 2)
  column_names      = columns.map(&:name)
  foreign_key_names = relations[:belongs_to].values.map {|value| value[:foreign_key]}.map(&:to_s) 
  return column_names.sort == foreign_key_names.sort
end

#longest_association_name(associations, is_singular_association) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/legacy_data/table_definition.rb', line 52

def longest_association_name(associations, is_singular_association)
  association_names = associations.keys.map do |assoc| 
    association_name = TableDefinition.class_name_for(assoc).underscore
    association_name = association_name.pluralize unless is_singular_association
    association_name
  end
  association_with_longest_name = association_names.max { |a,b| a.length <=> b.length }
end

#multi_column_unique_constraints_to_sObject



81
82
83
84
85
# File 'lib/legacy_data/table_definition.rb', line 81

def multi_column_unique_constraints_to_s
  constraints[:multi_column_unique].map do |cols| 
    "#validates_uniqueness_of_multiple_column_constraint #{cols.inspect}"
  end
end

#numericality_of_constraints_to_sObject



105
106
107
108
109
110
111
112
113
114
# File 'lib/legacy_data/table_definition.rb', line 105

def numericality_of_constraints_to_s
  [:allow_nil, :do_not_allow_nil].map do |nullable| 
    if constraints[:numericality_of][nullable].blank? 
      []
    else
      cols_list = constraints[:numericality_of][nullable].map {|cols| cols.downcase.to_sym.inspect}.join(', ')
      "validates_numericality_of #{cols_list}#{", {:allow_nil=>true}" if nullable == :allow_nil }"
    end
  end unless constraints[:numericality_of].blank? 
end

#options_to_s(options) ⇒ Object



61
62
63
64
65
66
# File 'lib/legacy_data/table_definition.rb', line 61

def options_to_s options
  alphabetized_option_keys = options.keys.map(&:to_s).sort
  alphabetized_option_keys.map do |key|
    "#{key.to_sym.inspect} => #{options[key.to_sym].inspect}"
  end.join(', ')
end

#presence_of_constraints_to_sObject



87
88
89
90
# File 'lib/legacy_data/table_definition.rb', line 87

def presence_of_constraints_to_s
  cols_list = constraints[:presence_of].map {|cols| cols.downcase.to_sym.inspect}.join(', ')
  "validates_presence_of #{cols_list}"
end

#relationships_to_sObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/legacy_data/table_definition.rb', line 21

def relationships_to_s
  relationships_text = []
  [:has_many, :has_one, :belongs_to, :has_and_belongs_to_many].each do |relation_type|
    is_singular_association = [:has_one, :belongs_to].include?(relation_type)
    unless relations[relation_type].nil?
      association_with_longest_name = longest_association_name(relations[relation_type], is_singular_association)
      
      relations[relation_type].keys.sort.each do |table_name|
        options =  relations[relation_type][table_name]
        class_for_table = TableDefinition.class_name_for(table_name)
        association_name = class_for_table.underscore
        association_name = association_name.pluralize unless is_singular_association
        needs_class_name = (LegacyData.conventional_class_name(association_name) != class_for_table)
        options[:class_name] = class_for_table if needs_class_name

        spaces = association_with_longest_name.size - association_name.size
        relationships_text << "#{relation_type} #{association_name.to_sym.inspect},#{' ' * spaces} #{options_to_s(options)}"
      end
    end
  end
  relationships_text.join "\n  "
end

#unconventional_primary_key?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/legacy_data/table_definition.rb', line 17

def unconventional_primary_key?
  primary_key != 'id'
end

#unconventional_table_name?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/legacy_data/table_definition.rb', line 13

def unconventional_table_name?
  table_name != class_name.underscore.pluralize
end

#unique_constraints_to_sObject



76
77
78
79
# File 'lib/legacy_data/table_definition.rb', line 76

def unique_constraints_to_s
  cols_list = constraints[:unique].map {|cols| cols.downcase.to_sym.inspect}.join(', ')
  "validates_uniqueness_of #{cols_list}" 
end