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

ActiveSupport::Concern
で学ぶRuby

rince
November 22, 2017

 ActiveSupport::Concern
で学ぶRuby

11/22のEbisu.rbでLTをした際の資料です。
ActiveSupport::Concernがどう実現されているかを実際にコードを読んで学び、Rubyへの理解を深めることを目的とした内容です。

rince

November 22, 2017
Tweet

More Decks by rince

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ w ླ໦࿨ਅʢ!LB[VNBYʣ w ܦྺ w ɹΧΧΫίϜೖࣾ w ʙ৯΂ϩά w

    ʙΩφϦϊ w झຯ w ཱྀ !"#$%&'()*+,-./0123456789:;<=>?@
  2. 0CKFDUFYUFOE w ࢦఆͨ͠ϞδϡʔϧͷΠϯελϯεϝιουΛTFMGͷಛ ҟϝιουͱͯ͠௥Ճ͢Δ module M def my_method 'Ebisu.rb' end

    end class C1 include M end class C2 extend M end C1.new.my_method #=> "Ebisu.rb" C2.my_method #=> "Ebisu.rb"
  3. "DUJWF4VQQPSU$PODFSOͷྫᶃ module M def self.included(base) base.extend ClassMethods base.class_eval do scope

    :disabled, ->{ where(disabled: true) } end end module ClassMethods ... end end module M extend ActiveSupport::Concern included do scope :disabled, ->{ where(disabled: true) } end class_methods do ... end end $PODFSOͳ͠ $PODFSO͋Γ w Ϋϥεϝιουͷ௥Ճ΍JODMVEFͨ͠ଆͷΫϥεϝιουͷݺͼग़͠Λ؆ ୯ʹॻ͚Δ
  4. "DUJWF4VQQPSU$PODFSOͷྫᶄ module M1 def self.included(base) base.extend(ClassMethods) end module ClassMethods def

    m1_class_method 'M1!' end end end module M2 include M1 def self.included(base) base.extend(ClassMethods) end module ClassMethods def m2_class_method m1_class_method + ' M2!' end end end class C include M2 end C.m2_class_method #=> NameError: undefined local variable or method `m1_class_method' for C:Class $PODFSOͳ͠ w JODMVEF͢ΔϞδϡʔϧͷґଘؔ܎Λ؅ཧͯ͘͠ΕΔ module M1 extend ActiveSupport::Concern class_methods do def m1_class_method 'M1!' end end end module M2 extend ActiveSupport::Concern include M1 class_methods do def m2_class_method m1_class_method + ' M2!' end end end class C include M2 end C.m2_class_method #=> "M1! M2!" $PODFSO͋Γ
  5. 1 module ActiveSupport 2 module Concern 3 class MultipleIncludedBlocks <

    StandardError #:nodoc: 4 def initialize 5 super "Cannot define multiple 'included' blocks for a Concern" 6 end 7 end 8 9 def self.extended(base) #:nodoc: 10 base.instance_variable_set(:@_dependencies, []) 11 end 12 13 def append_features(base) 14 if base.instance_variable_defined?(:@_dependencies) 15 base.instance_variable_get(:@_dependencies) << self 16 return false 17 else 18 return false if base < self 19 @_dependencies.each { |dep| base.include(dep) } 20 super 21 base.extend const_get(:ClassMethods) if const_defined?(:ClassMethods) 22 base.class_eval(&@_included_block) if instance_variable_defined?(:@_included_block) 23 end 24 end 25 26 def included(base = nil, &block) 27 if base.nil? 28 raise MultipleIncludedBlocks if instance_variable_defined?(:@_included_block) 29 30 @_included_block = block 31 else 32 super 33 end 34 end 35 36 def class_methods(&class_methods_module_definition) 37 mod = const_defined?(:ClassMethods, false) ? 38 const_get(:ClassMethods) : 39 const_set(:ClassMethods, Module.new) 40 41 mod.module_eval(&class_methods_module_definition) 42 end 43 end 44 end
  6. 1 module ActiveSupport 2 module Concern 3 class MultipleIncludedBlocks <

    StandardError #:nodoc: 4 def initialize 5 super "Cannot define multiple 'included' blocks for a Concern" 6 end 7 end 8 9 def self.extended(base) #:nodoc: 10 base.instance_variable_set(:@_dependencies, []) 11 end 12 13 def append_features(base) 14 if base.instance_variable_defined?(:@_dependencies) 15 base.instance_variable_get(:@_dependencies) << self 16 return false 17 else 18 return false if base < self 19 @_dependencies.each { |dep| base.include(dep) } 20 super 21 base.extend const_get(:ClassMethods) if const_defined?(:ClassMethods) 22 base.class_eval(&@_included_block) if instance_variable_defined?(:@_included_block) 23 end 24 end 25 26 def included(base = nil, &block) 27 if base.nil? 28 raise MultipleIncludedBlocks if instance_variable_defined?(:@_included_block) 29 30 @_included_block = block 31 else 32 super 33 end 34 end 35 36 def class_methods(&class_methods_module_definition) 37 mod = const_defined?(:ClassMethods, false) ? 38 const_get(:ClassMethods) : 39 const_set(:ClassMethods, Module.new) 40 41 mod.module_eval(&class_methods_module_definition) 42 end 43 end 44 end
  7. 1 module ActiveSupport 2 module Concern 3 class MultipleIncludedBlocks <

    StandardError #:nodoc: 4 def initialize 5 super "Cannot define multiple 'included' blocks for a Concern" 6 end 7 end 8 9 def self.extended(base) #:nodoc: 10 base.instance_variable_set(:@_dependencies, []) 11 end 12 13 def append_features(base) 14 if base.instance_variable_defined?(:@_dependencies) 15 base.instance_variable_get(:@_dependencies) << self 16 return false 17 else 18 return false if base < self 19 @_dependencies.each { |dep| base.include(dep) } 20 super 21 base.extend const_get(:ClassMethods) if const_defined?(:ClassMethods) 22 base.class_eval(&@_included_block) if instance_variable_defined?(:@_included_block) 23 end 24 end 25 26 def included(base = nil, &block) 27 if base.nil? 28 raise MultipleIncludedBlocks if instance_variable_defined?(:@_included_block) 29 30 @_included_block = block 31 else 32 super 33 end 34 end 35 36 def class_methods(&class_methods_module_definition) 37 mod = const_defined?(:ClassMethods, false) ? 38 const_get(:ClassMethods) : 39 const_set(:ClassMethods, Module.new) 40 41 mod.module_eval(&class_methods_module_definition) 42 end 43 end 44 end
  8. $PODFSOͷίʔυΛಡΉᶄ  !@EFQFOEFODJFT͕ఆٛ͞Ε͍ͯΕ͹ʢDPODFSOͰ͋Ε͹ʣɺ !@EFQFOEFODJFTʹࣗ෼ࣗ਎Λ௥Ճͯ͠GBMTFΛฦ͢JODMVEF͠ͳ͍  DPODFSOͰͳ͍৔߹ɺܧঝνΣʔϯʹࣗ෼ࣗ਎͕௥Ճ͞Ε͍ͯͳ͚Ε͹ɺґଘ ͨ͠DPODFSOΛ࠶ؼతʹJODMVEF͍͖ͯ͠ɺ࠷ޙʹTVQFSͰܧঝνΣʔϯʹࣗ ෼ࣗ਎Λ௥Ճ͢Δ  $MBTT.FUIPETϞδϡʔϧΛFYUFOE͢Δ

     JODMVEFEʹ౉ͨ͠ϒϩοΫΛJODMVEFͨ͠ଆͷΫϥεͷίϯςΩετͰ࣮ߦ͢Δ def append_features(base) if base.instance_variable_defined?(:@_dependencies) base.instance_variable_get(:@_dependencies) << self return false else return false if base < self @_dependencies.each { |dep| base.include(dep) } super base.extend const_get(:ClassMethods) if const_defined?(:ClassMethods) base.class_eval(&@_included_block) if instance_variable_defined?(:@_included_block) end end
  9. 1 module ActiveSupport 2 module Concern 3 class MultipleIncludedBlocks <

    StandardError #:nodoc: 4 def initialize 5 super "Cannot define multiple 'included' blocks for a Concern" 6 end 7 end 8 9 def self.extended(base) #:nodoc: 10 base.instance_variable_set(:@_dependencies, []) 11 end 12 13 def append_features(base) 14 if base.instance_variable_defined?(:@_dependencies) 15 base.instance_variable_get(:@_dependencies) << self 16 return false 17 else 18 return false if base < self 19 @_dependencies.each { |dep| base.include(dep) } 20 super 21 base.extend const_get(:ClassMethods) if const_defined?(:ClassMethods) 22 base.class_eval(&@_included_block) if instance_variable_defined?(:@_included_block) 23 end 24 end 25 26 def included(base = nil, &block) 27 if base.nil? 28 raise MultipleIncludedBlocks if instance_variable_defined?(:@_included_block) 29 30 @_included_block = block 31 else 32 super 33 end 34 end 35 36 def class_methods(&class_methods_module_definition) 37 mod = const_defined?(:ClassMethods, false) ? 38 const_get(:ClassMethods) : 39 const_set(:ClassMethods, Module.new) 40 41 mod.module_eval(&class_methods_module_definition) 42 end 43 end 44 end
  10. $PODFSOͷίʔυΛಡΉᶅ !@JODMVEFE@CMPDLʹJODMVEFEʹ౉͞ΕͨϒϩοΫͷ1SPDΦϒ δΣΫτΛ୅ೖ͢Δ def included(base = nil, &block) if base.nil?

    raise MultipleIncludedBlocks if instance_variable_defined?(:@_included_block) @_included_block = block else super end end def class_methods(&class_methods_module_definition) mod = const_defined?(:ClassMethods, false) ? const_get(:ClassMethods) : const_set(:ClassMethods, Module.new) mod.module_eval(&class_methods_module_definition) end $MBTT.FUIPETϞδϡʔϧʹDMBTT@NFUIPETʹ౉͞ΕͨϒϩοΫͷ ಺༰Λ௥Ճ͢Δ