How to use included method of ClassMethods Package

Best Howitzer_ruby code snippet using ClassMethods.included

concern.rb

Source:concern.rb Github

copy

Full Screen

2module ActiveSupport3 # A typical module looks like this:4 #5 # module M6 # def self.included(base)7 # base.extend ClassMethods8 # base.class_eval do9 # scope :disabled, -> { where(disabled: true) }10 # end11 # end12 #13 # module ClassMethods14 # ...15 # end16 # end17 #18 # By using <tt>ActiveSupport::Concern</tt> the above module could instead be19 # written as:20 #21 # require "active_support/concern"22 #23 # module M24 # extend ActiveSupport::Concern25 #26 # included do27 # scope :disabled, -> { where(disabled: true) }28 # end29 #30 # class_methods do31 # ...32 # end33 # end34 #35 # Moreover, it gracefully handles module dependencies. Given a +Foo+ module36 # and a +Bar+ module which depends on the former, we would typically write the37 # following:38 #39 # module Foo40 # def self.included(base)41 # base.class_eval do42 # def self.method_injected_by_foo43 # ...44 # end45 # end46 # end47 # end48 #49 # module Bar50 # def self.included(base)51 # base.method_injected_by_foo52 # end53 # end54 #55 # class Host56 # include Foo # We need to include this dependency for Bar57 # include Bar # Bar is the module that Host really needs58 # end59 #60 # But why should +Host+ care about +Bar+'s dependencies, namely +Foo+? We61 # could try to hide these from +Host+ directly including +Foo+ in +Bar+:62 #63 # module Bar64 # include Foo65 # def self.included(base)66 # base.method_injected_by_foo67 # end68 # end69 #70 # class Host71 # include Bar72 # end73 #74 # Unfortunately this won't work, since when +Foo+ is included, its <tt>base</tt>75 # is the +Bar+ module, not the +Host+ class. With <tt>ActiveSupport::Concern</tt>,76 # module dependencies are properly resolved:77 #78 # require "active_support/concern"79 #80 # module Foo81 # extend ActiveSupport::Concern82 # included do83 # def self.method_injected_by_foo84 # ...85 # end86 # end87 # end88 #89 # module Bar90 # extend ActiveSupport::Concern91 # include Foo92 #93 # included do94 # self.method_injected_by_foo95 # end96 # end97 #98 # class Host99 # include Bar # It works, now Bar takes care of its dependencies100 # end101 #102 # === Prepending concerns103 #104 # Just like <tt>include</tt>, concerns also support <tt>prepend</tt> with a corresponding105 # <tt>prepended do</tt> callback. <tt>module ClassMethods</tt> or <tt>class_methods do</tt> are106 # prepended as well.107 #108 # <tt>prepend</tt> is also used for any dependencies.109 module Concern110 class MultipleIncludedBlocks < StandardError #:nodoc:111 def initialize112 super "Cannot define multiple 'included' blocks for a Concern"113 end114 end115 class MultiplePrependBlocks < StandardError #:nodoc:116 def initialize117 super "Cannot define multiple 'prepended' blocks for a Concern"118 end119 end120 def self.extended(base) #:nodoc:121 base.instance_variable_set(:@_dependencies, [])122 end123 def append_features(base) #:nodoc:124 if base.instance_variable_defined?(:@_dependencies)125 base.instance_variable_get(:@_dependencies) << self126 false127 else128 return false if base < self129 @_dependencies.each { |dep| base.include(dep) }130 super131 base.extend const_get(:ClassMethods) if const_defined?(:ClassMethods)132 base.class_eval(&@_included_block) if instance_variable_defined?(:@_included_block)133 end134 end135 def prepend_features(base) #:nodoc:136 if base.instance_variable_defined?(:@_dependencies)137 base.instance_variable_get(:@_dependencies).unshift self138 false139 else140 return false if base < self141 @_dependencies.each { |dep| base.prepend(dep) }142 super143 base.singleton_class.prepend const_get(:ClassMethods) if const_defined?(:ClassMethods)144 base.class_eval(&@_prepended_block) if instance_variable_defined?(:@_prepended_block)145 end146 end147 # Evaluate given block in context of base class,148 # so that you can write class macros here.149 # When you define more than one +included+ block, it raises an exception.150 def included(base = nil, &block)151 if base.nil?152 if instance_variable_defined?(:@_included_block)153 if @_included_block.source_location != block.source_location154 raise MultipleIncludedBlocks155 end156 else157 @_included_block = block158 end159 else160 super161 end162 end163 # Evaluate given block in context of base class,164 # so that you can write class macros here.165 # When you define more than one +prepended+ block, it raises an exception.166 def prepended(base = nil, &block)167 if base.nil?168 if instance_variable_defined?(:@_prepended_block)169 if @_prepended_block.source_location != block.source_location170 raise MultiplePrependBlocks171 end...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful