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.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #belongs_to_relations ⇒ Object
- #belongs_to_tables ⇒ Object
- #convert_has_many_to_habtm(join_table) ⇒ Object
-
#initialize(options) ⇒ TableDefinition
constructor
A new instance of TableDefinition.
- #join_table? ⇒ Boolean
- #to_hash ⇒ 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 |
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
23 24 25 26 |
# File 'lib/legacy_data/table_definition.rb', line 23 def belongs_to_relations return {} if relations.nil? or relations[:belongs_to].nil? relations[:belongs_to] end |
#belongs_to_tables ⇒ Object
28 29 30 31 |
# File 'lib/legacy_data/table_definition.rb', line 28 def belongs_to_tables return [] if belongs_to_relations == {} belongs_to_relations.keys end |
#convert_has_many_to_habtm(join_table) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/legacy_data/table_definition.rb', line 33 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], :association_foreign_key=>join_table.belongs_to_relations[other_table_name], :join_table =>join_table.table_name.to_sym } relations[:has_some].delete(join_table.table_name) end |
#join_table? ⇒ Boolean
19 20 21 |
# File 'lib/legacy_data/table_definition.rb', line 19 def join_table? (columns.size == 2) and relations[:belongs_to] and (relations[:belongs_to].values.map(&:to_s) == columns) end |
#to_hash ⇒ Object
13 14 15 16 17 |
# File 'lib/legacy_data/table_definition.rb', line 13 def to_hash hash = {} [:class_name, :table_name, :columns, :primary_key, :relations, :constraints].each {|field| hash[field] = self.send(field) } hash end |