Class: Lucie::Snippets::Template::FileUtilsHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/lucie/snippets/template.rb

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ FileUtilsHelper

Returns a new instance of FileUtilsHelper.



27
28
29
# File 'lib/lucie/snippets/template.rb', line 27

def initialize(controller)
  @controller = controller
end

Instance Method Details

#add_content_to_file(path, &content) ⇒ Object



64
65
66
# File 'lib/lucie/snippets/template.rb', line 64

def add_content_to_file(path, &content)
  block_given? && File.open(path, "a") { |f| yield f } || File.new(path, "a")
end

#create_dir_for(file_path) ⇒ Object



57
58
59
60
61
62
# File 'lib/lucie/snippets/template.rb', line 57

def create_dir_for(file_path)
  dir_name = File.dirname(file_path)
  if !File.directory?(dir_name)
    FileUtils.mkdir_p(dir_name)
  end
end

#create_file(path, &content) ⇒ Object



31
32
33
34
35
# File 'lib/lucie/snippets/template.rb', line 31

def create_file(path, &content)
  path = normalized path
  create_dir_for path
  add_content_to_file path, &content
end

#is_relative?(file_path) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/lucie/snippets/template.rb', line 45

def is_relative?(file_path)
  file_path[0] != "/"
end

#normalized(path) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/lucie/snippets/template.rb', line 49

def normalized(path)
  if is_relative?(path)
    File.join([@controller.app.root, path])
  else
    path
  end
end

#template(template, target, binding) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/lucie/snippets/template.rb', line 37

def template(template, target, binding)
  template = "app/templates/#{template}" if is_relative?(template)
  target   = File.join([@controller.app.pwd, target]) if is_relative?(target)
  create_file(target) do |f|
    f.write ERB.new(File.read(normalized(template))).result(binding)
  end
end