Sha256: 820581e09aad8db6ae01c01a554e81f3f397d137104ac673991347cd8af057c8

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module UnrealReaper
  class Manager
    attr_reader :obj, :installed_packages

    def initialize(obj)
      @obj = obj
      @installed_packages = {}
    end

    def _install(name, top_cls, helper_cls, writer_cls, opts = {})
      options = opts.symbolize_keys
      installed_packages[name.to_sym] = {
        top_cls: top_cls,
        helper_cls: helper_cls,
        writer_cls: writer_cls
      }

      if top_cls
        top_cls.prepend(PackageObjExt)
        top_obj = top_cls.new(obj, options)
        define_singleton_method(name) { top_obj }
      end

      if helper_cls
        helper_cls.prepend(PackageObjExt)
        _instance_manager_cls.class_eval do
          define_method name do
            _packages_objs["#{name}_helper_obj"] ||= helper_cls.new(obj, options)
          end
        end
      end

      if writer_cls
        writer_cls.prepend(PackageObjExt)

        _instance_manager_cls.class_eval do
          define_method "#{name}=" do |val|
            (_packages_objs["#{name}_writer_obj"] ||= writer_cls.new(obj, options))._write(val)
          end
        end
      end
    end

    def _instance_manager_cls
      @_instance_manager_cls ||= Class.new { include InstanceManager }
    end
  end

  module InstanceManager
    attr_reader :obj, :_packages_objs

    def initialize(obj)
      @obj = obj
      @_packages_objs = {}
    end

    def _write(data)
      data.each do |package_name, val|
        send("#{package_name}=", val)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unreal_reaper-0.1.0 lib/unreal_reaper/manager.rb