Class: RareMap::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/rare_map/table.rb

Overview

RareMap::Table defines a table of a database.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ Table

Creates a Table.

Parameters:

  • the name of this Table

  • (defaults to: {})

    the options of this Table

Options Hash (opts):

  • :id (true, false)

    the id existence of this Table

  • :primary_key (String)

    the primary key of this Table

  • :fk_suffix (String)

    the foreign key suffix of this Table



29
30
31
32
33
34
35
# 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

Instance Attribute Details

#columnsArray

Returns an Array of Column of this Table.

Returns:

  • an Array of Column of this Table



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rare_map/table.rb', line 16

class Table
  attr_reader :name, :id
  attr_writer :primary_key
  attr_accessor :fk_suffix, :columns
  
  # 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
  def initialize(name, opts = {})
    @name = name
    @id = opts[:id] != false
    @primary_key = opts[:primary_key]
    @columns = []
    @fk_suffix = opts[:fk_suffix] || 'id'
  end
  
  # Returns the primary key of this Table.
  #
  # @return [String, nil] the primary key of this Table
  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
  
  # Returns the singular name of this Table.
  #
  # @return [String] the singular name of this Table
  def singularize
    @name.pluralize.singularize
  end
  
  # Returns the plural name of this Table.
  #
  # @return [String] the plural name of this Table
  def pluralize
    @name.pluralize
  end
  
  # Returns the name of this Table if given Column matched.
  #
  # @return [String, nil] the name of this Table if given Column matched, nil otherwise
  def match_foreign_key(column)
    if column.ref_table == @name || foreign_keys.include?(column.name.downcase)
      @name if primary_key
    end
  end
  
  # Returns the name of this Table if given primary key matched.
  #
  # @return [String, nil] the name of this Table if given primary key matched, nil otherwise
  def match_foreign_key_by_primary_key(pk)
    @name if foreign_keys.include?(pk.to_s.downcase) && primary_key
  end
  
  private
  
  def foreign_keys
    ["#{@name}_#{fk_suffix}",       "#{@name}#{fk_suffix}",
     "#{singularize}_#{fk_suffix}", "#{singularize}#{fk_suffix}",
     "#{pluralize}_#{fk_suffix}",   "#{pluralize}#{fk_suffix}"].map(&:downcase)
  end
end

#fk_suffixString

Returns the foreign key suffix of this Table.

Returns:

  • the foreign key suffix of this Table



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rare_map/table.rb', line 16

class Table
  attr_reader :name, :id
  attr_writer :primary_key
  attr_accessor :fk_suffix, :columns
  
  # 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
  def initialize(name, opts = {})
    @name = name
    @id = opts[:id] != false
    @primary_key = opts[:primary_key]
    @columns = []
    @fk_suffix = opts[:fk_suffix] || 'id'
  end
  
  # Returns the primary key of this Table.
  #
  # @return [String, nil] the primary key of this Table
  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
  
  # Returns the singular name of this Table.
  #
  # @return [String] the singular name of this Table
  def singularize
    @name.pluralize.singularize
  end
  
  # Returns the plural name of this Table.
  #
  # @return [String] the plural name of this Table
  def pluralize
    @name.pluralize
  end
  
  # Returns the name of this Table if given Column matched.
  #
  # @return [String, nil] the name of this Table if given Column matched, nil otherwise
  def match_foreign_key(column)
    if column.ref_table == @name || foreign_keys.include?(column.name.downcase)
      @name if primary_key
    end
  end
  
  # Returns the name of this Table if given primary key matched.
  #
  # @return [String, nil] the name of this Table if given primary key matched, nil otherwise
  def match_foreign_key_by_primary_key(pk)
    @name if foreign_keys.include?(pk.to_s.downcase) && primary_key
  end
  
  private
  
  def foreign_keys
    ["#{@name}_#{fk_suffix}",       "#{@name}#{fk_suffix}",
     "#{singularize}_#{fk_suffix}", "#{singularize}#{fk_suffix}",
     "#{pluralize}_#{fk_suffix}",   "#{pluralize}#{fk_suffix}"].map(&:downcase)
  end
end

#idtrue, false (readonly)

Returns true if this Table has id, false otherwise.

Returns:

  • true if this Table has id, false otherwise



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rare_map/table.rb', line 16

class Table
  attr_reader :name, :id
  attr_writer :primary_key
  attr_accessor :fk_suffix, :columns
  
  # 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
  def initialize(name, opts = {})
    @name = name
    @id = opts[:id] != false
    @primary_key = opts[:primary_key]
    @columns = []
    @fk_suffix = opts[:fk_suffix] || 'id'
  end
  
  # Returns the primary key of this Table.
  #
  # @return [String, nil] the primary key of this Table
  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
  
  # Returns the singular name of this Table.
  #
  # @return [String] the singular name of this Table
  def singularize
    @name.pluralize.singularize
  end
  
  # Returns the plural name of this Table.
  #
  # @return [String] the plural name of this Table
  def pluralize
    @name.pluralize
  end
  
  # Returns the name of this Table if given Column matched.
  #
  # @return [String, nil] the name of this Table if given Column matched, nil otherwise
  def match_foreign_key(column)
    if column.ref_table == @name || foreign_keys.include?(column.name.downcase)
      @name if primary_key
    end
  end
  
  # Returns the name of this Table if given primary key matched.
  #
  # @return [String, nil] the name of this Table if given primary key matched, nil otherwise
  def match_foreign_key_by_primary_key(pk)
    @name if foreign_keys.include?(pk.to_s.downcase) && primary_key
  end
  
  private
  
  def foreign_keys
    ["#{@name}_#{fk_suffix}",       "#{@name}#{fk_suffix}",
     "#{singularize}_#{fk_suffix}", "#{singularize}#{fk_suffix}",
     "#{pluralize}_#{fk_suffix}",   "#{pluralize}#{fk_suffix}"].map(&:downcase)
  end
