Sha256: 93bf1aeb9948b3c0558f4c41ca43455480a026be854ac7dba5e0e61305a7e48c

Contents?: true

Size: 1.33 KB

Versions: 9

Compression:

Stored size: 1.33 KB

Contents

require 'json'
require 'yaml'

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

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
mc_delta-0.4.0 lib/source/students_list_format_strategy.rb
mc_delta-0.3.0 lib/source/students_list_format_strategy.rb
mc_delta-0.2.0 lib/source/students_list_format_strategy.rb
mc_delta-0.1.0 lib/source/students_list_format_strategy.rb
model_gem-0.2.0 lib/model_gem_source/students_list_format_strategy.rb
model_gem-0.1.9 lib/model_gem_source/students_list_format_strategy.rb
model_gem-0.1.8 lib/model_gem_source/students_list_format_strategy.rb
model_gem-0.1.7 ./model_gem_source/students_list_format_strategy.rb
model_gem-0.1.6 ./model_gem_source/students_list_format_strategy.rb