class Ruboty::Generator

Generator Core

Generator

Constants

README
RUBOTY_ACTION_TEMPLATE
RUBOTY_BASE_FILE
RUBOTY_GENERATOR_FILE
RUBOTY_GENERATOR_FILE_TEMPLATE
RUBOTY_HANDLER_FILE
RUBOTY_HANDLER_TEMPLATE
RUBOTY_MODULE_FILE
RUBOTY_MODULE_TEMPLATE
VERSION

Public Class Methods

generate(options = {}) click to toggle source

generate ruboty template.

# File lib/ruboty/generator/generate.rb, line 89
def self.generate(options = {})
  config = load_config
  execute_bundle_gem(config)
  module_src = generate_module(config)
  output_module(module_src, config.gem_name)
  handler_src = generate_handler(config, options[:a])
  output_handler(handler_src, config.gem_name)
  return unless options[:a]
  actions = generate_actions(config)
  output_actions(actions, config.gem_name)
end
init() click to toggle source

generate Rubotymegenfile to current directory.

# File lib/ruboty/generator/generate.rb, line 82
def self.init
  File.open(RUBOTY_GENERATOR_FILE, 'w') do |f|
    f.puts RUBOTY_GENERATOR_FILE_TEMPLATE
  end
end

Private Class Methods

execute_bundle_gem(config) click to toggle source
# File lib/ruboty/generator/generate.rb, line 114
def self.execute_bundle_gem(config)
  `bundle gem ruboty-#{config.gem_name}`
end
generate_action_definitions(config, gem_class_name, have_actions) click to toggle source
# File lib/ruboty/generator/generate.rb, line 155
def self.generate_action_definitions(config, gem_class_name, have_actions)
  if have_actions
    action_requires = config.commands.map { |e| "require \"ruboty/#{config.gem_name}/actions/#{e.read_name}\"" }.join("\n")
    action_definitions = generate_action_definitions_with_action(config, gem_class_name)
  else
    action_requires = ''
    action_definitions = generate_action_definitions_without_action(config)
  end
  action_macros = generate_action_macros(config)
  { action_requires: action_requires, action_definitions: action_definitions, action_macros: action_macros }
end
generate_action_definitions_with_action(config, gem_class_name) click to toggle source
# File lib/ruboty/generator/generate.rb, line 168
    def self.generate_action_definitions_with_action(config, gem_class_name)
      definitions = config.commands.map do |e|
        <<-EOS
      def #{e.read_name}(message)
        Ruboty::#{gem_class_name}::Actions::#{e.read_name.capitalize}.new(message).call
      end
        EOS
      end
      definitions.join("\n")
    end
generate_action_definitions_without_action(config) click to toggle source
# File lib/ruboty/generator/generate.rb, line 180
    def self.generate_action_definitions_without_action(config)
      definitions = config.commands.map do |e|
        <<-EOS
      def #{e.read_name}(message)
        # TODO: implement your action
      end
        EOS
      end
      definitions.join("\n")
    end
generate_action_macros(config) click to toggle source
# File lib/ruboty/generator/generate.rb, line 192
def self.generate_action_macros(config)
  action_macros = config.commands.map do |e|
    "      on /#{e.read_pattern}/, name: '#{e.read_name}', description: '#{e.read_description}'"
  end
  action_macros.join("\n")
end
generate_actions(config) click to toggle source
# File lib/ruboty/generator/generate.rb, line 205
def self.generate_actions(config)
  gem_class_name = config.gem_class_name
  config.commands.each_with_object([]) do |e, memo|
    action_name = e.read_name
    erb = ERB.new(RUBOTY_ACTION_TEMPLATE)
    action = {}
    action[:name] = action_name
    action[:contents] = erb.result(binding)
    memo << action
  end
end
generate_env(config) click to toggle source
# File lib/ruboty/generator/generate.rb, line 200
def self.generate_env(config)
  config.env.map { |e| "      env :#{e.read_name}, \"#{e.read_description}\"" }.join("\n")
end
generate_handler(config, have_actions) click to toggle source
# File lib/ruboty/generator/generate.rb, line 134
def self.generate_handler(config, have_actions)
  gem_class_name = config.gem_class_name
  gem_name = config.gem_name
  description = config.description
  actions = generate_action_definitions(config, gem_class_name, have_actions)
  action_requires = actions[:action_requires]
  action_definitions = actions[:action_definitions]
  action_macros = actions[:action_macros]
  envs = generate_env(config)
  erb = ERB.new(RUBOTY_HANDLER_TEMPLATE)
  erb.result(binding)
end
generate_module(config) click to toggle source
# File lib/ruboty/generator/generate.rb, line 119
def self.generate_module(config)
  gem_class_name = config.gem_class_name
  gem_name = config.gem_name
  erb = ERB.new(RUBOTY_MODULE_TEMPLATE)
  erb.result(binding)
end
load_config() click to toggle source
# File lib/ruboty/generator/generate.rb, line 101
def self.load_config
  src = read_dsl
  dsl = Ruboty::Dsl.new
  dsl.instance_eval(src)
  dsl.ruboty_generator
end
output_actions(actions, handler_name) click to toggle source
# File lib/ruboty/generator/generate.rb, line 218
def self.output_actions(actions, handler_name)
  actions.each do |action|
    actions_dir_path = "./ruboty-#{handler_name}/#{Ruboty::Generator::RUBOTY_BASE_FILE}/#{handler_name}/actions"
    FileUtils.mkdir_p(actions_dir_path)
    File.open("#{actions_dir_path}/#{action[:name]}.rb", 'w:utf-8') { |e| e.puts action[:contents] }
  end
end
output_handler(handler_src, handler_name) click to toggle source
# File lib/ruboty/generator/generate.rb, line 148
def self.output_handler(handler_src, handler_name)
  handler_path = "./ruboty-#{handler_name}/#{Ruboty::Generator::RUBOTY_HANDLER_FILE}"
  FileUtils.mkdir_p(handler_path)
  File.open("#{handler_path}/#{handler_name}.rb", 'w:utf-8') { |e| e.puts handler_src }
end
output_module(module_src, module_name) click to toggle source
# File lib/ruboty/generator/generate.rb, line 127
def self.output_module(module_src, module_name)
  module_path = "./ruboty-#{module_name}/#{Ruboty::Generator::RUBOTY_MODULE_FILE}"
  FileUtils.mkdir_p(module_path)
  File.open("#{module_path}/#{module_name}.rb", 'w:utf-8') { |e| e.puts module_src }
end
read_dsl() click to toggle source
# File lib/ruboty/generator/generate.rb, line 109
def self.read_dsl
  File.open(RUBOTY_GENERATOR_FILE, &:read)
end