Class: RareMap::Table
- Inherits:
-
Object
- Object
- RareMap::Table
- Defined in:
- lib/rare_map/table.rb
Overview
RareMap::Table defines a table of a database.
Instance Attribute Summary collapse
-
#columns ⇒ Array
An Array of Column of this Table.
-
#fk_suffix ⇒ String
The foreign key suffix of this Table.
-
#id ⇒ true, false
readonly
True if this Table has id, false otherwise.
-
#name ⇒ String
readonly
The name of this Table.
-
#primary_key ⇒ String?
Returns the primary key of this Table.
Instance Method Summary collapse
-
#initialize(name, opts = {}) ⇒ Table
constructor
Creates a Table.
-
#match_foreign_key(column) ⇒ String?
Returns the name of this Table if given Column matched.
-
#match_foreign_key_by_primary_key(pk) ⇒ String?
Returns the name of this Table if given primary key matched.
-
#pluralize ⇒ String
Returns the plural name of this Table.
-
#singularize ⇒ String
Returns the singular name of this Table.
Constructor Details
#initialize(name, opts = {}) ⇒ Table
Creates a 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
#columns ⇒ Array
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_suffix ⇒ String
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 |
#id ⇒ true, false (readonly)
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 |
#name ⇒ String (readonly)
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_key ⇒ String?
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.
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.
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 |
#pluralize ⇒ String
Returns the plural name of this Table.
62 63 64 |
# File 'lib/rare_map/table.rb', line 62 def pluralize @name.pluralize end |
#singularize ⇒ String
Returns the singular name of this Table.
55 56 57 |
# File 'lib/rare_map/table.rb', line 55 def singularize @name.pluralize.singularize end |