Sha256: 59ba0b38845ebf7cbc4c8c6b2d5293e4cd6563b8b4710bb016773054b7a1c03c

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module Pcli
  module Depends
    def self.on(*names, **kw_names)
      Module.new do
        class_methods = Module.new do
          define_method :dependencies do
            names + kw_names.values
          end
        end

        define_singleton_method :included do |base|
          base.extend class_methods
        end

        names.each do |name|
          method_name = name.split('.').last

          define_method(method_name) { @_dependencies[name] }
        end

        kw_names.each do |method_name, name|
          define_method(method_name) { @_dependencies[name] }
        end

        define_method :initialize do |**args|
          @_dependencies = self.class.dependencies.map { |n| [n, nil] }.to_h

          args.each do |key, value|
            key = key.to_s
            raise "#{self.class.name} given invalid dependency \"#{key}\"" unless @_dependencies.key?(key)

            @_dependencies[key] = value
          end
          @_dependencies.each_key do |n|
            raise "#{self.class.name} missing dependency \"#{n}\"" unless args.key?(n.to_sym)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pcli-0.1.1 lib/pcli/depends.rb
pcli-0.1.0 lib/pcli/depends.rb