Sha256: 13903dfe24ae2144c8692f64646a972ccff83c0d582107ba516db55337a976ed

Contents?: true

Size: 1.86 KB

Versions: 13

Compression:

Stored size: 1.86 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

13 entries across 13 versions & 5 rubygems

Version Path
specstar-remarkable-0.0.0 lib/remarkable/active_record/matchers/have_column_matcher.rb
maksar-remarkable_activerecord-4.0.0.alpha6 lib/remarkable/active_record/matchers/have_column_matcher.rb
schubert-remarkable_activerecord-4.0.0.alpha4 lib/remarkable/active_record/matchers/have_column_matcher.rb
benhutton-remarkable_activerecord-4.0.0.alpha6 lib/remarkable/active_record/matchers/have_column_matcher.rb
remarkable_activerecord-4.0.0.alpha4 lib/remarkable/active_record/matchers/have_column_matcher.rb
remarkable_activerecord-4.0.0.alpha3 lib/remarkable/active_record/matchers/have_column_matcher.rb
remarkable_activerecord-4.0.0.alpha2 lib/remarkable/active_record/matchers/have_column_matcher.rb
remarkable_activerecord-4.0.0.alpha1 lib/remarkable_activerecord/matchers/have_column_matcher.rb
remarkable_activerecord-3.1.13 lib/remarkable_activerecord/matchers/have_column_matcher.rb
remarkable_activerecord-3.1.12 lib/remarkable_activerecord/matchers/have_column_matcher.rb
remarkable_activerecord-3.1.11 lib/remarkable_activerecord/matchers/have_column_matcher.rb
remarkable_activerecord-3.1.10 lib/remarkable_activerecord/matchers/have_column_matcher.rb
remarkable_activerecord-3.1.9 lib/remarkable_activerecord/matchers/have_column_matcher.rb