Class: Mosaic::Stores::Yaml

Inherits:
Object
  • Object
show all
Defined in:
lib/mosaic/stores/yaml.rb

Overview

Store data in yaml files

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Yaml

Returns a new instance of Yaml.



9
10
11
12
13
14
15
16
17
18
# File 'lib/mosaic/stores/yaml.rb', line 9

def initialize(name)
  @store = name
  @directory = "#{Mosaic.root}/app/stores"
  begin
    @yaml = YAML::load(File.open("#{@directory}/#{@store}.yml"))
  rescue
    create_file_and_folder
    @yaml = YAML::load(File.open("#{@directory}/#{@store}.yml"))
  end
end

Instance Attribute Details

#yamlObject (readonly)

Returns the value of attribute yaml.



7
8
9
# File 'lib/mosaic/stores/yaml.rb', line 7

def yaml
  @yaml
end

Instance Method Details

#create_file_and_folderObject

Create the files and folders if they are missing



21
22
23
24
25
26
27
# File 'lib/mosaic/stores/yaml.rb', line 21

def create_file_and_folder
  begin
    Dir::mkdir(@directory)
  rescue Errno::EEXIST
  end
  FileUtils.touch "#{@directory}/#{@store}.yml"
end

#save(hash) ⇒ Object

Write the hash back to the yaml file



30
31
32
# File 'lib/mosaic/stores/yaml.rb', line 30

def save(hash)
  File.open("#{@directory}/#{@store}.yml", 'w+') {|f| f.write(hash.to_yaml) }
end