Class: Ego::PluginHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/ego/plugin_helper.rb

Overview

The PluginHelper assists the user in writing extensions by generating boilerplate code.

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(query: nil, program_name: nil) ⇒ PluginHelper

Returns a new instance of PluginHelper.

Parameters:

  • query (String) (defaults to: nil)

    example user query

  • program_name (String) (defaults to: nil)

    the executable name



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

#hintString

Provide a hint for initializing a new plug-in.

Returns:

  • (String)

    hint text



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

#pathString

Derive a plug-in path from the user query.

Returns:

  • (String)

    plug-in path



35
36
37
38
# File 'lib/ego/plugin_helper.rb', line 35

def path
  @path ||= Filesystem.config("plugins/#{slug}.rb")
                      .sub(/^#{ENV['HOME']}/, '~')
end

#slugString

Derive a slug from the user query.

Returns:

  • (String)

    slug



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

#templateString

Provide a template for initializing a new plug-in.

Returns:

  • (String)

    template contents



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