Sha256: a34793a1478868b2dae6519301153c71b43e7a2d9eb43d49539f4f6520d768c4

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

require_relative "../logging/logger"

module Kanal
  module Core
    module Conditions
      # This class helps creating conditions in dsl way,
      # with using helper methods
      class ConditionCreator
        include Logging::Logger

        def initialize(name)
          @name = name
          @met_block = nil
          @with_argument = false
        end

        def create(&block)
          logger.info "Attempting to create condition '#{@name}'"

          instance_eval(&block)

          unless @name
            logger.fatal "Attempted to create condition without name"

            raise "Please provide name for condition"
          end

          unless @met_block
            logger.fatal "Attempted to create condition without met block"

            raise "Please provide met? block for condition #{@name}"
          end

          logger.info "Creating condition '#{@name}'"

          Condition.new @name, with_argument: @with_argument, &@met_block
        end

        def with_argument
          @with_argument = true
        end

        def met?(&block)
          @met_block = block
        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_creator.rb
kanal-0.5.0 lib/kanal/core/conditions/condition_creator.rb
kanal-0.4.3 lib/kanal/core/conditions/condition_creator.rb
kanal-0.4.2 lib/kanal/core/conditions/condition_creator.rb