JayTarka
JayTarka

Reputation: 571

Find out where information comes form

I'm piecing together how the rspec-core gem works and I've come across a brick wall. This is inside the Example class:

def description
   description = if metadata[:description].to_s.empty?
                    location_description
                  else
                    metadata[:description]
                  end

    RSpec.configuration.format_docstrings_block.call(description)
end

I'm finding it impossible to find out where the hash inside metadata actually comes from.

I've tried ctags - it directed me towards ExampleGroup#metadata which is a method that returns a hash pertaining to groups; it's not the same hash that's inside the metadata call in the example. Disappointed with ctags - I was expecting the speed by which I piece together how gems work to experience a quantum leap if ctags worked as advertised.

How is anyone supposed to find out where the information returned by a call comes from?

Personally I find it pretty weird, inside the example class there is no def metadata nor metadata = {} and yet there the metadata call sits within #description, returning a hash. There is a attr_reader :metadata, but that's "outgoing". That's if something outside the class wants to access an Example instance' metadata (even though it doesn't contain a metadata method but whatever); attr_reader has nothing to do with setting. So I guess the only explanation is that metadata is a method defined in some other class? Be awesome if ctags could actually do its job and direct me to said method.

Is something like this fictional where_is_itmethod possible? This would be brilliant:

def description
     where_is_it metadata #=> rspec-core/lib/some_support_class.rb:132
end

Upvotes: 0

Views: 35

Answers (1)

Aleksey Shein
Aleksey Shein

Reputation: 7482

Try this: p method(:metadata).source_location in a place where this method is accessible (i.e. callable). That should give you an array with path and line number, like this:

Loading development environment (Rails 4.2.1)
[1] pry(main)> app.method(:get).source_location
=> ["/Users/alexey/.rvm/gems/ruby-2.2.0@pgm-rails2/gems/actionpack-4.2.1/lib/action_dispatch/testing/integration.rb", 31]

Upvotes: 1

Related Questions