Sha256: 29859dd09e458aaff498bfeb91039441607fee42f9308859d4d0781a62d0104e

Contents?: true

Size: 977 Bytes

Versions: 3

Compression:

Stored size: 977 Bytes

Contents

require 'rspec'

RSpec::Matchers.define :have_default_value_for do |attribute|
  match do
    @attribute    = attribute.to_s
    @values       = klass._default_attribute_values
    @disallow_nil = klass._default_attribute_values_not_allowing_nil

    match?
  end

  chain :with_value do |value|
    @expected_value = value
  end

  chain :and_allow_nil do
    @expected_disallow_nil = false
  end

  chain :and_disallow_nil do
    @expected_disallow_nil = true
  end

  private

  def klass
    subject.class
  end

  def match?
    have_value? && have_expected_value? && disallows_nil?
  end

  def have_value?
    !@values[@attribute].nil?
  end

  def have_expected_value?
    if @expected_value.nil?
      true
    else
      @values[@attribute].instance_variable_get(:@value) == @expected_value
    end
  end

  def disallows_nil?
    if @expected_disallow_nil.nil?
      true
    else
      @disallow_nil.include?(@attribute) == @expected_disallow_nil
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
default_value_for-matchers-0.2.1 lib/default_value_for/matchers.rb
default_value_for-matchers-0.2.0 lib/default_value_for/matchers.rb
default_value_for-matchers-0.1.1 lib/default_value_for/matchers.rb