Sha256: a9e733cb8dffa99c3df8791f0de6dbfd47c31e28d351cd5563264dcfe0efe07d

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

module Remarkable::Mongoid
  module Matchers

    # Specify the document should have a field
    #
    # examples:
    #  it { should have_field :name }
    #  it { should have_field :age, :type => Integer, :default => 0 }
    #
    # @param [Symbol, Hash] options
    # @option options [Class] :type The data type
    # @option options :default The default value for this field
    #
    # @return [Remarkable::Mongoid::Matchers::HasFieldMatcher]
    def have_field(field, options = {})
      HasFieldMatcher.new(field, options)
    end

    class HasFieldMatcher
      attr_accessor :field, :options

      def initialize(field, options)
        self.field   = field.to_s
        self.options = { :type => Object }.merge(options)
      end

      def matches?(subject)
        @subject = subject.is_a?(Class) ? subject : subject.class
        @subject.fields.has_key?(field) && (@subject.fields[field].type == options[:type])
      end

      def description
        "have field #{field} #{humanized_options}"
      end

      def failure_message_for_should
        "expected #{@subject} to have field #{field} #{humanized_options}"
      end

      def failure_message_for_should_not
        "expected #{@subject} to not have field #{field} #{humanized_options}"
      end

      private

      def humanized_options
        "of type #{options[:type]}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
remarkable_mongoid-0.6.0 lib/remarkable/mongoid/fields.rb