Class: Trekyll::TrekyllFileManager

Inherits:
Object
  • Object
show all
Defined in:
lib/trekyll/filemanager.rb

Constant Summary collapse

DIR_DEFAULTS =

Defoult directory structure

{
# "source"         => Dir.pwd,
"data_dir"       => "_data",
"widgets_dir"    => "_widgets",
"posts_dir"      => "_posts",
"blocks_dir"     => "_blocks"
}
FILE_DEFAULTS =

Defoult file structure

{
"_data"       => "nav.yml"
}

Instance Method Summary collapse

Constructor Details

#initializeTrekyllFileManager

Adding new attribute => value in DIR_DEFOULTS this constructor creates getter and setter method for newly entered property (directory)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/trekyll/filemanager.rb', line 26

def initialize()

    DIR_DEFAULTS.map{ |attribute_name, attribute_value|

        self.class.send(:define_method, "#{attribute_name}=".to_sym) do |value|
          instance_variable_set("@" + attribute_name.to_s, value)
        end

        self.class.send(:define_method, attribute_name.to_sym) do
          instance_variable_get("@" + attribute_name.to_s)
        end

        self.send("#{attribute_name}=".to_sym, attribute_value)

    }
end

Instance Method Details

#create_file_structObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/trekyll/filemanager.rb', line 57

def create_file_struct
    #Create directories
    DIR_DEFAULTS.map { |k,dir|
        dirname = File.dirname(dir + "/empty")
        puts "Creating dir: #{dirname}"
        unless File.directory?(dirname)
            FileUtils::mkdir_p(dirname)
        end
    }
    # Create files in correspondin directories
    FILE_DEFAULTS.map { |dir,file|
        puts "Creating file: #{file}"
        File.new(dir + "/" + file, 'w')
    }
end

#delete_file_structObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/trekyll/filemanager.rb', line 43

def delete_file_struct
    # Clear all existing md files except index.md
    Dir.glob( Dir.pwd + '/*.md').each do |file|
        if file != Dir.pwd + "/index.md"
            File.delete(file)
        end
    end
    puts "+++++ Trekyll is: +++++"
    puts "** Flushing old data **"

    # Clear dir structure
    DIR_DEFAULTS.map{ |k,dir_name| FileUtils.rm_rf(dir_name) }
end