Class: Soup::Backends::YAMLBackend
- Defined in:
- lib/soup/backends/yaml_backend.rb
Constant Summary collapse
- ATTRIBUTE_TOKEN =
"--- # Soup attributes"
Instance Method Summary collapse
- #all_snips ⇒ Object
- #destroy(name) ⇒ Object
-
#initialize(path = "soup") ⇒ YAMLBackend
constructor
A new instance of YAMLBackend.
- #load_snip(name) ⇒ Object
- #names ⇒ Object
- #prepare ⇒ Object
- #save_snip(attributes) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(path = "soup") ⇒ YAMLBackend
Returns a new instance of YAMLBackend.
6 7 8 |
# File 'lib/soup/backends/yaml_backend.rb', line 6 def initialize(path="soup") @base_path = path end |
Instance Method Details
#all_snips ⇒ Object
54 55 56 57 58 |
# File 'lib/soup/backends/yaml_backend.rb', line 54 def all_snips Dir[path_for("*")].map do |key| load_snip(File.basename(key, ".yml")) end end |
#destroy(name) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/soup/backends/yaml_backend.rb', line 44 def destroy(name) path = path_for(name) if File.exist?(path) File.delete(path) true else nil end end |
#load_snip(name) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/soup/backends/yaml_backend.rb', line 18 def load_snip(name) path = path_for(name) if File.exist?(path) file = File.read(path) if attribute_start = file.index(ATTRIBUTE_TOKEN) content = file.slice(0...attribute_start) attributes = {:name => name}.merge(YAML.load(file.slice(attribute_start..-1))) else attributes = {:content => file, :name => name} end attributes.update(:content => content) if content && content.length > 0 Snip.new(attributes, self) else nil end end |
#names ⇒ Object
14 15 16 |
# File 'lib/soup/backends/yaml_backend.rb', line 14 def names Dir[path_for("*")].map { |s| File.basename(s, ".yml") } end |
#prepare ⇒ Object
10 11 12 |
# File 'lib/soup/backends/yaml_backend.rb', line 10 def prepare FileUtils.mkdir_p(@base_path) end |
#save_snip(attributes) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/soup/backends/yaml_backend.rb', line 35 def save_snip(attributes) File.open(path_for(attributes[:name]), 'w') do |f| attributes_without_content = attributes.dup f.write attributes_without_content.delete(:content) f.write attributes_without_content.to_yaml.gsub(/^---\s/, ATTRIBUTE_TOKEN + "\n") if attributes_without_content.any? end Snip.new(attributes, self) end |