ãæ²å ±ãActiveSupport::Concern ã® ClassMethod ã¯ã¢ã¸ã¥ã¼ã«ã¡ã½ããã«ãªã件
çããããã«ã¡ã¯ï¼ã太é¼å¼ãããµã¼ã¸ï¼é¢ç½ããï¼ãã¨æã£ãããã¿ã¤å¤å¼ãããµã¼ã¸ãã§ãããMUGENUP ã® osada ã§ããååã£ã¦é£ããã§ãããã
ã¨ããã§ãActiveSupport::Concern
ã® ClassMethod
ã§å®ç¾©ããã¯ã©ã¹å¤æ°ããã©ãã«ãããããåç¥ã§ããï¼
# app/models/concern/item_module.rb module ItemModule extend ActiveSupport::Concern module ClassMethods def my_module_method @@concern_class_variable ||= "concern_class_variable" end end end
# app/models/item.rb class Item < ActiveRecord::Base include ItemModule end
# spec/models/item_spec.rb describe Item do it "ClassMethods ã§å®ç¾©ããã¨åç §ã§ãã" do expect(Item.my_module_method).to eq "concern_class_variable" end end
ããã¾ã§ã¯OKã ã¨æãã¾ãã ã§ã¯ããããèªèº«ã®ã¯ã©ã¹å¤æ°ã¨ãã¦åç §ã§ããªããã¨ããåç¥ã§ããããï¼
it "ClassMethods ã§åãè¾¼ãã ã¯ã©ã¹ã¡ã½ãããå®è¡ãã¦ãèªèº«ã®ã¯ã©ã¹å¤æ°ã¨ãã¦ã¯å®ç¾©ãããªã" do expect{ Item.my_module_method }.not_to change{Item.class_variable_defined?(:@@concern_class_variable)}.from(false) end
åç §ã¯ã§ããã®ã«ãå®ç¾©ããã¦ããªããããã¯ä¸ä½ã©ãã«ããã®ï¼ã¨ããã®ãä»åã®ã話ã§ãã
ã¯ã©ã¹å¤æ°ã¯ã©ãã«å®ç¾©ããã¦ããã®ï¼
ruby ã®å ´åãã¯ã©ã¹ã¡ã½ããã¯ã¯ã©ã¹ã«ç¹ç°ã¡ã½ããã追å ãããã¨ããæèã§å®ç¾©ããã¾ãã
Ruby ã«ãããã¯ã©ã¹ã¡ã½ããã¨ã¯ã¯ã©ã¹ã®ç¹ç°ã¡ã½ããã®ãã¨ã§ãã ãããã£ã¦ãä½ããã®æ¹æ³ã§ã¯ã©ã¹ãªãã¸ã§ã¯ãã«ã¡ã½ãããå®ç¾©ããã°ãã ããã¯ã©ã¹ã¡ã½ããã¨ãªãã¾ãã http://docs.ruby-lang.org/ja/2.0.0/doc/spec=2fdef.html#class_method
ãã£ã¦ãã¯ã©ã¹ã¡ã½ããå ã§å®ç¾©ãããã¯ã©ã¹å¤æ°ã¯ãèªèº«ã®ã¯ã©ã¹å¤æ°ã¨ãªãã¾ãã
class Item < ActiveRecord::Base include ItemModule class << self def my_class_methods @@my_class_variable = "my_class_variable" end end end
it "ã¯ã©ã¹ã¡ã½ãããå®è¡ããã¨ã¯ã©ã¹å¤æ°ãå®ç¾©ããã" do expect{ Item.my_class_methods }.to change{Item.class_variable_defined?(:@@my_class_variable)}.from(false).to(true) end
ããã¯å ¨ãæå¾ éãã®åãã§ããå ã»ã©ã¨éãã®ã¯ãèªèº«ã®ã¯ã©ã¹ã¡ã½ããã¨ãã¦å®ç¾©ãã¦ããã¨ããç¹ã§ããã ã§ã¯ãclasså®ç¾©ãªãOKã§ãmodule ã ãããã¡ãªãã§ããããï¼
module ã® included ã¨ãã¦å®ç¾©ãã¦ã¿ã
ActiveSupport::Concern
ã§ã¯ included
ã¨ããã¡ã½ããã§ãã¯ã©ã¹å®ç¾©ã®ä¸ã§ã³ã¼ããè©ä¾¡ãããã¨ãã§ãã¾ãã
module ItemModule extend ActiveSupport::Concern included do class << self def my_singleton_module_method @@singleton_include_module_method = "singleton_include_module_method" end end end end
ãã®çµæã¯ã¯ã©ã¹å¤æ°ã¯ã¯ã©ã¹ã«ç´ä»ãã¾ãããæå¾ éãã§ãã
it "included 㧠æ¡å¼µããç¹ç°ã¯ã©ã¹ã®ã¯ã©ã¹ã¡ã½ããã¯ã¯ã©ã¹å¤æ°ãå®ç¾©ããã" do expect{ Item.my_singleton_module_method }.to change{Item.class_variable_defined?(:@@singleton_include_module_method)}.from(false).to(true) end
ã©ããããmodule ã ããã§ã¯ãªãããã§ãããéãã¯ãincluded
ã¨ClassMethod
ãªã®ã§ãæå¾
éãã«åããªãã®ã¯ClassMethod
ã®ããã®ããã§ãã
ClassMethod
ã¨included
ã®éãã¯ã©ãã«ããã®ãï¼
ClassMethod
ã¯ã©ã®ããã«åãã®ã§ããããï¼
base.extend const_get("ClassMethods") if const_defined?("ClassMethods")
ClassMethod
ã¨ããå®ç¾©ããã£ãããbase
ã«å¯¾ãã¦extend
ããã¨ããå®è£
ã«ãªã£ã¦ãã¾ãã
ä¸æ¹includes
ã¯ã
base.class_eval(&@_included_block) if instance_variable_defined?("@_included_block")
def included(base = nil, &block) if base.nil? @_included_block = block else super end end
base ã¯ã©ã¹ã®class_eval
ãå¼ã³åºãã¦ãããããèªèº«ã®ã³ã³ããã¹ãã§è©ä¾¡ããã¾ãã
éãã¯ClassMethod
ã¯ã¢ã¸ã¥ã¼ã«ã®extend
ã§ãããincluded
ã¯ã¯ã©ã¹å®ç¾©å
ã§ã®è©ä¾¡ã¨ããã¨ããã§ãã
å¼æ°ã§æå®ããã¢ã¸ã¥ã¼ã«ã®ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã self ã®ç¹ç° ã¡ã½ããã¨ãã¦è¿½å ãã¾ãã instance method Object#extend
extend ãããã¯ã©ã¹å¤æ°ã¯ã¢ã¸ã¥ã¼ã«å¤æ°ã¨ãªã
extend
ã¯ã¢ã¸ã¥ã¼ã«ã®ã¡ã½ããããç¹ç°ã¯ã©ã¹ã®ã¡ã½ããã¨ãã¦è¿½å ããã ãã§ãããã£ã¦ãextend
ã§è¿½å ãããã¯ã©ã¹ã¡ã½ããå
ã§å®ç¾©ãããã¯ã©ã¹å¤æ°ã¯ãã¢ã¸ã¥ã¼ã«å¤æ°ã«ãªã£ã¦ãã¾ãã®ã§ãï¼ï¼ï¼
it "ClassMethods ã§å®ç¾©ããã¯ã©ã¹ã¡ã½ããã¯ãã¢ã¸ã¥ã¼ã«å¤æ°" do expect{ Item.my_module_method }.to change{ItemModule::ClassMethods.class_variable_defined?(:@@concern_class_variable)}.from(false).to(true) end
ããã¦ããã«æããããã¨ã«ãã¢ã¸ã¥ã¼ã«å¤æ°ã¯include
ããå
¨ã¦ã§å
±æããã¾ããããã¥ã¢ã«ã«ãã£ããæ¸ããã¦ãã¾ãã
ã¢ã¸ã¥ã¼ã«ã§å®ç¾©ãããã¯ã©ã¹å¤æ°(ã¢ã¸ã¥ã¼ã«å¤æ°)ã¯ããã®ã¢ã¸ã¥ã¼ã«ã㤠ã³ã¯ã«ã¼ãããã¯ã©ã¹éã§ãå ±æããã¾ãã http://docs.ruby-lang.org/ja/2.0.0/doc/spec=2fvariables.html#class
module Foo @@foo = 1 end class Bar include Foo p @@foo += 1 # => 2 end class Baz include Foo p @@foo += 1 # => 3 end
ClassMethod ã¯ã¢ã¸ã¥ã¼ã«ã¡ã½ããã
ååã£ã¦é£ããã§ããããClassMethod
ã ã¨æã£ã¦ããã(ããã¦å®éã«ã¯ã©ã¹ã¡ã½ãããªã®ã§ãã)ããã®ä¸ã§å®ç¾©ããã¯ã©ã¹å¤æ°ã¯ãã¢ã¸ã¥ã¼ã«å¤æ°ã«ãªã£ã¦ãã¾ãã®ã§ãã
ããããã¯ã§ããã ã ClassMethod
ã¯ä½¿ããªãããã«ããããã¨å¿ã«æ±ºããæ¥ã§ããã
[注æ] ã¢ã¸ã¥ã¼ã«å¤æ°ãã¯ã©ã¹å¤æ°ãã¯ã©ã¹ã¡ã½ããã®å½¹å²ã«ã¤ãã¦
ã¢ã¸ã¥ã¼ã«å¤æ°ãå®ç¾©ããã¹ãã§ã¯ãªããã¨ããã®ã¯ããè¨ããã¦ãããã¨ã ã¨æãã¾ããããããã¢ã¸ã¥ã¼ã«ã¯æ°å¦çãªæå³ã§é¢æ°ã§ããã¹ãã ã¨ããã®ãç§ã®æè«ã§ããå¯ä½ç¨ããªãã¨è¨ãæãã¦ãè¯ãã§ãã
ãªã®ã§ãã¢ã¸ã¥ã¼ã«å ã§ã¯ã©ã¹å¤æ°ã使ãã¹ãã§ã¯ãªãã¨ãããææã¯æãã ã¨æãã¾ããã¯ã©ã¹å¤æ°ã使ããªãæ¹ãè¯ãã¨ããã®ãç´å¾ã§ãã¾ãã
æ¬è¨äºã¯ãã¯ã©ã¹å¤æ°ãã¯ã©ã¹ã¡ã½ããã®ä½¿ç¨ãæ¨å¥¨ãããã®ã§ã¯ããã¾ãããããã¾ã§ãç¥ã£ã¦ããã¨ç½ ãåé¿ã§ãããããã¨ããè¨äºã§ãã®ã§ããäºæ¿ã®ã»ã©ãããããé¡ãè´ãã¾ãã
åè
» ActiveSupport::Concern ã§ãããã¼ãªã¢ã¸ã¥ã¼ã«ã©ã¤ããéã TECHSCORE BLOG
宣ä¼
MUGENUP ã§ã¯ãrails ã使ãããã¨ã³ã¸ãã¢ãåéä¸ã§ãã ç¡éæµéçºããä¸ç·ãã¾ãããï¼
大きな裁量で自社サービス開発!Rubyエンジニアをウォンテッド! - 株式会社MUGENUPの求人 - Wantedly