Upgrade to Pro — share decks privately, control downloads, hide ads and more …

システム開発を支えるメタプログラミングの技術 / kaigionrails-2022

hogucc
October 21, 2022

システム開発を支えるメタプログラミングの技術 / kaigionrails-2022

Kaigi on Rails 2022で発表した「システム開発を支えるメタプログラミングの技術」のスライドです

https://kaigionrails.org/2022/talks/hogucc/

hogucc

October 21, 2022
Tweet

More Decks by hogucc

Other Decks in Programming

Transcript

  1. gem

  2. Ruby 2 send, public_send eval, instance_eval, module_eval, class_eval de fi

    ne_method, de fi ne_singleton_method, method_missing
  3. current_user 🔍 module ActionDispatch::Routing class RouteSet prepend Devise::RouteSet end class

    Mapper def devise_for(*resources) … options = resources.extract_options! … resources.map!(&:to_sym) resources.each do |resource| mapping = Devise.add_mapping(resource, options) … /lib/devise/rails/routes.rb devise_for :users, skip: :all
  4. module ActionDispatch::Routing class RouteSet prepend Devise::RouteSet end class Mapper def

    devise_for(*resources) … resources.each do |resource| mapping = Devise.add_mapping(resource, options) … /lib/devise/rails/routes.rb devise_for :users, skip: :all resources current_user 🔍
  5. module ActionDispatch::Routing def self.add_mapping(resource, options) mapping = Devise::Mapping.new(resource, options) …

    @@helpers.each { |h| h.de fi ne_helpers(mapping) } mapping end … current_user 🔍
  6. def self.de fi ne_helpers(mapping) mapping = mapping.name class_eval <<-METHODS, __FILE__,

    __LINE__ + 1 … def current_#{mapping} @current_#{mapping} ||= warden.authenticate(scope: :#{mapping}) end … METHODS … end lib/devise/controllers/helpers.rb current_user current_user 🔍
  7. gem

  8. lib/site_settings/deprecated_settings.rb module SiteSettings::DeprecatedSettings … def setup_deprecated_methods SETTINGS.each do |old_setting, new_setting,

    override, version| … de fi ne_singleton_method old_setting do |warn: true| If warn Discourse.deprecate( `SiteSetting.#{old_setting}` has been deprecated. Please use `SiteSetting.#{new_setting}` instead. , drop_from: version ) end self.public_send(override ? new_setting : "_#{old_setting}") end … de fi ne_singleton_method
  9. module SiteSettings::DeprecatedSettings SETTINGS = [ # [<old setting>, <new_setting>, <override>,

    <version to drop>] [ search_tokenize_chinese_japanese_korean', 'search_tokenize_chinese', true, 2 . 9 '], ['default_categories_regular', 'default_categories_normal', true, 3 . 0 '], ] def setup_deprecated_methods SETTINGS.each do |old_setting, new_setting, override, version| de fi ne_singleton_method old_setting do |warn: true| … end end … old_setting new_setting lib/site_settings/deprecated_settings.rb
  10. module SiteSettings::DeprecatedSettings fi ne_singleton_method old_setting do |warn: true| If warn

    Discourse.deprecate( `SiteSetting.#{old_setting}` has been deprecated. Please use `SiteSetting.#{new_setting}` instead. , drop_from: version ) end self.public_send(override ? new_setting : "_#{old_setting}") end … Discourse.deprecate Rails.logger.warn lib/site_settings/deprecated_settings.rb
  11. module SiteSettings::DeprecatedSettings fi ne_singleton_method old_setting do |warn: true| If warn

    # end self.public_send(override ? new_setting : "_#{old_setting}") end … public_send String Symbol lib/site_settings/deprecated_settings.rb