Sha256: d41c8839b4be3badaf39174e1fb86ed3c8ec2de0e75f377ed3491573cf6c57f7

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

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

      def of_type(type, options={})
        @options[:type] = type
        @options[:member_type] = options.delete(:member_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 member_type
        attribute.options[:member_type]
      end

      def attribute_type
        attribute.options[:primitive]
      end

      def attribute_exists?
        attribute != nil
      end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rspec-virtus-0.2.0 lib/rspec-virtus/matcher.rb
rspec-virtus-0.1.1 lib/rspec-virtus/matcher.rb
rspec-virtus-0.1.0 lib/rspec-virtus/matcher.rb