Sha256: bd0263b94b4e2c9e0f5014d46d63d5e98946c15c8e3a65c3ecbb12394d9b93bb

Contents?: true

Size: 927 Bytes

Versions: 2

Compression:

Stored size: 927 Bytes

Contents

require_relative 'student_list_strategy'
require 'json'

class StudentListTxt < StudentListStrategy
  public_class_method :new

  #Переопределяем методы
  def string_to_list(str)
    to_hash(str.split("\n").map(&:chomp))
  end

  def list_to_string(list)
    to_string(list)
  end

  #делаем приватными методы реализующие перевод из list в string и наоборот
  private

  def to_hash( list_strings)
    list_hashes = []
    list_strings.each do
    |str|
      str = str.gsub('"', "")
      h = {}
      str.split(',').each do |atr|
        key, value = atr.split(':').map(&:strip)
        h[key.to_sym] = value
      end
      list_hashes << h
    end
    list_hashes
  end

  def to_string (hashes)
    str = hashes.map do
    |h|
      h.map{
        |key, value|
        "#{key}: #{value.inspect}"
      }.join(",")
    end
    str.join("\n")
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rafmycat-1.0.1 lib/source/strategy/students_list_txt.rb
rafmycat-1.0.0 lib/source/strategy/students_list_txt.rb