Sha256: 4c39aa6cb42aef7f1c71b1587e161b6d2d773752dc9e0982777ff465715d55e3

Contents?: true

Size: 932 Bytes

Versions: 1

Compression:

Stored size: 932 Bytes

Contents

module RSpec
  module Virtus
    class Matcher
      def initialize(attribute_name)
        @attribute_name = attribute_name
        @options = {}
      end

      def of_type(type)
        @options[:type] = type
        self
      end

      def matches?(subject)
        @subject = subject
        attribute_exists? && type_correct?
      end

      def failure_message
        "expected #{@attribute_name} to be defined"
      end

      def negative_failure_message
        "expected #{@attribute_name} not to be defined"
      end

      private

      def attribute
        @subject.attribute_set[@attribute_name]
      end

      def attribute_type
        attribute.options[:primitive]
      end

      def attribute_exists?
        attribute != nil
      end

      def type_correct?
        if @options[:type]
          attribute_type == @options[:type]
        else
          true
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rspec-virtus-0.0.1 lib/rspec-virtus/matcher.rb