Class: Kanal::Core::Router::RouterNode
- Inherits:
-
Object
- Object
- Kanal::Core::Router::RouterNode
- Includes:
- Helpers, Logging::Logger, Output
- Defined in:
- lib/kanal/core/router/router_node.rb
Overview
This class is used as a node in router tree, containing conditions and responses
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #after_respond(&block) ⇒ Object
- #after_respond_async(&block) ⇒ Object
-
#assign_condition_pack_and_condition_names_from_args!(*args) ⇒ Object
This method processes args to populate condition and condition pack in this router node.
- #children? ⇒ Boolean
- #condition ⇒ Object
- #condition_met?(input, core) ⇒ Boolean
- #debug_info ⇒ Object
-
#initialize(*args, router:, parent:, default: false, root: false, error: false) ⇒ RouterNode
constructor
parameter default: is for knowing that this node is for default response default response cannot have child nodes.
- #on(*args, &block) ⇒ Object
- #respond(&block) ⇒ Object
- #respond_async(&block) ⇒ Object
- #response? ⇒ Boolean
- #response_blocks ⇒ Object
- #root? ⇒ Boolean
Methods included from Logging::Logger
Constructor Details
#initialize(*args, router:, parent:, default: false, root: false, error: false) ⇒ RouterNode
parameter default: is for knowing that this node is for default response default response cannot have child nodes
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/kanal/core/router/router_node.rb', line 25 def initialize(*args, router:, parent:, default: false, root: false, error: false) @router = router @parent = parent @children = [] @response_blocks = [] @condition_pack_name = nil @condition_name = nil @condition_argument = nil # We omit setting conditions because default router node does not need any conditions # Also root node does not have conditions so we basically omit them if arguments are empty return if default || root || error # With this we attach names of condition pack and condition to this router # node, so we will be able to find them later at runtime and use them assign_condition_pack_and_condition_names_from_args!(*args) end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
19 20 21 |
# File 'lib/kanal/core/router/router_node.rb', line 19 def children @children end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
19 20 21 |
# File 'lib/kanal/core/router/router_node.rb', line 19 def parent @parent end |
Instance Method Details
#after_respond(&block) ⇒ Object
75 76 77 78 79 |
# File 'lib/kanal/core/router/router_node.rb', line 75 def after_respond(&block) raise "Router node with children cannot have response" unless @children.empty? @response_blocks.append ResponseBlock.new(block, functional: true) end |
#after_respond_async(&block) ⇒ Object
81 82 83 84 85 |
# File 'lib/kanal/core/router/router_node.rb', line 81 def after_respond_async(&block) raise "Router node with children cannot have response" unless @children.empty? @response_blocks.append ResponseBlock.new(block, functional: true, async: true) end |
#assign_condition_pack_and_condition_names_from_args!(*args) ⇒ Object
This method processes args to populate condition and condition pack in this router node
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/kanal/core/router/router_node.rb', line 93 def assign_condition_pack_and_condition_names_from_args!(*args) condition_pack_name = args[0] condition_name = args[1] # We assume we got condition that requires argument if condition_name.is_a? Hash # We search for arguments inside kwargs @condition_argument = condition_name.values.first condition_name = condition_name.keys.first end # This calls will raise errors if there is problem with pack or condition # inside of it pack = @router.core.condition_storage.get_condition_pack_by_name! condition_pack_name condition = pack.get_condition_by_name! condition_name if condition.with_argument? && !@condition_argument logger.fatal "Condition requires argument, but was provided as :symbol, not as positional_arg:" raise "Condition requires argument, though you wrote it as :symbol, not as positional_arg: Please check route with condition pack: #{condition_pack_name} and condition: #{condition_name}" end @condition_pack_name = condition_pack_name @condition_name = condition_name end |
#children? ⇒ Boolean
141 142 143 |
# File 'lib/kanal/core/router/router_node.rb', line 141 def children? !@children.empty? end |
#condition ⇒ Object
131 132 133 134 135 |
# File 'lib/kanal/core/router/router_node.rb', line 131 def condition pack = @router.core.condition_storage.get_condition_pack_by_name! @condition_pack_name pack.get_condition_by_name! @condition_name end |
#condition_met?(input, core) ⇒ Boolean
125 126 127 128 129 |
# File 'lib/kanal/core/router/router_node.rb', line 125 def condition_met?(input, core) c = condition c.met? input, core, @condition_argument end |
#debug_info ⇒ Object
121 122 123 |
# File 'lib/kanal/core/router/router_node.rb', line 121 def debug_info "RouterNode with condition pack: #{@condition_pack_name}, condition: #{@condition_name}, condition argument: #{@condition_argument}" end |
#on(*args, &block) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/kanal/core/router/router_node.rb', line 46 def on(*args, &block) raise "You cannot add children to nodes with response ready. Response is a final line" if response? child = RouterNode.new(*args, router: @router, parent: self) add_child child child.instance_eval(&block) end |
#respond(&block) ⇒ Object
63 64 65 66 67 |
# File 'lib/kanal/core/router/router_node.rb', line 63 def respond(&block) raise "Router node with children cannot have response" unless @children.empty? @response_blocks.append ResponseBlock.new(block) end |
#respond_async(&block) ⇒ Object
69 70 71 72 73 |
# File 'lib/kanal/core/router/router_node.rb', line 69 def respond_async(&block) raise "Router node with children cannot have response" unless @children.empty? @response_blocks.append ResponseBlock.new(block, async: true) end |
#response? ⇒ Boolean
87 88 89 |
# File 'lib/kanal/core/router/router_node.rb', line 87 def response? !@response_blocks.empty? end |
#response_blocks ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/kanal/core/router/router_node.rb', line 55 def response_blocks if @response_blocks.empty? raise "no response block configured for this node. router: #{@router.name}. debug: #{debug_info}" end @response_blocks end |
#root? ⇒ Boolean
137 138 139 |
# File 'lib/kanal/core/router/router_node.rb', line 137 def root? parent.nil? end |