|
13 | 13 | # See the License for the specific language governing permissions and |
14 | 14 | # limitations under the License. |
15 | 15 |
|
16 | | -# ActiveSupport::Concern is needed to help abstract out the boilerplate for creating includable modules |
17 | | -# TODO: Consider embedding active_support/concern directly. The difference in license will have to be addressed. |
18 | | - |
19 | | -require "active_support/concern" |
| 16 | +# Light Concern implementation without ActiveSupport |
20 | 17 |
|
21 | 18 | module Pedant |
22 | | - Concern = ActiveSupport::Concern |
| 19 | + module Concern |
| 20 | + def self.extended(base) |
| 21 | + base.instance_variable_set(:@_dependencies, []) |
| 22 | + end |
| 23 | + |
| 24 | + def included(base = nil, &block) |
| 25 | + if base.nil? |
| 26 | + raise MultipleIncludedBlocks if instance_variable_defined?(:@_included_block) |
| 27 | + @_included_block = block |
| 28 | + else |
| 29 | + super |
| 30 | + |
| 31 | + if instance_variable_defined?(:@_included_block) |
| 32 | + base.class_eval(&@_included_block) |
| 33 | + end |
| 34 | + |
| 35 | + const_get(:ClassMethods).tap do |class_methods| |
| 36 | + base.extend(class_methods) if defined?(class_methods) |
| 37 | + end if const_defined?(:ClassMethods) |
| 38 | + |
| 39 | + @_dependencies.each { |dep| base.include(dep) } |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + def append_features(base) |
| 44 | + return false if base < self |
| 45 | + super |
| 46 | + (@_dependencies ||= []).each { |dep| base.include(dep) } |
| 47 | + end |
23 | 48 |
|
24 | | - # This is a trick passed on by Dan Deleo. This creates a module Pedant::Concern that |
25 | | - # is exactly ActiveSupport::Concern. We can then reference Pedant::Concern instead of |
26 | | - # ActiveSupport::Concern. If we want to embed ActiveSupport::Concern, we can fill out |
27 | | - # Pedant::Concern without having to do a massive refactor. |
| 49 | + def class_methods(&block) |
| 50 | + mod = const_defined?(:ClassMethods, false) ? const_get(:ClassMethods) : const_set(:ClassMethods, Module.new) |
| 51 | + mod.module_eval(&block) |
| 52 | + end |
| 53 | + end |
28 | 54 | end |
0 commit comments