Sha256: b9284801aedd11507104b71c11f67377e6eaf507e138f3ea2fa6072150b58f3d

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module Liza
  class Error < StandardError; end
  class ConstNotFound < Error; end

  #

  module_function

  def log s
    puts s.bold
  end

  #

  def [] name
    const name
  end

  # Checks Object, each system, then Liza for Liza::Unit classes
  def const name
    name = name.to_s.camelize.to_sym

    k = const_check_object name
    return k if k

    k = const_check_systems name
    return k if k

    k = const_get name
    return k if k

    nil
  rescue NameError
    log "Liza const #{name} not found!"
    if Lizarb.ruby_supports_raise_cause?
      raise ConstNotFound, name, cause: nil
    else
      raise ConstNotFound, name, []
    end
  end

  # Checks each system, then Liza for Liza::Unit classes
  def const_missing name
    k = const_check_systems name
    return k if k

    super
  end

  def const_check_object name
    return if not Object.const_defined? name
    kk = Object.const_get name
    return kk if is_unit? kk

    nil
  end

  def const_check_systems name
    App.systems.frozen? or return nil

    for k in App.systems.values.reverse
      next if not k.const_defined? name.to_sym
      kk = k.const(name) if k.constants.include? name
      return kk if is_unit? kk
    end

    nil
  end

  def is_unit? kk
    kk && kk < Liza::Unit
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lizarb-1.0.4 lib/liza.rb