class Sequent::Generator::Aggregate

Public Class Methods

new(name) click to toggle source
# File lib/sequent/generator/aggregate.rb, line 12
def initialize(name)
  @name = name
end

Public Instance Methods

execute() click to toggle source
# File lib/sequent/generator/aggregate.rb, line 16
def execute
  ensure_not_used!
  copy_files
  rename_files
  replace_template_aggregate
end
name() click to toggle source
# File lib/sequent/generator/aggregate.rb, line 23
def name
  @name ||= File.basename(path)
end

Private Instance Methods

copy_files() click to toggle source
# File lib/sequent/generator/aggregate.rb, line 29
def copy_files
  FileUtils.copy_entry(File.expand_path('template_aggregate', __dir__), path)
end
ensure_not_used!() click to toggle source
# File lib/sequent/generator/aggregate.rb, line 70
def ensure_not_used!
  if File.directory?("#{path}/#{name_underscored}") || File.exist?("#{path}/#{name_underscored}.rb")
    fail TargetAlreadyExists
  end
end
name_camelized() click to toggle source
# File lib/sequent/generator/aggregate.rb, line 66
def name_camelized
  @name_camelized ||= name.camelize
end
name_underscored() click to toggle source
# File lib/sequent/generator/aggregate.rb, line 62
def name_underscored
  @name_underscored ||= name.underscore
end
path() click to toggle source
# File lib/sequent/generator/aggregate.rb, line 58
def path
  @path ||= File.expand_path('lib')
end
rename_files() click to toggle source
# File lib/sequent/generator/aggregate.rb, line 33
def rename_files
  FileUtils.mv("#{path}/template_aggregate.rb", "#{path}/#{name_underscored}.rb")
  FileUtils.mv("#{path}/template_aggregate", "#{path}/#{name_underscored}")

  FileUtils.mv(
    "#{path}/#{name_underscored}/template_aggregate_command_handler.rb",
    "#{path}/#{name_underscored}/#{name_underscored}_command_handler.rb",
  )
  FileUtils.mv(
    "#{path}/#{name_underscored}/template_aggregate.rb",
    "#{path}/#{name_underscored}/#{name_underscored}.rb",
  )
end
replace_template_aggregate() click to toggle source
# File lib/sequent/generator/aggregate.rb, line 47
def replace_template_aggregate
  files = Dir["#{path}/**/*"].select { |f| File.file?(f) }

  files.each do |filename|
    contents = File.read(filename)
    contents.gsub!('template_aggregate', name_underscored)
    contents.gsub!('TemplateAggregate', name_camelized)
    File.open(filename, 'w') { |f| f.puts contents }
  end
end