Class: Mongoid::Matchers::Document::HaveFieldMatcher

Inherits:
Matcher
  • Object
show all
Defined in:
lib/matchers/document/have_field.rb

Instance Method Summary collapse

Methods included from Helpers

#class_of, #to_sentence

Constructor Details

#initialize(*fields) ⇒ HaveFieldMatcher

Returns a new instance of HaveFieldMatcher.



5
6
7
# File 'lib/matchers/document/have_field.rb', line 5

def initialize(*fields)
  @fields = fields.collect(&:to_s)
end

Instance Method Details

#descriptionObject



54
55
56
57
58
59
60
# File 'lib/matchers/document/have_field.rb', line 54

def description
  desc = "have #{@fields.size > 1 ? 'fields' : 'field'} named"
  desc << " #{to_sentence(@fields)}"
  desc << " of type #{@type.inspect}" if @type
  desc << " with default value of #{@default.inspect}" if !@default.nil?
  desc
end

#failure_messageObject



45
46
47
# File 'lib/matchers/document/have_field.rb', line 45

def failure_message
  "#{@klass} to #{description}, got #{@errors.to_sentence}"
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/matchers/document/have_field.rb', line 19

def matches?(subject)
  @klass  = class_of(subject)
  @errors = []

  @fields.each do |field|
    if @klass.fields.include?(field)
      error = ""
      result_field = @klass.fields[field]

      if check_type_with(result_field.type)
        error << " of type #{result_field.type.inspect}"
      end

      if check_default_with(result_field.default_val)
        error << " with default value of #{result_field.default_val.inspect}"
      end

      @errors << "field #{field.inspect << error}" if !error.blank?
    else
      @errors << "no field named #{field.inspect}"
    end
  end

  @errors.empty?
end

#negative_failure_messageObject



49
50
51
52
# File 'lib/matchers/document/have_field.rb', line 49

def negative_failure_message
  msg = "#{@klass.inspect} to not #{description}, "
  msg << "got #{@klass.inspect} to #{description}"
end

#of_type(type) ⇒ Object



9
10
11
12
# File 'lib/matchers/document/have_field.rb', line 9

def of_type(type)
  @type = type
  self
end

#with_default_value(default) ⇒ Object



14
15
16
17
# File 'lib/matchers/document/have_field.rb', line 14

def with_default_value(default)
  @default = default
  self
end