Class: SafeAttributesMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/attribute_ext/rspec.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ SafeAttributesMatcher

Returns a new instance of SafeAttributesMatcher.



34
35
36
37
38
# File 'lib/attribute_ext/rspec.rb', line 34

def initialize(attributes)
  @attributes = attributes.map(&:to_s)
  @roles = []
  @safe  = []
end

Instance Method Details

#as(role, name = nil) ⇒ Object Also known as: and_as



40
41
42
43
# File 'lib/attribute_ext/rspec.rb', line 40

def as(role, name = nil)
  @roles << [role, name]
  self
end

#descriptionObject



79
80
81
82
83
84
85
86
# File 'lib/attribute_ext/rspec.rb', line 79

def description
  roles = @roles.any? ? (@roles || []).map { |r,n| n||r.to_s }.join(' and as ') : 'default'
  if @attributes.any?
    "have safe attributes #{@attributes.join(', ')} as #{roles}" 
  else
    "have no safe attributes as #{roles}" 
  end
end

#does_not_match?(model) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/attribute_ext/rspec.rb', line 60

def does_not_match?(model)
  !matches?(model)
end

#failure_message_for_shouldObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/attribute_ext/rspec.rb', line 64

def failure_message_for_should
  output  = "expected safe attributes: #{@attributes.inspect}\n" +
            "but has safe attributes:  #{@safe.inspect}\n"
           
  output += "missing elements are:     #{@missing.inspect}\n" if @missing.any?
  output += "extra elements are:       #{@extra.inspect}\n"   if @extra.any?
  output += "as #{@name || @role.to_s}" unless @role.nil?
  
  output
end

#failure_message_for_should_notObject



75
76
77
# File 'lib/attribute_ext/rspec.rb', line 75

def failure_message_for_should_not
  "WARNING: have_safe_attributes should not be used with should_not."
end

#matches?(model) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/attribute_ext/rspec.rb', line 46

def matches?(model)
  (@roles || [nil, nil]).each do |role, name|
    @role = role
    @name = name
    @safe = model.safe_attribute_names(role)
    
    @missing = (@attributes-@safe)
    @extra   = (@safe-@attributes)
    
    return false if @missing.any? or @extra.any?
  end
  true
end