Sha256: 3dc64f587254cb8f2d5822ef92eae3cd07e23816b57c299d2013db6dc66e64be

Contents?: true

Size: 1.99 KB

Versions: 28

Compression:

Stored size: 1.99 KB

Contents

module ActiveRecord
  # :stopdoc:
  module ConnectionAdapters
    # An abstract definition of a column in a table.
    class Column
      attr_reader :name, :default, :sql_type_metadata, :null, :table_name, :default_function, :collation, :comment

      delegate :precision, :scale, :limit, :type, :sql_type, to: :sql_type_metadata, allow_nil: true

      # Instantiates a new column in the table.
      #
      # +name+ is the column's name, such as <tt>supplier_id</tt> in <tt>supplier_id int</tt>.
      # +default+ is the type-casted default value, such as +new+ in <tt>sales_stage varchar(20) default 'new'</tt>.
      # +sql_type_metadata+ is various information about the type of the column
      # +null+ determines if this column allows +NULL+ values.
      def initialize(name, default, sql_type_metadata = nil, null = true, table_name = nil, default_function = nil, collation = nil, comment: nil)
        @name = name.freeze
        @table_name = table_name
        @sql_type_metadata = sql_type_metadata
        @null = null
        @default = default
        @default_function = default_function
        @collation = collation
        @comment = comment
      end

      def has_default?
        !default.nil? || default_function
      end

      def bigint?
        /\Abigint\b/ === sql_type
      end

      # Returns the human name of the column name.
      #
      # ===== Examples
      #  Column.new('sales_stage', ...).human_name # => 'Sales stage'
      def human_name
        Base.human_attribute_name(@name)
      end

      def ==(other)
        other.is_a?(Column) &&
          attributes_for_hash == other.attributes_for_hash
      end
      alias :eql? :==

      def hash
        attributes_for_hash.hash
      end

      protected

      def attributes_for_hash
        [self.class, name, default, sql_type_metadata, null, table_name, default_function, collation]
      end
    end

    class NullColumn < Column
      def initialize(name)
        super(name, nil)
      end
    end
  end
  # :startdoc:
end

Version data entries

28 entries across 28 versions & 4 rubygems

Version Path
activerecord-5.0.6 lib/active_record/connection_adapters/column.rb
activerecord-5.0.6.rc1 lib/active_record/connection_adapters/column.rb
activerecord-5.0.5 lib/active_record/connection_adapters/column.rb
activerecord-5.0.5.rc2 lib/active_record/connection_adapters/column.rb
activerecord-5.0.5.rc1 lib/active_record/connection_adapters/column.rb
activerecord-5.0.4 lib/active_record/connection_adapters/column.rb
activerecord-5.0.4.rc1 lib/active_record/connection_adapters/column.rb
activerecord-5.0.3 lib/active_record/connection_adapters/column.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activerecord-5.0.2/lib/active_record/connection_adapters/column.rb
activerecord-5.0.2 lib/active_record/connection_adapters/column.rb
activerecord-5.0.2.rc1 lib/active_record/connection_adapters/column.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/column.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/column.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/column.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/column.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/column.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/column.rb
autocompl-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/column.rb
abaci-0.3.0 vendor/bundle/gems/activerecord-5.0.0/lib/active_record/connection_adapters/column.rb
activerecord-5.0.1 lib/active_record/connection_adapters/column.rb