Class: ActiveSpec::Specifications::SizeSpecification

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

Overview

Checks that the values for the given attributes are of a particular size. The size can be a precise number (Fixnum) or a Range. If a Range is given, the specification will pass if the value’s size falls within the Range.

Creating a SizeSpecification with a Fixnum is the same as the Rails validates_size_of macro with the :exactly option. Using a Range is the equivalent of using validates_size_of with the :in option.

Instance Method Summary collapse

Constructor Details

#initialize(size, *attributes) ⇒ SizeSpecification

size can be a Fixnum or a Range.



13
14
15
16
# File 'lib/active_spec/specifications/size_specification.rb', line 13

def initialize(size, *attributes)
  @size = size
  super(*attributes)
end

Instance Method Details

#satisfied_by?(object) ⇒ Boolean

Will return false if the object does not respond to any of the specified attributes, or if any of the the values for each attribute does not respond to size. Otherwise it will check that the size of the value for each attribute equals the given size (if size is a Fixnum) or within the given Range.

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/active_spec/specifications/size_specification.rb', line 24

def satisfied_by?(object)
  attributes_satisfy? do |a|
    return false unless object.respond_to?(a)
    return false unless object.send(a).respond_to?(:size)
    case @size
      when Fixnum
        return false unless object.send(a).size == @size
      when Range
        return false unless @size.include?(object.send(a).size)
    end
    true
  end
end