Sha256: 1310897d268126962398bd427b99fd0e178c443c698a7799999004054f010df0

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

module Kanal
  module Core
    module Conditions
      # Base class for conditions
      # with this class you can
      class Condition
        attr_reader :name

        def initialize(name, with_argument: false, &met_block)
          @name = name

          raise "Cannot create condition without block" unless met_block

          @with_argument = with_argument

          # NOTE: this whole bunch of code, including method, is used to allow
          # in blocks returns without LocalJumpError
          # Basically converting block/proc into lambda, which will allow
          # unexpected returns for the comfortability of writing conditions
          # Kudos to: https://stackoverflow.com/a/2946734/2739103
          @proc_to_lambda_object = Object.new
          @proc_to_lambda_object.define_singleton_method(:met_block, &met_block)
        end

        def with_argument?
          @with_argument
        end

        # Check constructor for more info about this weird met block call
        def met?(input, core, argument)
          @proc_to_lambda_object.method(:met_block).call input, core, argument
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kanal-0.3.0 lib/kanal/core/conditions/condition.rb