Class: ActiveSpec::Specifications::PresenceSpecification

Inherits:
AttributesSpecification show all
Defined in:
lib/active_spec/specifications/presence_specification.rb

Overview

This specification will check that the values for each attribute are neither nil, or empty.

Example usage:

include ActiveSpec::Specifications

class Foo
  attr_accessor :bar
end

spec = PresenceSpecification.new(:bar)
foo = Foo.new
spec.satisfied_by?(foo) # returns false
foo.bar = ''
spec.satisfied_by?(foo) # returns false
foo.bar = 'baz'
spec.satisfied_by?(foo) # returns true

This works in the same way as the Rails validation macro validates_presence_of.

Instance Method Summary collapse

Methods inherited from AttributesSpecification

#initialize

Constructor Details

This class inherits a constructor from ActiveSpec::Specifications::AttributesSpecification

Instance Method Details

#satisfied_by?(object) ⇒ Boolean

Checks that the value for each attribute on the given object is neither nil or empty. It will also return false if the object fails to respond to any attribute.

Returns:

  • (Boolean)


27
28
29
30
31
32
33
# File 'lib/active_spec/specifications/presence_specification.rb', line 27

def satisfied_by?(object)
  attributes_satisfy? do |a|
    return false unless object.respond_to?(a)
    return false if object.send(a).nil?
    return false if object.send(a).empty?
  end
end