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

Version Path
student_mvp-0.1.10 lib/models/data_storage_strategy/JSON_storage_strategy.rb
student_mvp-0.1.9 lib/models/data_storage_strategy/JSON_storage_strategy.rb
student_mvp-0.1.8 lib/models/data_storage_strategy/JSON_storage_strategy.rb
student_mvp-0.1.7 lib/models/data_storage_strategy/JSON_storage_strategy.rb
student_mvp-0.1.6 lib/models/data_storage_strategy/JSON_storage_strategy.rb
student_mvp-0.1.5 lib/models/data_storage_strategy/JSON_storage_strategy.rb
student_mvp-0.1.4 lib/models/data_storage_strategy/JSON_storage_strategy.rb
student_mvp-0.1.3 lib/models/data_storage_strategy/JSON_storage_strategy.rb
student_mvp-0.1.2 lib/models/data_storage_strategy/JSON_storage_strategy.rb
student_mvp-0.1.1 lib/models/data_storage_strategy/JSON_storage_strategy.rb
student_mvp-0.1.0 lib/models/data_storage_strategy/JSON_storage_strategy.rb