Sha256: 5bb4020de171fa90a21e6169019f8a7f2d363d1740f39da11cf68b7041c5c754

Contents?: true

Size: 1.63 KB

Versions: 39

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

require 'active_support/concern'
require 'eac_ruby_utils/simple_cache'
require 'eac_ruby_utils/patches/object/if_present'

module EacRubyUtils
  class CommonConcern
    CLASS_METHODS_MODULE_NAME = 'ClassMethods'
    INSTANCE_METHODS_MODULE_NAME = 'InstanceMethods'

    attr_reader :after_callback

    def initialize(&after_callback)
      @after_callback = after_callback
    end

    def setup(a_module)
      Setup.new(self, a_module).run
    end

    class Setup
      include ::EacRubyUtils::SimpleCache
      attr_reader :a_module, :common_concern

      def initialize(common_concern, a_module)
        @common_concern = common_concern
        @a_module = a_module
      end

      def run
        setup = self
        a_module.extend(::ActiveSupport::Concern)
        a_module.included do
          %w[class_methods instance_methods after_callback].each do |suffix|
            setup.send("setup_#{suffix}", self)
          end
        end
      end

      def setup_class_methods(base)
        class_methods_module.if_present { |v| base.extend v }
      end

      def setup_instance_methods(base)
        instance_methods_module.if_present { |v| base.include v }
      end

      def setup_after_callback(base)
        common_concern.after_callback.if_present do |v|
          base.instance_eval(&v)
        end
      end

      def class_methods_module_uncached
        a_module.const_get(CLASS_METHODS_MODULE_NAME)
      rescue NameError
        nil
      end

      def instance_methods_module_uncached
        a_module.const_get(INSTANCE_METHODS_MODULE_NAME)
      rescue NameError
        nil
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 3 rubygems

Version Path
eac_ruby_utils-0.55.0 lib/eac_ruby_utils/common_concern.rb
avm-tools-0.80.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
avm-tools-0.79.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
avm-tools-0.78.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
ehbrs-tools-0.16.2 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
eac_ruby_utils-0.54.0 lib/eac_ruby_utils/common_concern.rb
eac_ruby_utils-0.53.0 lib/eac_ruby_utils/common_concern.rb
avm-tools-0.77.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
eac_ruby_utils-0.52.0 lib/eac_ruby_utils/common_concern.rb
ehbrs-tools-0.16.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
eac_ruby_utils-0.51.1 lib/eac_ruby_utils/common_concern.rb
avm-tools-0.76.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
eac_ruby_utils-0.51.0 lib/eac_ruby_utils/common_concern.rb
eac_ruby_utils-0.50.0 lib/eac_ruby_utils/common_concern.rb
ehbrs-tools-0.16.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
avm-tools-0.76.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
eac_ruby_utils-0.49.1 lib/eac_ruby_utils/common_concern.rb
avm-tools-0.75.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
avm-tools-0.75.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
avm-tools-0.74.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb