Class: Alula::Storage::FileStorage

Inherits:
Alula::Storage show all
Defined in:
lib/alula/storages/filestorage.rb

Instance Attribute Summary

Attributes inherited from Alula::Storage

#options, #outputted

Instance Method Summary collapse

Methods inherited from Alula::Storage

#custom, load, #page, #post

Constructor Details

#initialize(options, opts) ⇒ FileStorage

Returns a new instance of FileStorage.



6
7
8
9
10
11
12
13
# File 'lib/alula/storages/filestorage.rb', line 6

def initialize(options, opts)
  super
  
  # Check we have given directories
  %w{content_path posts_path pages_path attachments_path}.each do |dir|
    raise "Directory #{options[dir]} for #{dir} does not exists!" unless ::File.directory?(options[dir])
  end
end

Instance Method Details

#attachmentsObject



24
25
26
# File 'lib/alula/storages/filestorage.rb', line 24

def attachments
  @attachments ||= _list_all_in(self.options["attachments_path"])
end

#customsObject



28
29
30
# File 'lib/alula/storages/filestorage.rb', line 28

def customs
  @customs ||= _list_all_in(self.options["custom_path"])
end

#output(path, file, &blk) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/alula/storages/filestorage.rb', line 71

def output(path, file, &blk)
  path = path(path, File.dirname(file))
  
  fname = ::File.join(path, File.basename(file))
  dirname = ::File.dirname(fname)
  FileUtils.mkdir_p dirname unless ::File.directory?(dirname)
  
  ::File.open(fname, "w") do |io|
    io.write yield io
  end
  
  fname
end

#output_public(path, &blk) ⇒ Object



64
65
66
67
68
69
# File 'lib/alula/storages/filestorage.rb', line 64

def output_public(path, &blk)
  fname = output(:public, path, &blk)
  
  # Mark saved file
  @outputted << fname
end

#pagesObject



20
21
22
# File 'lib/alula/storages/filestorage.rb', line 20

def pages
  @pages ||= _list_all_in(self.options["pages_path"])
end

#path(type, *appendum) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/alula/storages/filestorage.rb', line 36

def path(type, *appendum)
  dirname = case type
  when :public
    File.join(self.options["public_path"])
  when :custom
    File.join(self.options["custom_path"])
  when :assets
    File.join(self.options["public_path"], "assets")
  when :cache
    File.join(self.options["cache_path"])
  end

  dirname = File.join(dirname, appendum) if appendum
  
  FileUtils.mkdir_p dirname unless File.directory?(dirname)
  
  dirname
end

#postsObject

List all posts



16
17
18
# File 'lib/alula/storages/filestorage.rb', line 16

def posts
  @posts ||= _list_all_in(self.options["posts_path"])
end

#prepare(preserve = false) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/alula/storages/filestorage.rb', line 56

def prepare(preserve = false)
  if !preserve
    FileUtils.rm_rf self.options["public_path"]
  end
  
  FileUtils.mkdir_p self.options["public_path"]
end

#staticsObject



32
33
34
# File 'lib/alula/storages/filestorage.rb', line 32

def statics
  @statics ||= _list_all_in(self.options["static_path"])
end