Class: Hanami::Generators::Generator

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hanami/generators/generator.rb

Defined Under Namespace

Classes: Processor

Instance Method Summary collapse

Constructor Details

#initialize(template_source_path, target_path) ⇒ Generator

Returns a new instance of Generator


16
17
18
19
20
21
22
# File 'lib/hanami/generators/generator.rb', line 16

def initialize(template_source_path, target_path)
  @template_source_path = template_source_path
  @target_path = target_path
  @template_mappings = []
  @processor = Processor.new
  @processor.class.source_root(@template_source_path)
end

Instance Method Details

#add_mapping(source, target) ⇒ Object


24
25
26
# File 'lib/hanami/generators/generator.rb', line 24

def add_mapping(source, target)
  @template_mappings << [source, target]
end

#prepend_after_leading_comments(path, *args, &block) ⇒ Object

Modelled after Thor's `inject_into_class`


35
36
37
38
39
40
41
# File 'lib/hanami/generators/generator.rb', line 35

def prepend_after_leading_comments(path, *args, &block)
  config = args.last.is_a?(Hash) ? args.pop : {}
  # Either prepend after the last comment line,
  # or the first line in the file, if there are no comments
  config.merge!(after: /\A(?:^#.*$\s)*/)
  @processor.insert_into_file(path, *(args << config), &block)
end

#process_templates(options = {}) ⇒ Object


28
29
30
31
32
# File 'lib/hanami/generators/generator.rb', line 28

def process_templates(options = {})
  @template_mappings.each do |src, dst|
    @processor.template(@template_source_path.join(src), @target_path.join(dst), options)
  end
end