Sha256: a8453c56577d26e7d271c3720f2c60a46002a8996589edac570018fd5d3bca23

Contents?: true

Size: 1.81 KB

Versions: 3

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

require_relative "./condition"
require_relative "./condition_creator"
require_relative "../logging/logger"

module Kanal
  module Core
    module Conditions
      # This class stores conditions inside
      # It is served as some kind of namespace for conditions, with specific
      # name of pack and helper methods
      class ConditionPack
        include Logging::Logger

        attr_reader :name

        def initialize(name)
          @name = name
          @conditions = []
        end

        def get_condition_by_name!(name)
          condition = get_condition_by_name name

          unless condition
            logger.fatal "Attempted to get condition #{name} in pack #{@name}"

            raise "Condition #{name} was not found in pack #{@name}. Maybe it was not added?"
          end

          condition
        end

        def get_condition_by_name(name)
          @conditions.find { |c| c.name == name }
        end

        def register_condition(condition)
          logger.debug "Attempting to register condition '#{condition.name}'"

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

            raise "Can register only conditions that inherit Condition class"
          end

          if condition_registered? condition
            logger.warn "Condition '#{condition.name}' already registered"
            return self
          end

          logger.debug "Registering condition '#{condition.name}'"

          @conditions.append condition

          self
        end

        def condition_registered?(condition)
          @conditions.each do |c|
            return true if c.name == condition.name
          end

          false
        end

        private :condition_registered?
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kanal-0.8.0 lib/kanal/core/conditions/condition_pack.rb
kanal-0.7.0 lib/kanal/core/conditions/condition_pack.rb
kanal-0.6.0 lib/kanal/core/conditions/condition_pack.rb