Class: BoringBuilder::Initializer

Inherits:
Object
  • Object
show all
Defined in:
lib/boringbuilder/initializer.rb

Constant Summary collapse

TEMPLATES =
%i[auto ruby node rust go generic].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, template: :auto) ⇒ Initializer

Returns a new instance of Initializer.



12
13
14
15
# File 'lib/boringbuilder/initializer.rb', line 12

def initialize(root, template: :auto)
  @root = Pathname.new(root).expand_path
  @requested_template = template.to_s.downcase.to_sym
end

Instance Attribute Details

#requested_templateObject (readonly)

Returns the value of attribute requested_template.



10
11
12
# File 'lib/boringbuilder/initializer.rb', line 10

def requested_template
  @requested_template
end

#rootObject (readonly)

Returns the value of attribute root.



10
11
12
# File 'lib/boringbuilder/initializer.rb', line 10

def root
  @root
end

Instance Method Details

#call(force: false) ⇒ Object

Raises:



17
18
19
20
21
22
23
24
# File 'lib/boringbuilder/initializer.rb', line 17

def call(force: false)
  validate!
  raise ConfigurationError, "#{path} already exists; pass --force to replace it" if path.exist? && !force

  FileUtils.mkdir_p(path.dirname)
  path.write(template)
  path
end

#template_nameObject



26
27
28
29
30
31
32
33
34
# File 'lib/boringbuilder/initializer.rb', line 26

def template_name
  return requested_template unless requested_template == :auto
  return :ruby if root.join("Gemfile").file?
  return :node if root.join("package.json").file?
  return :rust if root.join("Cargo.toml").file?
  return :go if root.join("go.mod").file?

  :generic
end