Sha256: 9dfd2b931b15336fb9a0b0422448fec05355f3641db6dfe4d7acb96225e1db02

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

require_relative "method_repository/version"

module MethodRepository
  def self.included(base)
    base.instance_variable_set(:@targets, {})
    base.extend(ModuleMethods)
  end

  module ModuleMethods
    def extended(base)
      klass_name = base.class.to_s == 'Class' ? base.to_s : base.class.to_s

      if (methods = @targets[klass_name])
        base.singleton_class.class_eval do
          methods.each do |method|
            define_method method[:name], method[:block]
          end
        end
      end
    end

    def included(base)
      if (methods = @targets[base.to_s])
        base.class_eval do
          methods.each do |method|
            define_method method[:name], method[:block]
          end
        end
      end
    end

    def insert(name, klasses = {}, &block)
      if !klasses[:in]
        raise ArgumentError.new("`:in' parameter is required")
      end

      klasses[:in].each do |klass|
        @targets[klass] ||= []
        @targets[klass] << {
          name:  name,
          block: block,
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
method_repository-0.0.4 lib/method_repository.rb