Class: LegacyData::TableClassNameMapper

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/legacy_data/table_class_name_mapper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dictionaryObject

Returns the value of attribute dictionary.



7
8
9
# File 'lib/legacy_data/table_class_name_mapper.rb', line 7

def dictionary
  @dictionary
end

#naming_conventionObject

Returns the value of attribute naming_convention.



7
8
9
# File 'lib/legacy_data/table_class_name_mapper.rb', line 7

def naming_convention
  @naming_convention
end

Class Method Details

.log(msg) ⇒ Object



65
66
67
# File 'lib/legacy_data/table_class_name_mapper.rb', line 65

def self.log msg
  puts msg
end

.method_missing(method_id, *arguments, &block) ⇒ Object



16
17
18
# File 'lib/legacy_data/table_class_name_mapper.rb', line 16

def self.method_missing(method_id, *arguments, &block)
  instance.send(method_id, *arguments, &block)
end

Instance Method Details

#class_name_for(table_name) ⇒ Object



51
52
53
# File 'lib/legacy_data/table_class_name_mapper.rb', line 51

def class_name_for table_name
  lookup_class_name(table_name) || compute_class_name(table_name)
end

#clear_dictionaryObject



20
21
22
# File 'lib/legacy_data/table_class_name_mapper.rb', line 20

def clear_dictionary
  @dictionary = nil
end

#compute_class_name(table_name) ⇒ Object



59
60
61
62
63
# File 'lib/legacy_data/table_class_name_mapper.rb', line 59

def compute_class_name table_name
  table_name =~ /#{naming_convention}/i
  stripped_table_name = $1 || table_name
  dictionary[table_name] = LegacyData.conventional_class_name stripped_table_name
end

#dictionary_file_nameObject



47
48
49
# File 'lib/legacy_data/table_class_name_mapper.rb', line 47

def dictionary_file_name
  File.join(Rails.root, 'app', 'models', 'table_mappings.yml')
end

#let_user_validate_dictionaryObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/legacy_data/table_class_name_mapper.rb', line 35

def let_user_validate_dictionary
  save_dictionary
  self.class.log <<-MSG
Done analyzing the tables.  
  Automatic class names written to '#{LegacyData::TableClassNameMapper.dictionary_file_name}'
  Since the database probably does not follow Rails naming conventions you should take a look at the class names and update them in that file. 
  Once you're done hit <enter> to continue generating the models"
  MSG
  gets
  load_dictionary
end

#load_dictionaryObject



24
25
26
27
# File 'lib/legacy_data/table_class_name_mapper.rb', line 24

def load_dictionary
  clear_dictionary
  File.exists?(dictionary_file_name) ? YAML.load_file(dictionary_file_name) : {}
end

#lookup_class_name(table_name) ⇒ Object



55
56
57
# File 'lib/legacy_data/table_class_name_mapper.rb', line 55

def lookup_class_name table_name
  dictionary[table_name]
end

#save_dictionaryObject



29
30
31
32
33
# File 'lib/legacy_data/table_class_name_mapper.rb', line 29

def save_dictionary
  File.open(dictionary_file_name, 'w') do |out|
    YAML.dump(dictionary, out)
  end
end