File: singular_table_names.rb

package info (click to toggle)
ruby-sequel 5.63.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,408 kB
  • sloc: ruby: 113,747; makefile: 3
file content (33 lines) | stat: -rw-r--r-- 1,107 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# frozen-string-literal: true

module Sequel
  module Plugins
    # The singular_table_names plugin changes the default
    # table names for subclasses to not assume a plural version.
    # By default, Sequel assumes table names for models use
    # the plural versions.
    #
    # Note that this plugin only affects subclasses of the
    # class it is loaded into, it does not affect the
    # current class.  So it only makes sense to load this
    # into Sequel::Model itself, or a subclass of Sequel::Model
    # that is created via Class.new.
    #
    # Usage:
    #
    #   # Don't assume pluralized table names
    #   Sequel::Model.plugin :singular_table_names
    module SingularTableNames
      module ClassMethods
        # Returns the implicit table name for the model class, which is the demodulized,
        # underscored, name of the class.
        #
        #   Artist.implicit_table_name # => :artist
        #   Foo::ArtistAlias.implicit_table_name # => :artist_alias
        def implicit_table_name
          underscore(demodulize(name)).to_sym
        end
      end
    end
  end
end