{"id":2177,"date":"2013-02-28T16:57:13","date_gmt":"2013-02-28T07:57:13","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=2177"},"modified":"2018-11-14T16:33:55","modified_gmt":"2018-11-14T07:33:55","slug":"activesupport%e3%82%92%e8%aa%ad%e3%82%80-modulealias_method_chain","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2013\/02\/28\/activesupport%e3%82%92%e8%aa%ad%e3%82%80-modulealias_method_chain\/","title":{"rendered":"ActiveSupport\u3092\u8aad\u3080: Module#alias_method_chain"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>\u300c<a title=\"Rails: alias_method_chain: \u65e2\u5b58\u306e\u51e6\u7406\u3092\u4fee\u6b63\u3059\u308b\u5e38\u5957\u624b\u6bb5\" href=\"http:\/\/www.techscore.com\/blog\/2013\/02\/27\/rails-alias_method_chain-%e6%97%a2%e5%ad%98%e3%81%ae%e5%87%a6%e7%90%86%e3%82%92%e4%bf%ae%e6%ad%a3%e3%81%99%e3%82%8b%e5%b8%b8%e5%a5%97%e6%89%8b%e6%ae%b5\/\">Rails: alias_method_chain: \u65e2\u5b58\u306e\u51e6\u7406\u3092\u4fee\u6b63\u3059\u308b\u5e38\u5957\u624b\u6bb5<\/a>\u300d\u3067\u306f alias_method_chain \u3092\u3054\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002Ruby2.0 \u306e Module.prepend \u306b\u7f6e\u304d\u63db\u3048\u3089\u308c\u308b\u904b\u547d\u304b\u3082\u3057\u308c\u306a\u3044\u3082\u306e\u306e\u3001\u4e16\u306e\u4e2d\u306e\u591a\u304f\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u898f\u5f8b\u3068\u4fbf\u5229\u3055\u3092\u4e0e\u3048\u7d9a\u3051\u3066\u304d\u305f\u305d\u306e\u5fc3\u306f\u6c7a\u3057\u3066\u5fd8\u308c\u306a\u3044\u3002\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306f ActiveSupport \u306e Module#alias_method_chain \u306e\u30b3\u30fc\u30c9\uff08\u73fe\u6642\u70b9\u306e\u6700\u65b0 v3.2.12\uff09\u3092\u898b\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>Module#alias_method_chain<\/h2>\n<p>Module#alias_method_chain \u306f <a href=\"https:\/\/github.com\/rails\/rails\/blob\/v3.2.12\/activesupport\/lib\/active_support\/core_ext\/module\/aliasing.rb\">core_ext\/module\/aliasing.rb<\/a> \u3067\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">  def alias_method_chain(target, feature)\r\n    # Strip out punctuation on predicates or bang methods since\r\n    # e.g. target?_without_feature is not a valid method name.\r\n    aliased_target, punctuation = target.to_s.sub(\/([?!=])$\/, ''), $1\r\n    yield(aliased_target, punctuation) if block_given?\r\n\r\n    with_method = \"#{aliased_target}_with_#{feature}#{punctuation}\"\r\n    without_method = \"#{aliased_target}_without_#{feature}#{punctuation}\"\r\n\r\n    alias_method without_method, target\r\n    alias_method target, with_method\r\n\r\n    case\r\n    when public_method_defined?(without_method)\r\n      public target\r\n    when protected_method_defined?(without_method)\r\n      protected target\r\n    when private_method_defined?(without_method)\r\n      private target\r\n    end\r\n  end<\/pre>\n<p>Ruby \u306b\u304a\u3051\u308b\u30e1\u30bd\u30c3\u30c9\u540d\u306f\u300cgsub!\u300d\u300cnil?\u300d\u300cname=\u300d\u306e\u3088\u3046\u306b\u6700\u5f8c\u304c\u300c!\u300d\u300c?\u300d\u300c=\u300d\u3068\u306a\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u4ee5\u4e0b\u306e\u90e8\u5206\u3067\u306f\u30e1\u30bd\u30c3\u30c9\u540d\u304b\u3089\u300c!\u300d\u300c?\u300d\u300c=\u300d\u3092\u5206\u96e2\u3057\u3066\u3044\u307e\u3059\u3002\u4f8b\u3048\u3070\u300calias_method_chain :name=, ...\u300d\u3068\u547c\u3073\u51fa\u3057\u305f\u5834\u5408\u306f\u3001aliased_target \u306b\u306f\u300cname\u300d\u3001punctuation \u306b\u306f\u300c=\u300d\u304c\u5165\u308a\u307e\u3059\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001\u30d6\u30ed\u30c3\u30af\u304c\u6307\u5b9a\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306f\u3001aliased_target \u3068 punctuation \u3092\u5f15\u6570\u306b\u30d6\u30ed\u30c3\u30af\u3092\u547c\u3073\u51fa\u3057\u3066\u3044\u307e\u3059\uff08\u3053\u306e\u52d5\u4f5c\u306f\u30b3\u30fc\u30c9\u3092\u8aad\u3093\u3067\u521d\u3081\u3066\u77e5\u308a\u307e\u3057\u305f\uff09\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">    # Strip out punctuation on predicates or bang methods since\r\n    # e.g. target?_without_feature is not a valid method name.\r\n    aliased_target, punctuation = target.to_s.sub(\/([?!=])$\/, ''), $1\r\n    yield(aliased_target, punctuation) if block_given?<\/pre>\n<p>\u6b21\u306e\u90e8\u5206\u3067\u306f\u300cxxx_with_xxx\u300d\u3068\u300cxxx_without_xxx\u300d\u3068\u3044\u3046\u6587\u5b57\u5217\u3092\u69cb\u7bc9\u3057\u3001alias_method \u3067\u30a8\u30a4\u30ea\u30a2\u30b9\u3092\u8a2d\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u306e\u90e8\u5206\u304c alias_method_chain \u304c\u884c\u3046\u30e1\u30a4\u30f3\u306e\u4ed5\u4e8b\u3067\u3059\u306d\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">    with_method = \"#{aliased_target}_with_#{feature}#{punctuation}\"\r\n    without_method = \"#{aliased_target}_without_#{feature}#{punctuation}\"\r\n\r\n    alias_method without_method, target\r\n    alias_method target, with_method<\/pre>\n<p>\u6700\u5f8c\u306b\u3001\u4ee5\u4e0b\u306e\u90e8\u5206\u3067\u5143\u306e\u30e1\u30bd\u30c3\u30c9\u3068\u540c\u3058\u53ef\u8996\u6027\u306b\u306a\u308b\u3088\u3046\u306b\u8a2d\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">    case\r\n    when public_method_defined?(without_method)\r\n      public target\r\n    when protected_method_defined?(without_method)\r\n      protected target\r\n    when private_method_defined?(without_method)\r\n      private target\r\n    end<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<p>\u307b\u3068\u3093\u3069\u4e88\u5099\u77e5\u8b58\u3068\u3057\u3066\u77e5\u3063\u3066\u3044\u305f\u3053\u3068\u306e\u518d\u78ba\u8a8d\u306b\u306a\u308a\u307e\u3057\u305f\u304c\u3001alias_method_chain \u306b\u30d6\u30ed\u30c3\u30af\u3092\u6e21\u305b\u308b\u3053\u3068\u306f\u521d\u3081\u3066\u77e5\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u4ed6\u306b\u3082 <a href=\"https:\/\/github.com\/rails\/rails\/blob\/v3.2.12\/activesupport\/lib\/active_support\/core_ext\/module\/aliasing.rb\">core_ext\/module\/aliasing.rb<\/a> \u306b\u306f alias_attribute \u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u3082\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\n<p>\u3053\u3061\u3089\u306f\u5c5e\u6027\u306e\u30a8\u30a4\u30ea\u30a2\u30b9\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306e\u3082\u306e\u3067\u3001\u300calias_attribute :title, :name\u300d\u306e\u3088\u3046\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<p>\u30b3\u30fc\u30c9\u3092\u8aad\u3081\u3070\u4f55\u304b\u3057\u3089\u65b0\u3057\u3044\u767a\u898b\u304c\u3042\u308a\u307e\u3059\u306d\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>\u300cRails: alias_method_chain: \u65e2\u5b58\u306e\u51e6\u7406\u3092\u4fee\u6b63\u3059\u308b\u5e38\u5957\u624b\u6bb5\u300d\u3067\u306f alias_method_chain \u3092\u3054\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2013\/02\/28\/activesupport%e3%82%92%e8%aa%ad%e3%82%80-modulealias_method_chain\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[89,18,84],"tags":[68,24],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/2177"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=2177"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/2177\/revisions"}],"predecessor-version":[{"id":17950,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/2177\/revisions\/17950"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=2177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=2177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=2177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}