An Interest In:
Web News this Week
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
November 9, 2022 04:39 pm GMT
Original Link: https://dev.to/abitrolly/find-delegated-methods-in-ruby-on-rails-96e
Find delegated methods in Ruby on Rails
I needed a way to find delegated methods in GitLab codebase for testing, so I came up with this.
delegated = delegates(project, :ci_cd_settings)
def delegates(fromobj, tosym) # Get map of delegated methods. # Rails doesn't maintain a table of delegated methods, so it needs to be # reconstructed by parsing methods source. regex = /^\s*delegate .*to: \s*:#{tosym}.*/mx delegated = {} fromobj.methods.each do |symname| method = fromobj.method(symname) next unless method.source_location next unless method.source =~ regex name = symname.to_s # Example for `Project.last.method("ci_opt_in_jwt").source`` # " delegate :opt_in_jwt, :opt_in_jwt=, to: :ci_cd_settings, prefix: :ci, allow_nil: true
" prefix_match = method.source.match /prefix: \s+ :(\w+)/mx delegated[name] = if prefix_match name.delete_prefix("#{prefix_match[1]}_") else name end end delegatedend
Original Link: https://dev.to/abitrolly/find-delegated-methods-in-ruby-on-rails-96e
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To