File: subject.rb

package info (click to toggle)
ruby-rspec-its 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 168 kB
  • sloc: ruby: 455; sh: 25; makefile: 2
file content (26 lines) | stat: -rw-r--r-- 636 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# frozen_string_literal: true

module RSpec
  module Its
    # @api private
    # Handles turning subject into an expectation target
    module Subject
      def for(attribute, subject)
        if Array === attribute
          if Hash === subject
            attribute.inject(subject) { |inner, attr| inner[attr] }
          else
            subject[*attribute]
          end
        else
          attribute_chain = attribute.to_s.split('.')
          attribute_chain.inject(subject) do |inner_subject, attr|
            inner_subject.public_send(attr)
          end
        end
      end

      module_function :for
    end
  end
end