Class: YamlRecord::Adapters::LocalStore

Inherits:
Object
  • Object
show all
Defined in:
lib/yaml_record/adapters/local_store.rb

Instance Method Summary collapse

Instance Method Details

#read(source, dynamic_source, source_file_name) ⇒ Object

Returns YAML File as ruby collection

Example:

@adapter.read("foo") => [{...}, {...}]


13
14
15
16
17
18
19
20
21
# File 'lib/yaml_record/adapters/local_store.rb', line 13

def read(source, dynamic_source, source_file_name)
  dir_path = source + dynamic_source
  full_path = dir_path + source_file_name

  Dir::mkdir(dir_path) unless File.directory?(dir_path)
  File.open(full_path, 'w') unless File.exists?(full_path)

  YAML.load_file(source + dynamic_source + source_file_name)
end

#write(source, source_file_name, dynamic_source, collection) ⇒ Object

Writes ruby collection as YAML File

Example:

@adapter.write("foo", [{...}, {...}]) => "<yaml data>"


29
30
31
32
33
34
35
36
37
# File 'lib/yaml_record/adapters/local_store.rb', line 29

def write(source, source_file_name, dynamic_source, collection)
  dir_path = source + dynamic_source
  full_path = dir_path + source_file_name

  Dir::mkdir(dir_path) unless File.directory?(dir_path)
  File.open(full_path, 'w') unless File.exists?(full_path)
  
  File.open(source + dynamic_source + source_file_name, 'w') {|f| f.write(collection.to_yaml) }
end