Class: Rabal::FileTree

Inherits:
ActionTree show all
Defined in:
lib/rabal/file_tree.rb

Overview

Represents a file to be created. Generally a FileTree will use a source .erb file and combine it with the Rabal::Specification to create file_contents. The file_contents are what are written out in the action.

To provide custom template or some decision making, overwrite the before_action method. So long as file_contents contains the data to write out to the file by the time action is called all is well.

Instance Attribute Summary collapse

Attributes inherited from Tree

#children, #name, #parameters, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ActionTree

#after_action, #process

Methods inherited from Tree

#<<, #add_at_path, #current_path, #depth, #each, #find_subtree, #has_subtree?, #is_leaf?, #is_root?, #method_missing, #post_add, #root, #size, #tree_at_path, #walk

Constructor Details

#initialize(file_name, template) ⇒ FileTree

Create a FileTree with a name from a template



54
55
56
57
58
59
# File 'lib/rabal/file_tree.rb', line 54

def initialize(file_name,template)
    super(file_name)
    @file_name = file_name
    @template = template
    @file_contents = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rabal::Tree

Instance Attribute Details

#file_contentsObject

the contents of the file to write



25
26
27
# File 'lib/rabal/file_tree.rb', line 25

def file_contents
  @file_contents
end

#file_nameObject

the name of the file to create



19
20
21
# File 'lib/rabal/file_tree.rb', line 19

def file_name
  @file_name
end

#templateObject

the erb template the file is created from as a String



22
23
24
# File 'lib/rabal/file_tree.rb', line 22

def template
  @template
end

Class Method Details

.from_file(path, strip_ext = true, non_default_file_name = nil) ⇒ Object

Create a new FileTree from the path to a file. The file_name by default of the FileTree is basename of the path, minus any extension. This can be altered with the strip_extension and file_name parameters. Passing nil for the file_name achieves the default behavior.

The contents of the file at path will be loaded into the template member variable.



39
40
41
42
43
44
45
46
47
# File 'lib/rabal/file_tree.rb', line 39

def from_file(path,strip_ext = true, non_default_file_name = nil)
    template  = IO.read(File.expand_path(path))
    file_name = non_default_file_name || File.basename(path)
    file_name = File.basename(file_name,(strip_ext ? ".*" : ""))
    if file_name.index("-") then
        file_name = file_name.underscore
    end
    FileTree.new(file_name,template)
end

Instance Method Details

#actionObject

Open up the file and write the contents. It is assumed that the present working directory is the location for the file to be created.



83
84
85
86
87
88
89
90
91
92
# File 'lib/rabal/file_tree.rb', line 83

def action
    if not File.file?(file_name) then
        info("creating #{file_name}")
        File.open(file_name,"w+") do |f|
            f.write(file_contents)
        end
    else
        debug("skipping #{file_name} - already exists")
    end
end

#before_actionObject

before the file is to be created, load the appropriate template and setup any variables that need to be in the binding for erb if there is one



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rabal/file_tree.rb', line 66

def before_action
    debug("creating content for #{file_name}")
    begin 
        @file_contents = ERB.new(template).result(binding)
    rescue Exception => e
        error("Error evaluating template #{file_name}")
        e.message.split("\n").each do |m|
            error(m)
        end
    end
end