Sha256: d1c421bb1b97b7c0bb2487ddb7aa774b4598136da24f75140704cf1b4ef0b8e4

Contents?: true

Size: 1.54 KB

Versions: 6

Compression:

Stored size: 1.54 KB

Contents

require 'json'
require 'yaml'

module McDelta
    class StudentsListFormatStrategy
        def read_from(filename)
            require 'method not implemented'
        end

        def write_to(filename, students)
            require 'method not implemented'
        end
    end

    class TxtStudentsListFormatStrategy < StudentsListFormatStrategy
        def read_from(filename)
            File.read(filename)
            .split("\n")
            .map { |v| Student.from_string(v) }
        end

        def write_to(filename, students)
            File.open(filename, 'w') { |file| 
                file.write(
                    students.map { |student| 
                        student.get_info
                    }
                    .join("\n")
                )
            }
        end
    end

    class JsonStudentsListFormatStrategy < StudentsListFormatStrategy
        def read_from(filename)
            file = File.read(filename)
            json = JSON.parse(file)
            json.map { |x| Student.from_json(x) }
        end

        def write_to(filename, students)
            File.open(filename, 'w') do |f|
                f.write(JSON.generate(students.map { |x| x.as_json }))
            end
        end
    end

    class YamlStudentsListFormatStrategy < StudentsListFormatStrategy
        def read_from(filename)
            YAML.load_file(filename)
        end
        
        def write_to(filename, students)
            File.open(filename, 'w') do |file|
                file.write(students.to_yaml)
            end
        end
    end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
shnaider_code-2.0.1 lib/source/students_list_format_strategy.rb
shnaider_code-2.0.0 lib/source/students_list_format_strategy.rb
mc_delta-1.0.0 lib/source/students_list_format_strategy.rb
mc_delta-0.9.0 lib/source/students_list_format_strategy.rb
mc_delta-0.8.0 lib/source/students_list_format_strategy.rb
mc_delta-0.7.0 lib/source/students_list_format_strategy.rb