Sha256: 79dd55ab801062b7c0c62620d54bb5cbb282110e886b4f20f0b171df120017b4

Contents?: true

Size: 1.9 KB

Versions: 4

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

require_relative "../logging/logger"

module Kanal
  module Core
    module Conditions
      # This class contains all needed functionality to store,
      # search conditions
      class ConditionStorage
        include Logging::Logger

        def initialize
          @condition_packs = []
        end

        def get_condition_pack_by_name!(name)
          pack = get_condition_pack_by_name name

          unless pack
            logger.fatal "Attempted to request unregistered condition pack #{name} from ConditionStorage"

            raise "Condition pack #{name} is not registered, but was requested from ConditionStorage"
          end

          pack
        end

        def get_condition_pack_by_name(name)
          @condition_packs.find { |p| p.name == name }
        end

        def condition_pack_or_condition_exists?(name)
          pack = get_condition_pack_by_name name

          unless pack
            found_condition = false
            # Checking every pack for conditions inside of it
            @condition_packs.each do |cp|
              condition = cp.get_condition_by_name name

              if condition
                found_condition = true
                break
              end
            end

            return found_condition
          end

          !pack.nil?
        end

        def register_condition_pack(pack)
          return if condition_pack_exists? pack

          unless pack.is_a? ConditionPack
            logger.fatal "Attempted to register condition pack which isn't of ConditionPack class"

            raise "Condition pack should be descendant of ConditionPack class"
          end

          logger.info "Registering condition pack '#{pack.name}'"

          @condition_packs.append pack
        end

        def condition_pack_exists?(pack)
          !(@condition_packs.find { |cp| cp.name == pack.name }).nil?
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
kanal-0.5.1 lib/kanal/core/conditions/condition_storage.rb
kanal-0.5.0 lib/kanal/core/conditions/condition_storage.rb
kanal-0.4.3 lib/kanal/core/conditions/condition_storage.rb
kanal-0.4.2 lib/kanal/core/conditions/condition_storage.rb