Class: Waylon::SkillRegistry
- Inherits:
-
Object
- Object
- Waylon::SkillRegistry
- Includes:
- Singleton
- Defined in:
- lib/waylon/skill_registry.rb
Overview
A place to track all skills known to this instance of Waylon
Instance Attribute Summary collapse
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Class Method Summary collapse
- .find_by_name(name) ⇒ Object
-
.register(name, class_name, condition) ⇒ Route
A wrapper around the singleton #register method.
- .route(message) ⇒ Object
Instance Method Summary collapse
-
#default_route(message) ⇒ Waylon::Route
Provides the default route based on the received message.
-
#help(user) ⇒ Hash
Gathers a Hash of help data for all routes a user is permitted to access.
-
#log(message, level = :info) ⇒ Object
Simple pass-through for logging through the Waylon Logger.
-
#register(name, skill_class, condition) ⇒ Route
Add the provided Skill subclass to the registry under ‘name`.
-
#route(message) ⇒ Hash
Given a message, find a suitable skill Route for it (sorted by priority, highest first) rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength rubocop:disable Metrics/PerceivedComplexity.
Instance Attribute Details
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
8 9 10 |
# File 'lib/waylon/skill_registry.rb', line 8 def routes @routes end |
Class Method Details
.find_by_name(name) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/waylon/skill_registry.rb', line 19 def self.find_by_name(name) [ *instance.routes, Routes::PermissionDenied.new, Routes::BlackHole.new, Routes::Default.new ].find { |r| r.name == name.to_s } end |
.register(name, class_name, condition) ⇒ Route
A wrapper around the singleton #register method
15 16 17 |
# File 'lib/waylon/skill_registry.rb', line 15 def self.register(name, class_name, condition) instance.register(name, class_name, condition) end |
.route(message) ⇒ Object
28 29 30 |
# File 'lib/waylon/skill_registry.rb', line 28 def self.route() instance.route() end |
Instance Method Details
#default_route(message) ⇒ Waylon::Route
Provides the default route based on the received message.
35 36 37 |
# File 'lib/waylon/skill_registry.rb', line 35 def default_route() .to_bot? ? Routes::Default.new : Routes::BlackHole.new end |
#help(user) ⇒ Hash
Gathers a Hash of help data for all routes a user is permitted to access
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/waylon/skill_registry.rb', line 42 def help(user) data = {} @routes.select { |r| r.permits?(user) && r.mention_only? }.each do |permitted| data[permitted.destination.component_namespace] ||= [] data[permitted.destination.component_namespace] << if permitted.help { name: permitted.name, help: permitted.help } else { name: permitted.name } end end data.reject { |_k, v| v.empty? } end |
#log(message, level = :info) ⇒ Object
Simple pass-through for logging through the Waylon Logger
59 60 61 |
# File 'lib/waylon/skill_registry.rb', line 59 def log(, level = :info) ::Waylon::Logger.log(, level) end |
#register(name, skill_class, condition) ⇒ Route
Add the provided Skill subclass to the registry under ‘name`
68 69 70 71 72 73 |
# File 'lib/waylon/skill_registry.rb', line 68 def register(name, skill_class, condition) raise Exceptions::ValidationError, "Must be a kind of Skill" unless skill_class.ancestors.include?(Skill) @routes ||= [] @routes << Route.new(name: name.to_s, destination: skill_class, condition:) end |
#route(message) ⇒ Hash
Given a message, find a suitable skill Route for it (sorted by priority, highest first) rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength rubocop:disable Metrics/PerceivedComplexity
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/waylon/skill_registry.rb', line 82 def route() route = nil = .body.strip @routes ||= [] @routes.sort_by(&:priority).reverse.each do |this_route| if this_route.permits?(.) && this_route.matches?() && (this_route.properly_mentions?() || .private?) route = this_route elsif this_route.permits?(.) && this_route.matches?() && !this_route.properly_mentions?() # Black hole these because they're not direct mentions route = Routes::BlackHole.new elsif this_route.matches?() route = Routes::PermissionDenied.new end if route log("Using route '#{route.name}' based on '#{}' from '#{..email}'", :debug) break end end route end |