end

#nameString (readonly)

Returns the name of this Table.

Returns:

  • the name of this Table



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rare_map/table.rb', line 16

class Table
  attr_reader :name, :id
  attr_writer :primary_key
  attr_accessor :fk_suffix, :columns
  
  # 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
  def initialize(name, opts = {})
    @name = name
    @id = opts[:id] != false
    @primary_key = opts[:primary_key]
    @columns = []
    @fk_suffix = opts[:fk_suffix] || 'id'
  end
  
  # Returns the primary key of this Table.
  #
  # @return [String, nil] the primary key of this Table
  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
  
  # Returns the singular name of this Table.
  #
  # @return [String] the singular name of this Table
  def singularize
    @name.pluralize.singularize
  end
  
  # Returns the plural name of this Table.
  #
  # @return [String] the plural name of this Table
  def pluralize
    @name.pluralize
  end
  
  # Returns the name of this Table if given Column matched.
  #
  # @return [String, nil] the name of this Table if given Column matched, nil otherwise
  def match_foreign_key(column)
    if column.ref_table == @name || foreign_keys.include?(column.name.downcase)
      @name if primary_key
    end
  end
  
  # Returns the name of this Table if given primary key matched.
  #
  # @return [String, nil] the name of this Table if given primary key matched, nil otherwise
  def match_foreign_key_by_primary_key(pk)
    @name if foreign_keys.include?(pk.to_s.downcase) && primary_key
  end
  
  private
  
  def foreign_keys
    ["#{@name}_#{fk_suffix}",       "#{@name}#{fk_suffix}",
     "#{singularize}_#{fk_suffix}", "#{singularize}#{fk_suffix}",
     "#{pluralize}_#{fk_suffix}",   "#{pluralize}#{fk_suffix}"].map(&:downcase)
  end
end

#primary_keyString?

Returns the primary key of this Table.

Returns:

  • the primary key of this Table



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rare_map/table.rb', line 16

class Table
  attr_reader :name, :id
  attr_writer :primary_key
  attr_accessor :fk_suffix, :columns
  
  # 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
  def initialize(name, opts = {})
    @name = name
    @id = opts[:id] != false
    @primary_key = opts[:primary_key]
    @columns = []
    @fk_suffix = opts[:fk_suffix] || 'id'
  end
  
  # Returns the primary key of this Table.
  #
  # @return [String, nil] the primary key of this Table
  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
  
  # Returns the singular name of this Table.
  #
  # @return [String] the singular name of this Table
  def singularize
    @name.pluralize.singularize
  end
  
  # Returns the plural name of this Table.
  #
  # @return [String] the plural name of this Table
  def pluralize
    @name.pluralize
  end
  
  # Returns the name of this Table if given Column matched.
  #
  # @return [String, nil] the name of this Table if given Column matched, nil otherwise
  def match_foreign_key(column)
    if column.ref_table == @name || foreign_keys.include?(column.name.downcase)
      @name if primary_key
    end
  end
  
  # Returns the name of this Table if given primary key matched.
  #
  # @return [String, nil] the name of this Table if given primary key matched, nil otherwise
  def match_foreign_key_by_primary_key(pk)
    @name if foreign_keys.include?(pk.to_s.downcase) && primary_key
  end
  
  private
  
  def foreign_keys
    ["#{@name}_#{fk_suffix}",       "#{@name}#{fk_suffix}",
     "#{singularize}_#{fk_suffix}", "#{singularize}#{fk_suffix}",
     "#{pluralize}_#{fk_suffix}",   "#{pluralize}#{fk_suffix}"].map(&:downcase)
  end
end

Instance Method Details

#match_foreign_key(column) ⇒ String?

Returns the name of this Table if given Column matched.

Returns:

  • the name of this Table if given Column matched, nil otherwise



69
70
71
72
73
# 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) ⇒ String?

Returns the name of this Table if given primary key matched.

Returns:

  • the name of this Table if given primary key matched, nil otherwise



78
79
80
# File 'lib/rare_map/table.rb', line 78

def match_foreign_key_by_primary_key(pk)
  @name if foreign_keys.include?(pk.to_s.downcase) && primary_key
end

#pluralizeString

Returns the plural name of this Table.

Returns:

  • the plural name of this Table



62
63
64
# File 'lib/rare_map/table.rb', line 62

def pluralize
  @name.pluralize
end

#singularizeString

Returns the singular name of this Table.

Returns:

  • the singular name of this Table



55
56
57
# File 'lib/rare_map/table.rb', line 55

def singularize
  @name.pluralize.singularize
end