Class: ActiveWorkbench::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/active_workbench/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, id, name) ⇒ Table

Returns a new instance of Table.



36
37
38
39
40
41
42
43
44
# File 'lib/active_workbench/base.rb', line 36

def initialize schema, id, name
  @schema = schema
  @id = id
  @name = name
  @columns_ids ||= {}
  @columns_names ||= {}
  @fks_ids ||= {}
  @fks_names ||= {}
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



34
35
36
# File 'lib/active_workbench/base.rb', line 34

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



34
35
36
# File 'lib/active_workbench/base.rb', line 34

def name
  @name
end

Instance Method Details

#add_column(id, name) ⇒ Object



55
56
57
# File 'lib/active_workbench/base.rb', line 55

def add_column id, name
  @columns_ids[id] = @columns_names[name] = Column.new(self, id, name)
end

#add_fk(id, name, *opts) ⇒ Object



46
47
48
49
# File 'lib/active_workbench/base.rb', line 46

def add_fk id, name, *opts
  opts = *opts
  @fks_ids[id] = @fks_names[name] = FK.new(self, id, name, opts[:many], opts[:assoc])
end

#class_nameObject



75
76
77
# File 'lib/active_workbench/base.rb', line 75

def class_name
  @name.classify
end

#column_by_id(name) ⇒ Object



67
68
69
# File 'lib/active_workbench/base.rb', line 67

def column_by_id name
  @columns_ids[name]
end

#column_by_name(name) ⇒ Object



63
64
65
# File 'lib/active_workbench/base.rb', line 63

def column_by_name name
  @columns_names[name]
end

#columnsObject



59
60
61
# File 'lib/active_workbench/base.rb', line 59

def columns
  @columns_ids.values
end

#fksObject



51
52
53
# File 'lib/active_workbench/base.rb', line 51

def fks
  @fks_ids.values
end

#plural?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/active_workbench/base.rb', line 71

def plural?
  @name == class_name.tableize
end

#to_modelObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/active_workbench/base.rb', line 79

def to_model
  set_table_name = ""
  unless plural?
    # need to set table name with set_table_name()
    set_table_name = "\n  set_table_name(\"#{@name}\")"
  end
  associations = ""
  fks.each do |fk|
    associations << fk.to_assotiation
  end

  "class #{class_name} < ActiveRecord::Base#{set_table_name}#{associations}\nend"
end