Class: GoNative::Utils::TemplateInflator

Inherits:
Object
  • Object
show all
Extended by:
DSL::Serviceable
Defined in:
lib/gonative/utils/template_inflator.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ TemplateInflator

Returns a new instance of TemplateInflator.



13
14
15
# File 'lib/gonative/utils/template_inflator.rb', line 13

def initialize(options)
  @options = options
end

Instance Method Details

#callObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gonative/utils/template_inflator.rb', line 17

def call
  Dir.glob('*/').each do |dir|
    dir_name = dir.delete_suffix('/')
    FileUtils.cd(dir_name) do
      call
    end
    normalized_name = normalized_name(dir_name)
    FileUtils.mv(dir_name, normalized_name) if dir_name != normalized_name
  end
  inflate_files
end

#contents(file) ⇒ Object



36
37
38
39
40
# File 'lib/gonative/utils/template_inflator.rb', line 36

def contents(file)
  content = File.read(file)
  context = OpenStruct.new(options)
  ERB.new(content).result(context.instance_eval { binding })
end

#inflate_filesObject



29
30
31
32
33
34
# File 'lib/gonative/utils/template_inflator.rb', line 29

def inflate_files
  Dir.glob("*#{TEMPLATE_FILES_EXTENSION}").each do |file|
    File.write(normalized_name(file), contents(file))
    FileUtils.rm(file)
  end
end

#normalized_name(file_name) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/gonative/utils/template_inflator.rb', line 42

def normalized_name(file_name)
  new_name = file_name.dup
  options.each do |key, value|
    new_name.gsub!(key.to_s.upcase, value)
  end
  new_name.delete_suffix(TEMPLATE_FILES_EXTENSION)
end