Sha256: 302caefff882568de0bf1c969d5bc401a8abfd81c07d02b39e80019e9d166946

Contents?: true

Size: 1.93 KB

Versions: 11

Compression:

Stored size: 1.93 KB

Contents

module Remarkable
  module ActiveRecord
    module Matchers
      class HaveColumnMatcher < Remarkable::ActiveRecord::Base #:nodoc:
        arguments :collection => :columns, :as => :column

        optional :type, :default, :precision, :limit, :scale, :sql_type
        optional :primary, :null, :default => true

        collection_assertions :column_exists?, :options_match?

        before_assert do
          @options.each{|k,v| @options[k] = v.to_s}
        end

        protected

          def column_exists?
            !column_type.nil?
          end

          def options_match?
            actual = get_column_options(column_type, @options.keys)
            return actual == @options, :actual => actual.inspect
          end

          def column_type
            subject_class.columns.detect {|c| c.name == @column.to_s }
          end

          def get_column_options(column, keys)
            keys.inject({}) do |hash, key|
              hash[key] = column.instance_variable_get("@#{key}").to_s
              hash
            end
          end

          def interpolation_options
            { :options => @options.inspect }
          end

      end

      # Ensures that a column of the database actually exists.
      #
      # == Options
      # 
      # * All options available in migrations are available:
      #
      #   :type, :default, :precision, :limit, :scale, :sql_type, :primary, :null
      #
      # == Examples
      #
      #   should_have_column :name, :type => :string, :default => ''
      #
      #   it { should have_column(:name, :type => :string) }
      #   it { should have_column(:name).type(:string) }
      # 
      def have_column(*args, &block)
        HaveColumnMatcher.new(*args, &block).spec(self)
      end
      alias :have_columns    :have_column
      alias :have_db_column  :have_column
      alias :have_db_columns :have_column

    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
remarkable_activerecord-3.1.7 lib/remarkable_activerecord/matchers/have_column_matcher.rb
remarkable_activerecord-3.1.8 lib/remarkable_activerecord/matchers/have_column_matcher.rb
remarkable_activerecord-3.0.10 lib/remarkable_activerecord/matchers/have_column_matcher.rb
remarkable_activerecord-3.1.2 lib/remarkable_activerecord/matchers/have_column_matcher.rb
remarkable_activerecord-3.1.1 lib/remarkable_activerecord/matchers/have_column_matcher.rb
remarkable_activerecord-3.1.4 lib/remarkable_activerecord/matchers/have_column_matcher.rb
remarkable_activerecord-3.1.3 lib/remarkable_activerecord/matchers/have_column_matcher.rb
remarkable_activerecord-3.0.9 lib/remarkable_activerecord/matchers/have_column_matcher.rb
remarkable_activerecord-3.1.0 lib/remarkable_activerecord/matchers/have_column_matcher.rb
remarkable_activerecord-3.1.6 lib/remarkable_activerecord/matchers/have_column_matcher.rb
remarkable_activerecord-3.1.5 lib/remarkable_activerecord/matchers/have_column_matcher.rb