Class: Lucie::Snippets::Template::FileUtilsHelper
- Inherits:
-
Object
- Object
- Lucie::Snippets::Template::FileUtilsHelper
- Defined in:
- lib/lucie/snippets/template.rb
Instance Method Summary collapse
- #add_content_to_file(path, &content) ⇒ Object
- #create_dir_for(file_path) ⇒ Object
- #create_file(path, &content) ⇒ Object
-
#initialize(controller) ⇒ FileUtilsHelper
constructor
A new instance of FileUtilsHelper.
- #is_relative?(file_path) ⇒ Boolean
- #normalized(path) ⇒ Object
- #template(template, target, binding) ⇒ Object
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
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 |