# File lib/rare_map/table.rb, line 40 def primary_key return @primary_key if @primary_key return 'id' if @id candidates = @columns.find_all { |col| col.unique }.map { |col| col.name } return 'id' if candidates.include? 'id' candidates.find { |c| c =~ eval("/^#{@name}.*id$/i") } || candidates.find { |c| c =~ eval("/^#{singularize}.*id$/i") } || candidates.find { |c| c =~ eval("/^#{pluralize}.*id$/i") } || candidates.first end
class RareMap::Table
RareMap::Table
defines a table of a database. @author Wei-Ming Wu @!attribute [r] name
@return [String] the name of this Table
@!attribute [r] id
@return [true, false] true if this Table has id, false otherwise
@!attribute primary_key
@return [String] the primary key of this Table
@!attribute fk_suffix
@return [String] the foreign key suffix of this Table
@!attribute columns
@return [Array] an Array of Column of this Table
Attributes
columns[RW]
fk_suffix[RW]
id[R]
name[R]
primary_key[W]
Public Class Methods
new(name, opts = {})
click to toggle source
Creates a Table
.
@param name [String] the name of this Table
@param opts [Hash] the options of this Table
@option opts [true, false] :id the id existence of this Table
@option opts [String] :primary_key the primary key of this Table
@option opts [String] :fk_suffix the foreign key suffix of this Table
@return [Table] a Table
object
# File lib/rare_map/table.rb, line 29 def initialize(name, opts = {}) @name = name @id = opts[:id] != false @primary_key = opts[:primary_key] @columns = [] @fk_suffix = opts[:fk_suffix] || 'id' end
Public Instance Methods
match_foreign_key(column)
click to toggle source
Returns the name of this Table
if given Column
matched.
@return [String, nil] the name of this Table
if given Column
matched, nil otherwise
# File lib/rare_map/table.rb, line 69 def match_foreign_key(column) if column.ref_table == @name || foreign_keys.include?(column.name.downcase) @name if primary_key end end
match_foreign_key_by_primary_key(pk)
click to toggle source
pluralize()
click to toggle source
primary_key()
click to toggle source
Returns the primary key of this Table
.
@return [String, nil] the primary key of this Table
Private Instance Methods
foreign_keys()
click to toggle source
# File lib/rare_map/table.rb, line 84 def foreign_keys ["#{@name}_#{fk_suffix}", "#{@name}#{fk_suffix}", "#{singularize}_#{fk_suffix}", "#{singularize}#{fk_suffix}", "#{pluralize}_#{fk_suffix}", "#{pluralize}#{fk_suffix}"].map(&:downcase) end