Class: Rdm::Gen::Package

Inherits:
Object
  • Object
show all
Includes:
Concerns::TemplateHandling
Defined in:
lib/rdm/gen/package.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::TemplateHandling

included

Constructor Details

#initialize(current_dir, package_name, package_relative_path, skip_tests = false) ⇒ Package

Returns a new instance of Package.



19
20
21
22
23
24
# File 'lib/rdm/gen/package.rb', line 19

def initialize(current_dir, package_name, package_relative_path, skip_tests = false)
  @current_dir           = File.expand_path(current_dir)
  @package_name          = package_name
  @package_relative_path = package_relative_path
  @skip_tests            = skip_tests
end

Instance Attribute Details

#current_dirObject

Returns the value of attribute current_dir.



18
19
20
# File 'lib/rdm/gen/package.rb', line 18

def current_dir
  @current_dir
end

#package_nameObject

Returns the value of attribute package_name.



18
19
20
# File 'lib/rdm/gen/package.rb', line 18

def package_name
  @package_name
end

#package_relative_pathObject

Returns the value of attribute package_relative_path.



18
19
20
# File 'lib/rdm/gen/package.rb', line 18

def package_relative_path
  @package_relative_path
end

#skip_testsObject

Returns the value of attribute skip_tests.



18
19
20
# File 'lib/rdm/gen/package.rb', line 18

def skip_tests
  @skip_tests
end

Class Method Details

.generate(current_dir:, package_name:, package_relative_path:, skip_tests: false) ⇒ Object



9
10
11
12
13
# File 'lib/rdm/gen/package.rb', line 9

def generate(current_dir:, package_name:, package_relative_path:, skip_tests: false)
  Rdm::Gen::Package.new(
    current_dir, package_name, package_relative_path, skip_tests
  ).create
end

Instance Method Details

#append_package_to_rdm_packagesObject



70
71
72
73
# File 'lib/rdm/gen/package.rb', line 70

def append_package_to_rdm_packages
  new_source_content = source_content.strip + "\npackage '#{package_relative_path}'"
  File.write(source_path, new_source_content)
end

#check_preconditions!Object



60
61
62
63
64
65
66
67
68
# File 'lib/rdm/gen/package.rb', line 60

def check_preconditions!
  if Dir.exist?(File.join(current_dir, package_relative_path))
    raise Rdm::Errors::PackageDirExists, 'package dir exists'
  end

  if rdm_source.package_paths.include?(package_relative_path)
    raise Rdm::Errors::PackageExists, 'package exists'
  end
end

#createObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rdm/gen/package.rb', line 38

def create
  check_preconditions!
  package_subdir_name = Rdm.settings.send(:package_subdir_name)

  Dir.chdir(current_dir) do
    ensure_file([package_relative_path, '.gitignore'])
    ensure_file([package_relative_path, package_subdir_name, package_name, '.gitignore'])
    ensure_file(
      [package_relative_path, package_subdir_name, "#{package_name}.rb"],
      template_content('main_module_file.rb.erb', package_name_camelized: package_name.camelize)
    )
    ensure_file(
      [package_relative_path, 'Package.rb'],
      template_content('package.rb.erb', package_name: package_name)
    )
    init_rspec unless skip_tests

    move_templates
    append_package_to_rdm_packages
  end
end

#init_rspecObject



75
76
77
78
79
80
# File 'lib/rdm/gen/package.rb', line 75

def init_rspec
  Dir.chdir(templates_path) do
    copy_template('.rspec')
    copy_template('spec/spec_helper.rb')
  end
end

#move_templatesObject



82
83
84
85
86
# File 'lib/rdm/gen/package.rb', line 82

def move_templates
  Dir.chdir(templates_path) do
    copy_template('bin/console_irb', 'bin/console')
  end
end

#rdm_sourceObject



26
27
28
# File 'lib/rdm/gen/package.rb', line 26

def rdm_source
  @rdm_source ||= Rdm::SourceParser.new(source_path).parse_source_content
end

#source_contentObject



34
35
36
# File 'lib/rdm/gen/package.rb', line 34

def source_content
  File.open(source_path).read
end

#source_pathObject



30
31
32
# File 'lib/rdm/gen/package.rb', line 30

def source_path
  File.join(current_dir, Rdm::SOURCE_FILENAME)
end