Sha256: f34ede780ca9cd3bbca42a99d2f8f724049248960cd229b83cdad3aa53062465

Contents?: true

Size: 1.37 KB

Versions: 6

Compression:

Stored size: 1.37 KB

Contents

module Lux::Config::Plugin
  extend self

  @plugins = {}

  # load specific plugin
  # Lux.plugin :foo
  # Lux.plugin 'foo/bar'
  # Lux.plugin.folders
  def load arg
    arg = arg.to_s if arg.is_a?(Symbol)

    if arg.is_a?(String)
      arg = arg.include?('/') ? { folder: arg } : { name: arg }
    end

    opts           = arg.to_opts name: String, folder: String, namespace: Symbol
    opts.name    ||= opts.folder.split('/').last
    opts.name      = opts.name.to_s
    opts.folder  ||= Lux.fw_root.join('plugins', opts.name).to_s
    opts.namespace = [opts.namespace] unless opts.namespace.is_a?(Array)

    return @plugins[opts.name] if @plugins[opts.name]

    die(%{Plugin "#{opts.name}" not found in "#{opts.folder}"}) unless Dir.exist?(opts.folder)

    @plugins[opts.name] ||= opts

    base = Pathname.new(opts.folder).join(opts.name, '.rb')

    if base.exist?
      require base.to_s
    else
      Lux::Config.require_all(opts.folder)
    end

    @plugins[opts.name]
  end

  def get name
    @plugins[name.to_s] || die('Plugin "%s" not loaded' % name)
  end

  def loaded
     @plugins.values
  end

  def keys
    @plugins.keys
  end

  def plugins
    @plugins
  end

  # get all folders in a namespace
  def folders namespace=:main
    name = name.to_sym

    list = @plugins.values
    list.select { |it| it.namespace.include?(namespace) }
    list.map { |it| it.folder }
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lux-fw-0.5.37 ./lib/lux/config/lib/plugin.rb
lux-fw-0.5.36 ./lib/lux/config/lib/plugin.rb
lux-fw-0.5.35 ./lib/lux/config/lib/plugin.rb
lux-fw-0.5.34 ./lib/lux/config/lib/plugin.rb
lux-fw-0.5.33 ./lib/lux/config/lib/plugin.rb
lux-fw-0.5.32 ./lib/lux/config/lib/plugin.rb