Class: SafeAttributesMatcher
- Inherits:
-
Object
- Object
- SafeAttributesMatcher
- Defined in:
- lib/attribute_ext/rspec.rb
Overview
:nodoc:
Instance Method Summary collapse
- #as(role, name = nil) ⇒ Object (also: #and_as)
- #description ⇒ Object
- #does_not_match?(model) ⇒ Boolean
- #failure_message_for_should ⇒ Object
- #failure_message_for_should_not ⇒ Object
-
#initialize(attributes) ⇒ SafeAttributesMatcher
constructor
A new instance of SafeAttributesMatcher.
- #matches?(model) ⇒ Boolean
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 |
#description ⇒ Object
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
60 61 62 |
# File 'lib/attribute_ext/rspec.rb', line 60 def does_not_match?(model) !matches?(model) end |
#failure_message_for_should ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/attribute_ext/rspec.rb', line 64 def 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_not ⇒ Object
75 76 77 |
# File 'lib/attribute_ext/rspec.rb', line 75 def "WARNING: have_safe_attributes should not be used with should_not." end |
#matches?(model) ⇒ 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 |