Class: Kanal::Core::Conditions::Condition

Inherits:
Object
  • Object
show all
Includes:
Logging::Logger
Defined in:
lib/kanal/core/conditions/condition.rb

Overview

Base class for conditions with this class you can

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging::Logger

#logger

Constructor Details

#initialize(name, with_argument: false, &met_block) ⇒ Condition

Returns a new instance of Condition.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kanal/core/conditions/condition.rb', line 15

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

  unless met_block
    logger.fatal "Attempted to create condition #{name} without block"

    raise "Cannot create condition without block"
  end

  @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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/kanal/core/conditions/condition.rb', line 13

def name
  @name
end

Instance Method Details

#met?(input, core, argument) ⇒ Boolean

Check constructor for more info about this weird met block call

Returns:

  • (Boolean)


40
41
42
# File 'lib/kanal/core/conditions/condition.rb', line 40

def met?(input, core, argument)
  @proc_to_lambda_object.method(:met_block).call input, core, argument
end

#with_argument?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/kanal/core/conditions/condition.rb', line 35

def with_argument?
  @with_argument
end