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

.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



37
38
39
# File 'lib/legacy_data/table_class_name_mapper.rb', line 37

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



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

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.downcase.pluralize)
end

#dictionary_file_nameObject



33
34
35
# File 'lib/legacy_data/table_class_name_mapper.rb', line 33

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

#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



41
42
43
# File 'lib/legacy_data/table_class_name_mapper.rb', line 41

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