Sha256: f9d57be2b0f51985b41d6bff7a67982187dbce099970de77a37c6155cfbe1af1

Contents?: true

Size: 1.65 KB

Versions: 44

Compression:

Stored size: 1.65 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_constructor

      def initialize(common_constructor, a_module)
        @common_constructor = common_constructor
        @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_constructor.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

44 entries across 44 versions & 3 rubygems

Version Path
eac_ruby_utils-0.45.1 lib/eac_ruby_utils/common_concern.rb
eac_ruby_utils-0.45.0 lib/eac_ruby_utils/common_concern.rb
ehbrs-tools-0.13.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
avm-tools-0.68.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
eac_ruby_utils-0.44.0 lib/eac_ruby_utils/common_concern.rb
avm-tools-0.67.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
avm-tools-0.66.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
avm-tools-0.65.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
avm-tools-0.64.2 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
avm-tools-0.64.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
avm-tools-0.64.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
avm-tools-0.63.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
avm-tools-0.62.4 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
avm-tools-0.62.3 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
avm-tools-0.62.2 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
avm-tools-0.62.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
avm-tools-0.62.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
avm-tools-0.61.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
avm-tools-0.60.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb
avm-tools-0.59.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb