Skip to content

Commit

Permalink
Refactor subject capture
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed Nov 3, 2024
1 parent 04267e9 commit f5d6493
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
16 changes: 2 additions & 14 deletions lib/rspec/its.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'rspec/its/subject'
require 'rspec/its/version'
require 'rspec/core'

Expand Down Expand Up @@ -128,20 +129,7 @@ def its(attribute, *options, &block)
its_caller = caller.grep_v(%r{/lib/rspec/its})

describe(attribute.to_s, caller: its_caller) do
let(:__its_subject) do
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.send(attr)
end
end
end
let(:__its_subject) { RSpec::Its::Subject.for(attribute, subject) }

def is_expected
expect(__its_subject)
Expand Down
26 changes: 26 additions & 0 deletions lib/rspec/its/subject.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,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.send(attr)
end
end
end

module_function :for
end
end
end

0 comments on commit f5d6493

Please sign in to comment.