Class: Kanal::Core::Conditions::ConditionPack

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

Overview

This class stores conditions inside It is served as some kind of namespace for conditions, with specific name of pack and helper methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging::Logger

#logger

Constructor Details

#initialize(name) ⇒ ConditionPack

Returns a new instance of ConditionPack.



18
19
20
21
# File 'lib/kanal/core/conditions/condition_pack.rb', line 18

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/kanal/core/conditions/condition_pack.rb', line 16

def name
  @name
end

Instance Method Details

#get_condition_by_name(name) ⇒ Object



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

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

#get_condition_by_name!(name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kanal/core/conditions/condition_pack.rb', line 23

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

#register_condition(condition) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/kanal/core/conditions/condition_pack.rb', line 39

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