diff --git a/lib/rspec/its.rb b/lib/rspec/its.rb index ad777fd..8e67d11 100644 --- a/lib/rspec/its.rb +++ b/lib/rspec/its.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require 'rspec/its/subject' require 'rspec/its/version' require 'rspec/core' @@ -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) diff --git a/lib/rspec/its/subject.rb b/lib/rspec/its/subject.rb new file mode 100644 index 0000000..0efd63d --- /dev/null +++ b/lib/rspec/its/subject.rb @@ -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