Class: LegacyData::TableClassNameMapper
- Inherits:
-
Object
- Object
- LegacyData::TableClassNameMapper
- Includes:
- Singleton
- Defined in:
- lib/legacy_data/table_class_name_mapper.rb
Instance Attribute Summary collapse
-
#dictionary ⇒ Object
Returns the value of attribute dictionary.
-
#naming_convention ⇒ Object
Returns the value of attribute naming_convention.
Class Method Summary collapse
- .class_name_for(table_name) ⇒ Object
- .dictionary_file_name ⇒ Object
- .load_dictionary ⇒ Object
- .naming_convention=(value) ⇒ Object
- .save_dictionary ⇒ Object
Instance Method Summary collapse
- #compute_class_name(table_name) ⇒ Object
- #dictionary_file_name ⇒ Object
- #load_dictionary ⇒ Object
- #lookup_class_name(table_name) ⇒ Object
- #save_dictionary ⇒ Object
Instance Attribute Details
#dictionary ⇒ Object
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_convention ⇒ Object
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
.class_name_for(table_name) ⇒ Object
42 43 44 |
# File 'lib/legacy_data/table_class_name_mapper.rb', line 42 def self.class_name_for table_name instance.lookup_class_name(table_name) || instance.compute_class_name(table_name) end |
.dictionary_file_name ⇒ Object
23 24 25 |
# File 'lib/legacy_data/table_class_name_mapper.rb', line 23 def self.dictionary_file_name instance.dictionary_file_name end |
.load_dictionary ⇒ Object
17 18 19 |
# File 'lib/legacy_data/table_class_name_mapper.rb', line 17 def self.load_dictionary instance.load_dictionary end |
.naming_convention=(value) ⇒ Object
14 15 16 |
# File 'lib/legacy_data/table_class_name_mapper.rb', line 14 def self.naming_convention= value instance.naming_convention = value end |
.save_dictionary ⇒ Object
20 21 22 |
# File 'lib/legacy_data/table_class_name_mapper.rb', line 20 def self.save_dictionary instance.save_dictionary end |
Instance Method Details
#compute_class_name(table_name) ⇒ Object
50 51 52 53 54 |
# File 'lib/legacy_data/table_class_name_mapper.rb', line 50 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_name ⇒ Object
38 39 40 |
# File 'lib/legacy_data/table_class_name_mapper.rb', line 38 def dictionary_file_name File.join(RAILS_ROOT, 'app', 'models', 'table_mappings.yml') end |
#load_dictionary ⇒ Object
27 28 29 30 |
# File 'lib/legacy_data/table_class_name_mapper.rb', line 27 def load_dictionary @dictionary = nil File.exists?(dictionary_file_name) ? YAML.load_file(dictionary_file_name) : {} end |
#lookup_class_name(table_name) ⇒ Object
46 47 48 |
# File 'lib/legacy_data/table_class_name_mapper.rb', line 46 def lookup_class_name table_name dictionary[table_name] end |
#save_dictionary ⇒ Object
32 33 34 35 36 |
# File 'lib/legacy_data/table_class_name_mapper.rb', line 32 def save_dictionary File.open(dictionary_file_name, 'w') do |out| YAML.dump(dictionary, out) end end |