Class: Soup::Backends::FileBackend

Inherits:
Base
  • Object
show all
Defined in:
lib/soup/backends/file_backend.rb

Instance Method Summary collapse

Methods inherited from Base

#find

Constructor Details

#initialize(path = "soup") ⇒ FileBackend

Returns a new instance of FileBackend.



4
5
6
# File 'lib/soup/backends/file_backend.rb', line 4

def initialize(path="soup")
  @base_path = path
end

Instance Method Details

#all_snipsObject



44
45
46
47
48
# File 'lib/soup/backends/file_backend.rb', line 44

def all_snips
  snip_paths.map do |path|
    load_snip_from_path(path, snip_name_from_path(path))
  end
end

#destroy(name) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/soup/backends/file_backend.rb', line 34

def destroy(name)
  path = path_for(name)
  if File.exist?(path)
    File.delete(path)
    true
  else
    nil
  end
end

#load_snip(name) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/soup/backends/file_backend.rb', line 16

def load_snip(name)
  path = snip_paths.find { |s| snip_name_from_path(s) == name }
  if path
    load_snip_from_path(path, name)
  else
    nil
  end
end

#namesObject



12
13
14
# File 'lib/soup/backends/file_backend.rb', line 12

def names
  snip_paths.map { |s| snip_name_from_path(s) }
end

#prepareObject



8
9
10
# File 'lib/soup/backends/file_backend.rb', line 8

def prepare
  FileUtils.mkdir_p(@base_path)
end

#save_snip(attributes) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/soup/backends/file_backend.rb', line 25

def save_snip(attributes)
  File.open(path_for(attributes[:name], attributes[:extension]), 'w') do |f|
    attributes_without_content = attributes.dup
    f.write attributes_without_content.delete(:content)
    f.write attributes_without_content.to_yaml.gsub(/^---\s/, "\n\n") if attributes_without_content.any?
  end
  Snip.new(attributes, self)
end