Class: MarkdownRecord::FileSaver

Inherits:
Object
  • Object
show all
Includes:
PathUtilities
Defined in:
lib/markdown_record/rendering/file_saver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PathUtilities

#base_content_root_name, #base_rendered_path, #base_rendered_root, #clean_path, #erb_locals_from_path, #fragment_attributes_from_path, #full_path_to_parts, #path_to_fragment_id, #remove_prefix, #rendered_path, #scoped_id_to_parts, #to_scoped_id

Constructor Details

#initializeFileSaver

Returns a new instance of FileSaver.



7
8
9
# File 'lib/markdown_record/rendering/file_saver.rb', line 7

def initialize
  @saved_files = {:html => [], :json => []}
end

Instance Attribute Details

#saved_filesObject

Returns the value of attribute saved_files.



5
6
7
# File 'lib/markdown_record/rendering/file_saver.rb', line 5

def saved_files
  @saved_files
end

Instance Method Details

#base_content_nameObject



29
30
31
# File 'lib/markdown_record/rendering/file_saver.rb', line 29

def base_content_name
  ::MarkdownRecord.config.content_root.basename
end

#file_name(subdirectory, extension, fragments) ⇒ Object



33
34
35
36
37
# File 'lib/markdown_record/rendering/file_saver.rb', line 33

def file_name(subdirectory, extension, fragments)
  base_name = ::MarkdownRecord.config.content_root.join(subdirectory).basename.to_s
  base_name += "_fragments" if fragments
  "#{base_name}.#{extension}"
end

#save_to_file(content, rendered_subdirectory, options, fragments = false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/markdown_record/rendering/file_saver.rb', line 11

def save_to_file(content, rendered_subdirectory, options, fragments = false)
  filename, subdirectory, extension = full_path_to_parts(rendered_subdirectory)
  filename = file_name(filename, extension, fragments)

  save_path = ::MarkdownRecord.config.rendered_content_root.join(subdirectory).join(filename)
  relative_path = save_path.to_s.gsub(Rails.root.to_s, "")

  @saved_files[extension.to_sym].unshift(relative_path)

  if options[:save]
    save_path.dirname.mkpath

    save_path.open('wb') do |file|
      file << content
    end
  end
end