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.



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

def dictionary
  @dictionary
end

#naming_conventionObject

Returns the value of attribute naming_convention.



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

def naming_convention
  @naming_convention
end

Class Method Details

.log(msg) ⇒ Object



63
64
65
# File 'lib/legacy_data/table_class_name_mapper.rb', line 63

def self.log msg
  puts msg
end

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



14
15
16
# File 'lib/legacy_data/table_class_name_mapper.rb', line 14

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

Instance Method Details

#class_name_for(table_name) ⇒ Object



49
50
51
# File 'lib/legacy_data/table_class_name_mapper.rb', line 49

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

#clear_dictionaryObject



18
19
20
# File 'lib/legacy_data/table_class_name_mapper.rb', line 18

def clear_dictionary
  @dictionary = nil
end

#compute_class_name(table_name) ⇒ Object



57
58
59
60
61
# File 'lib/legacy_data/table_class_name_mapper.rb', line 57

def compute_class_name table_name
  table_name =~ /#{naming_convention}/i
  stripped_table_name = $1 || table_name
  dictionary[table_name] = ActiveRecord::Base.class_name(stripped_table_name.underscore.downcase.pluralize)
end

#dictionary_file_nameObject



45
46
47
# File 'lib/legacy_data/table_class_name_mapper.rb', line 45

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

#let_user_validate_dictionaryObject



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

def let_user_validate_dictionary
  save_dictionary
  self.class.log "Done analyzing the tables.  \n  Automatic class names written to '\#{LegacyData::TableClassNameMapper.dictionary_file_name}'\n  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. \n  Once you're done hit <enter> to continue generating the models\"\n  MSG\n  gets\n  load_dictionary\nend\n"

#load_dictionaryObject



22
23
24
25
# File 'lib/legacy_data/table_class_name_mapper.rb', line 22

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

#lookup_class_name(table_name) ⇒ Object



53
54
55
# File 'lib/legacy_data/table_class_name_mapper.rb', line 53

def lookup_class_name table_name
  dictionary[table_name]
end

#save_dictionaryObject



27
28
29
30
31
# File 'lib/legacy_data/table_class_name_mapper.rb', line 27

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