Class: LegacyData::TableDefinition
- Inherits:
-
Object
- Object
- LegacyData::TableDefinition
- Defined in:
- lib/legacy_data/table_definition.rb
Instance Attribute Summary collapse
-
#class_name ⇒ Object
Returns the value of attribute class_name.
-
#columns ⇒ Object
Returns the value of attribute columns.
-
#constraints ⇒ Object
Returns the value of attribute constraints.
-
#primary_key ⇒ Object
Returns the value of attribute primary_key.
-
#relations ⇒ Object
Returns the value of attribute relations.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #belongs_to_relations ⇒ Object
- #belongs_to_tables ⇒ Object
- #constraints_to_s ⇒ Object
- #convert_has_many_to_habtm(join_table) ⇒ Object
- #custom_constraints_to_s ⇒ Object
- #inclusion_of_constraints_to_s ⇒ Object
-
#initialize(options) ⇒ TableDefinition
constructor
A new instance of TableDefinition.
- #join_table? ⇒ Boolean
- #longest_association_name(associations, is_singular_association) ⇒ Object
- #multi_column_unique_constraints_to_s ⇒ Object
- #numericality_of_constraints_to_s ⇒ Object
- #options_to_s(options) ⇒ Object
- #presence_of_constraints_to_s ⇒ Object
- #relationships_to_s ⇒ Object
- #unconventional_primary_key? ⇒ Boolean
- #unconventional_table_name? ⇒ Boolean
- #unique_constraints_to_s ⇒ Object
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() .each {|key, value| self.send("#{key}=", value) } end |
Instance Attribute Details
#class_name ⇒ Object
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 |
#columns ⇒ Object
Returns the value of attribute columns.
3 4 5 |
# File 'lib/legacy_data/table_definition.rb', line 3 def columns @columns end |
#constraints ⇒ Object
Returns the value of attribute constraints.
3 4 5 |
# File 'lib/legacy_data/table_definition.rb', line 3 def constraints @constraints end |
#primary_key ⇒ Object
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 |
#relations ⇒ Object
Returns the value of attribute relations.
3 4 5 |
# File 'lib/legacy_data/table_definition.rb', line 3 def relations @relations end |
#table_name ⇒ Object
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
49 50 51 |
# File 'lib/legacy_data/table_definition.rb', line 49 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_relations ⇒ Object
138 139 140 141 |
# File 'lib/legacy_data/table_definition.rb', line 138 def belongs_to_relations return {} if relations.nil? or relations[:belongs_to].nil? relations[:belongs_to] end |
#belongs_to_tables ⇒ Object
143 144 145 146 |
# File 'lib/legacy_data/table_definition.rb', line 143 def belongs_to_tables return [] if belongs_to_relations == {} belongs_to_relations.keys end |
#constraints_to_s ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/legacy_data/table_definition.rb', line 69 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
148 149 150 151 152 153 154 |
# File 'lib/legacy_data/table_definition.rb', line 148 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_s ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/legacy_data/table_definition.rb', line 117 def custom_constraints_to_s constraints[:custom].keys.map do |name| " validate \#{\"validate_\#{name}\".to_sym.inspect }\n def validate_\#{name}\n# TODO: validate this SQL constraint\n<<-SQL\n \#{constraints[:custom][name]}\nSQL\n end\n OUTPUT\n end\nend\n" |
#inclusion_of_constraints_to_s ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/legacy_data/table_definition.rb', line 93 def inclusion_of_constraints_to_s constraints[:inclusion_of].keys.map do |col| " def self.possible_values_for_\#{col}\n\#{constraints[:inclusion_of][col].inspect}\n end\n validates_inclusion_of \#{col.to_sym.inspect},\n :in => possible_values_for_\#{col}, \n :message => \"is not one of (\\\#{possible_values_for_\#{col}.join(', ')})\"\n OUTPUT\n end\nend\n" |
#join_table? ⇒ Boolean
131 132 133 134 135 136 |
# File 'lib/legacy_data/table_definition.rb', line 131 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
53 54 55 56 57 58 59 60 |
# File 'lib/legacy_data/table_definition.rb', line 53 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_s ⇒ Object
82 83 84 85 86 |
# File 'lib/legacy_data/table_definition.rb', line 82 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_s ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/legacy_data/table_definition.rb', line 106 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
62 63 64 65 66 67 |
# File 'lib/legacy_data/table_definition.rb', line 62 def alphabetized_option_keys = .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_s ⇒ Object
88 89 90 91 |
# File 'lib/legacy_data/table_definition.rb', line 88 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_s ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/legacy_data/table_definition.rb', line 22 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| = 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 = (ActiveRecord::Base.class_name(association_name.pluralize) != class_for_table) [: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
17 18 19 20 |
# File 'lib/legacy_data/table_definition.rb', line 17 def unconventional_primary_key? puts self.inspect if primary_key.nil? primary_key != 'id' end |
#unconventional_table_name? ⇒ 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_s ⇒ Object
77 78 79 80 |
# File 'lib/legacy_data/table_definition.rb', line 77 def unique_constraints_to_s cols_list = constraints[:unique].map {|cols| cols.downcase.to_sym.inspect}.join(', ') "validates_uniqueness_of #{cols_list}" end |