Sha256: 6fc73e70c9bd7272a44cb9d0c45e6c1661ce733c5c97e776c9ec32231bfcf7df
Contents?: true
Size: 597 Bytes
Versions: 11
Compression:
Stored size: 597 Bytes
Contents
require 'json' require_relative '../student/student.rb' require_relative './data_storage_strategy.rb' class JSON_storage_strategy < Data_storage_strategy # read from json file def read(file_path) return [] unless File.exist?(file_path) data = JSON.parse(File.read(file_path), symbolize_names: true) rescue [] data.map do |data| Student.new(**data) end end # read to json file def write(file_path, students) data = students.map { |student| student.to_h } File.write(file_path, JSON.pretty_generate(data)) end end
Version data entries
11 entries across 11 versions & 1 rubygems