Sha256: 815fb40e8a88b2d42b4be388b8d529eed1751d614420cf97d9b0a42b38d2799b

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

require "unreal_reaper/version"

require 'active_support'
require 'active_support/core_ext'

module UnrealReaper
  extend ActiveSupport::Autoload

  autoload :Installer
  autoload :Manager
  autoload :PackageObjExt

  class << self
    def included(cls)
      cls.class_eval do
        def self.ur_package(name, opts = {})
          UnrealReaper::Installer.new(self).install(name, opts)
        end
      end
    end

    def load(package, opts = { top_cls: nil, helper_cls: nil, writer_cls: nil })
      name, tm, hm, wm = format_package(package, opts)
      raise "Cannot load a empty package" unless tm || hm || wm
      packages[name.to_sym] = { top_cls: tm, helper_cls: hm, writer_cls: wm }
    end

    def packages
      @packages ||= {}
    end

    private

    def format_package(package, opts)
      opts = opts.symbolize_keys
      name = nil
      tm = opts[:top_cls]
      hm = opts[:helper_cls]
      wm = opts[:writer_cls]

      case package
      when String, Symbol
        name = package.to_sym
      when Module
        name = package.name.underscore
        tm ||= get_package_cls(package, :Top)
        hm ||= get_package_cls(package, :Helper)
        wm ||= get_package_cls(package, :Writer)

        if package.is_a?(Class)
          hm ||= package
          wm ||= package
        end
      else
        raise "The package isn't a module"
      end

      [name, tm, hm, wm]
    end

    def get_package_cls(package, name)
      return unless package.constants.include?(name)
      cls = package.const_get(name)
      raise "'#{name}' is not a class" unless cls.is_a?(Class)
      cls
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unreal_reaper-0.1.0 lib/unreal_reaper.rb