Class: YAML_storage_strategy

Inherits:
Data_storage_strategy show all
Defined in:
lib/models/data_storage_strategy/YAML_storage_strategy.rb

Instance Method Summary collapse

Instance Method Details

#read(file_path) ⇒ Object

read from yaml file



7
8
9
10
11
12
13
# File 'lib/models/data_storage_strategy/YAML_storage_strategy.rb', line 7

def read(file_path)
    return [] unless File.exist?(file_path)
    data = YAML.safe_load(File.read(file_path), permitted_classes: [Date, Symbol]) || []
    data.map do |student| 
        Student.new_from_hash(student) 
    end
end

#write(file_path, students) ⇒ Object

read to yaml file



16
17
18
19
# File 'lib/models/data_storage_strategy/YAML_storage_strategy.rb', line 16

def write(file_path, students)
    data = students.map { |student| student.to_h }
    File.write(file_path, data.to_yaml)
end