Module: Polyseerio::Agent::Helper
- Defined in:
- lib/agent/helper.rb
Overview
Helper functions for Agent
Class Method Summary collapse
-
.create_handler(iteratee) ⇒ Object
Returns a handler function.
-
.create_subtype_iterator(handlers, iteratee, *args) ⇒ Object
Returns an iterator for a handler subtype.
-
.extract_handler_options(options) ⇒ Object
Extracts handler options and then filters them based on if they are enabled or not.
-
.filter_enabled_handler_options(options) ⇒ Object
Given handler options, enabled subtype options are returned.
-
.filter_handlers(options) ⇒ Object
Given agent options, handlers options are returned.
-
.generate_name ⇒ Object
Returns a unique name.
-
.handle?(value) ⇒ Boolean
Determines if a handler configuration should be handled.
-
.reduce_handler_option ⇒ Object
Reduce handler options based on if they are enabled.
-
.resolve_name(config) ⇒ Object
Given an agent config a name will be returned.
-
.setup_with_handler(*args) ⇒ Object
Sets up a handler type.
-
.teardown_with_handler(*args) ⇒ Object
Tears down a handler type.
Class Method Details
.create_handler(iteratee) ⇒ Object
Returns a handler function.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/agent/helper.rb', line 23 def self.create_handler(iteratee) proc do |map, client, config, type, *args| if map.key? type.to_sym if config.key? type # to_sym? Polyseerio.log 'debug', "Performing handler work for: #{type}." # TODO: unit-test passed args work = config.fetch(type) .map(&iteratee.call(map.fetch(type), client, *args)) Concurrent::Promise.zip(*work) else Concurrent::Promise.fulfill [] end else Concurrent::Promise.reject('Could not find a handler type: ' \ "#{type}.") end end end |
.create_subtype_iterator(handlers, iteratee, *args) ⇒ Object
Returns an iterator for a handler subtype.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/agent/helper.rb', line 11 def self.create_subtype_iterator(handlers, iteratee, *args) proc do |key, value| if handlers.key? key.to_sym # TODO: unit-test to_sym iteratee.call(handlers, key, value, *args) else Concurrent::Promise.reject('Could not find a handler subtype: ' \ "#{key}.") end end end |
.extract_handler_options(options) ⇒ Object
Extracts handler options and then filters them based on if they are enabled or not. TODO: unit-test or integration-test
118 119 120 121 122 |
# File 'lib/agent/helper.rb', line 118 def self.() = Handler. filter_handlers end |
.filter_enabled_handler_options(options) ⇒ Object
Given handler options, enabled subtype options are returned.
103 104 105 |
# File 'lib/agent/helper.rb', line 103 def self.() .each_with_object({}, &reduce_handler_option) end |
.filter_handlers(options) ⇒ Object
Given agent options, handlers options are returned.
108 109 110 111 112 113 114 |
# File 'lib/agent/helper.rb', line 108 def self.filter_handlers() .each_with_object({}) do |(name, config), acc| acc[name] = config acc end end |
.generate_name ⇒ Object
Returns a unique name.
89 90 91 |
# File 'lib/agent/helper.rb', line 89 def self.generate_name 'ruby-instance' end |
.handle?(value) ⇒ Boolean
Determines if a handler configuration should be handled.
125 126 127 128 129 130 131 132 133 |
# File 'lib/agent/helper.rb', line 125 def self.handle?(value) return value if value == true if Functional::TypeCheck::Type?(value, Hash) && (value.key? :enabled) return value[:enabled] end false end |
.reduce_handler_option ⇒ Object
Reduce handler options based on if they are enabled.
94 95 96 97 98 99 100 |
# File 'lib/agent/helper.rb', line 94 def self.reduce_handler_option proc do |(name, config), acc| acc[name] = config if handle? config acc end end |
.resolve_name(config) ⇒ Object
Given an agent config a name will be returned.
136 137 138 139 140 |
# File 'lib/agent/helper.rb', line 136 def self.resolve_name(config) return config[:name] if config.key?(:name) && !config[:name].nil? generate_name end |
.setup_with_handler(*args) ⇒ Object
Sets up a handler type.
79 80 81 |
# File 'lib/agent/helper.rb', line 79 def self.setup_with_handler(*args) create_handler(@setup).curry.call(*args) end |
.teardown_with_handler(*args) ⇒ Object
Tears down a handler type.
84 85 86 |
# File 'lib/agent/helper.rb', line 84 def self.teardown_with_handler(*args) create_handler(@teardown).curry.call(*args) end |