Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 9, 2022 04:39 pm GMT

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:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To