summaryrefslogtreecommitdiff
path: root/lib/prependable.rb
blob: ff0630d9da38677efcf20e575bf1a0068e38200c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# This module is based on: https://gist.github.com/bcardarella/5735987

module Prependable
  include ActiveSupport::Concern

  def self.extended(base) #:nodoc:
    base.instance_variable_set(:@_dependencies, [])
  end

  def prepend_features(base)
    if base.instance_variable_defined?(:@_dependencies)
      base.instance_variable_get(:@_dependencies) << self
      return false
    else
      return false if base < self
      super
      base.singleton_class.send(:prepend, const_get('ClassMethods'))  if const_defined?(:ClassMethods)
      @_dependencies.each { |dep| base.send(:include, dep) }
      base.class_eval(&@_included_block) if instance_variable_defined?(:@_included_block)
    end
  end

  alias_method :prepended, :included
end