class Sequent::Generator::Command
Attributes
attrs[R]
command[R]
Public Class Methods
new(name, command, attrs)
click to toggle source
# File lib/sequent/generator/command.rb, line 16 def initialize(name, command, attrs) @name = name @command = command @attrs = attrs.map { |a| a.split(':') } end
Public Instance Methods
execute()
click to toggle source
# File lib/sequent/generator/command.rb, line 22 def execute ensure_existing_aggregate! add_command_to_aggregate end
name()
click to toggle source
# File lib/sequent/generator/command.rb, line 27 def name @name ||= File.basename(path) end
Private Instance Methods
add_command_to_aggregate()
click to toggle source
# File lib/sequent/generator/command.rb, line 62 def add_command_to_aggregate append_command append_command_handler end
append_command()
click to toggle source
# File lib/sequent/generator/command.rb, line 58 def append_command File.open("#{path_to_dir}/commands.rb", 'a') { |f| f << command_template.result(binding) } end
append_command_handler()
click to toggle source
# File lib/sequent/generator/command.rb, line 33 def append_command_handler ast = Parser::CurrentRuby.parse(File.read("#{path_to_dir}/#{name_underscored}_command_handler.rb")) target_cursor_position = find_target_cursor_position(ast) File.open("#{path_to_dir}/#{name_underscored}_command_handler.rb", 'r+') do |f| f.seek(target_cursor_position, IO::SEEK_SET) lines_to_be_overwritten = f.read f.seek(target_cursor_position, IO::SEEK_SET) f << command_handler_template.result(binding).gsub(/^.+(\s)$/) { |x| x.gsub!(Regexp.last_match(1), '') } f << lines_to_be_overwritten end end
command_handler_template()
click to toggle source
# File lib/sequent/generator/command.rb, line 91 def command_handler_template ERB.new(File.read(File.join(File.dirname(__FILE__), 'template_command_handler.erb'))) end
command_template()
click to toggle source
# File lib/sequent/generator/command.rb, line 87 def command_template ERB.new(File.read(File.join(File.dirname(__FILE__), 'template_command.erb'))) end
ensure_existing_aggregate!()
click to toggle source
# File lib/sequent/generator/command.rb, line 79 def ensure_existing_aggregate! if !File.directory?(path_to_dir) || !File.exist?("#{path_to_dir}/#{name_underscored}_command_handler.rb") || !File.exist?("#{path_to_dir}/commands.rb") fail NoAggregateFound end end
find_target_cursor_position(ast)
click to toggle source
# File lib/sequent/generator/command.rb, line 46 def find_target_cursor_position(ast) return unless ast.children.any? return if ast.children.any? { |c| c.class.to_s != 'Parser::AST::Node' } if (child = ast.children.find { |c| c.type.to_s == 'block' }) return child.loc.expression.end_pos end ast.children.map do |c| find_target_cursor_position(c) end&.flatten&.compact&.max end
name_underscored()
click to toggle source
# File lib/sequent/generator/command.rb, line 71 def name_underscored @name_underscored ||= name.underscore end
path()
click to toggle source
# File lib/sequent/generator/command.rb, line 67 def path @path ||= File.expand_path('lib') end
path_to_dir()
click to toggle source
# File lib/sequent/generator/command.rb, line 75 def path_to_dir @path_to_dir ||= "#{path}/#{name_underscored}" end