Class: Ego::PluginHelper
- Inherits:
-
Object
- Object
- Ego::PluginHelper
- Defined in:
- lib/ego/plugin_helper.rb
Overview
The PluginHelper assists the user in writing extensions by generating boilerplate code.
Instance Method Summary collapse
-
#hint ⇒ String
Provide a hint for initializing a new plug-in.
-
#initialize(query: nil, program_name: nil) ⇒ PluginHelper
constructor
A new instance of PluginHelper.
-
#path ⇒ String
Derive a plug-in path from the user query.
-
#slug ⇒ String
Derive a slug from the user query.
-
#template ⇒ String
Provide a template for initializing a new plug-in.
Constructor Details
#initialize(query: nil, program_name: nil) ⇒ PluginHelper
Returns a new instance of PluginHelper.
13 14 15 16 |
# File 'lib/ego/plugin_helper.rb', line 13 def initialize(query: nil, program_name: nil) @query = query || 'My new plugin' @program_name = program_name || 'ego' end |
Instance Method Details
#hint ⇒ String
Provide a hint for initializing a new plug-in.
43 44 45 46 47 48 49 50 51 |
# File 'lib/ego/plugin_helper.rb', line 43 def hint require 'shellwords' @hint ||= <<~HINT I don't understand "#{@query}". If you would like to add this capability, start by running: #{@program_name} #{@query.shellescape} > #{path} HINT end |
#path ⇒ String
Derive a plug-in path from the user query.
35 36 37 38 |
# File 'lib/ego/plugin_helper.rb', line 35 def path @path ||= Filesystem.config("plugins/#{slug}.rb") .sub(/^#{ENV['HOME']}/, '~') end |
#slug ⇒ String
Derive a slug from the user query.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ego/plugin_helper.rb', line 21 def slug @slug ||= @query .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr('\'', '') .gsub(/\W+/, '_') .gsub(/__+/, '_') .sub(/_$/, '') .downcase end |
#template ⇒ String
Provide a template for initializing a new plug-in.
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/ego/plugin_helper.rb', line 56 def template @template ||= <<~TEMPLATE Ego.plugin do |robot| robot.can 'do something new' robot.on(/^#{@query}$/i) do |params| alert 'Not implemented yet. Go ahead and edit #{path}.' end end TEMPLATE end |