FirstLevelModule extend SecondLevelModule def first_method self.class.second_method p 'first' end end class BaseClass include FirstLevelModule end BaseClass.new.first_method # => NoMethodError JODMVEFͨ͠ϞδϡʔϧͷதͰ ͞ΒʹผͷϞδϡʔϧͰఆٛ͞Εͨ ΫϥεϝιουΛݺͼग़͢ͱ /P.FUIPE&SSPS
FirstLevelModule include SecondLevelModule def first_method self.class.second_method p 'first’ end end class BaseClass extend SecondLevelModule include FirstLevelModule end BaseClass.new.first_method # => “second” # “first” ͜ΕͳΒͪΌΜͱಈ͘
:disabled, -> { where(disabled: true) } end end module ClassMethods ... end end JODMVEF͞Εͨͱ͖ʹൃՐ Ҿ༻ɿ"DUJWF4VQQPSU$PODFSO IUUQTBQJSVCZPOSBJMTPSHDMBTTFT"DUJWF4VQQPSU$PODFSOIUNM
self.included(base) base.send :extend, SecondLevelModule end ... end class BaseClass include FirstLevelModule ... end BaseClass.new.first_method # => “second” # “first” ґଘϞδϡʔϧΛແཧΓ FYUFOE͢Δ
end end module FirstLevelModule extend ActiveSupport::Concern extend SecondLevelModule def first_method self.class.second_method p 'first' end end class BaseClass include FirstLevelModule end BaseClass.new.first_method # => “second” # “first” ґଘϞδϡʔϧΛ FYUFOE͠ͳͯ͘ ͪΌΜͱಈ͘
< StandardError def initialize super "Cannot define multiple 'included' blocks for a Concern" end end def self.extended(base) base.instance_variable_set(:@_dependencies, []) end def append_features(base) if base.instance_variable_defined?(:@_dependencies) base.instance_variable_get(:@_dependencies) << self 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 def included(base = nil, &block) if base.nil? if instance_variable_defined?(:@_included_block) if @_included_block.source_location != block.source_location raise MultipleIncludedBlocks end else @_included_block = block end 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 end end
instance_variable_defined?(:@_included_block) if @_included_block.source_location != block.source_location raise MultipleIncludedBlocks end else @_included_block = block end 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
end class Bar include Foo end # => "Bar include Foo" TFMGJODMVEFEͷܗͰɺ JODMVEF͞Εͨͱ͖ͷ ಈ࡞Λॻ͘ Ҿ༻ɿJOTUBODFNFUIPE.PEVMFJODMVEFE 3VCZ IUUQTSVSFNBDMFBSDPEFDPNNFUIPE.PEVMFJJODMVEFEIUNM
if @_included_block.source_location != block.source_location raise MultipleIncludedBlocks end else @_included_block = block end else super end end Ϩγʔό͕͋Δ ʹຊདྷͷ͍ํΛ͍ͯ͠Δ
if @_included_block.source_location != block.source_location raise MultipleIncludedBlocks end else @_included_block = block end else super end end Ϩγʔόͳ͠ͷJODMVEFE͕ ݸҎ্͋ͬͨΒΤϥʔ
if @_included_block.source_location != block.source_location raise MultipleIncludedBlocks end else @_included_block = block end else super end end !@JODMVEFE@CMPDL ʹϒϩοΫΛ֨ೲ
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 CBTFFYUFOEͰ $MBTT.FUIPETΛΫϥεϝιουͱͯ͠ੜ͠ CBTFDMBTT@FWBMͰ !@JODMVEFE@CMPDLΛΫϥεϚΫϩͱ࣮ͯ͠ߦͨ͠
base.instance_variable_defined?(:@_dependencies) base.instance_variable_get(:@_dependencies) << self 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
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