Class: LegacyData::TableDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/legacy_data/table_definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options)
  options.each {|key, value| self.send("#{key}=", value) }
end

Instance Attribute Details

#class_nameObject

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

#columnsObject

Returns the value of attribute columns.



3
4
5
# File 'lib/legacy_data/table_definition.rb', line 3

def columns
  @columns
end

#constraintsObject

Returns the value of attribute constraints.



3
4
5
# File 'lib/legacy_data/table_definition.rb', line 3

def constraints
  @constraints
end

#primary_keyObject

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

#relationsObject

Returns the value of attribute relations.



3
4
5
# File 'lib/legacy_data/table_definition.rb', line 3

def relations
  @relations
end

#table_nameObject

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_relationsObject



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_tablesObject



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

Returns:

  • (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_hashObject



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