Class: FileDataSource

Inherits:
Object
  • Object
show all
Defined in:
lib/source/repositories/data_sources/file_data_source.rb

Overview

Источник данных из файла

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_transformer) ⇒ FileDataSource

Returns a new instance of FileDataSource.



13
14
15
16
17
# File 'lib/source/repositories/data_sources/file_data_source.rb', line 13

def initialize(data_transformer)
  self.students = []
  self.seq_id = 1
  self.data_transformer = data_transformer
end

Instance Attribute Details

#data_transformer=(value) ⇒ Object

Sets the attribute data_transformer

Parameters:

  • value

    the value to set the attribute data_transformer to.



11
12
13
# File 'lib/source/repositories/data_sources/file_data_source.rb', line 11

def data_transformer=(value)
  @data_transformer = value
end

Instance Method Details

#add_student(student) ⇒ Object



48
49
50
51
52
53
# File 'lib/source/repositories/data_sources/file_data_source.rb', line 48

def add_student(student)
  student.id = seq_id
  students << student
  self.seq_id += 1
  student.id
end

#load_from_file(file_path) ⇒ Object



19
20
21
22
23
# File 'lib/source/repositories/data_sources/file_data_source.rb', line 19

def load_from_file(file_path)
  hash_list = data_transformer.str_to_hash_list(File.read(file_path))
  self.students = hash_list.map { |h| Student.from_hash(h) }
  update_seq_id
end

#paginated_short_students(page, count, existing_data_list = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/source/repositories/data_sources/file_data_source.rb', line 34

def paginated_short_students(page, count, existing_data_list = nil)
  offset = (page - 1) * count
  slice = students[offset, count].map { |s| StudentShort.from_student(s) }

  return DataListStudentShort.new(slice) if existing_data_list.nil?

  existing_data_list.replace_objects(slice)
  existing_data_list
end

#remove_student(student_id) ⇒ Object



60
61
62
# File 'lib/source/repositories/data_sources/file_data_source.rb', line 60

def remove_student(student_id)
  students.reject! { |s| s.id == student_id }
end

#replace_student(student_id, student) ⇒ Object



55
56
57
58
# File 'lib/source/repositories/data_sources/file_data_source.rb', line 55

def replace_student(student_id, student)
  idx = students.find_index { |s| s.id == student_id }
  students[idx] = student
end

#save_to_file(file_path) ⇒ Object



25
26
27
28
# File 'lib/source/repositories/data_sources/file_data_source.rb', line 25

def save_to_file(file_path)
  hash_list = students.map(&:to_hash)
  File.write(file_path, data_transformer.hash_list_to_str(hash_list))
end

#sortedObject



44
45
46
# File 'lib/source/repositories/data_sources/file_data_source.rb', line 44

def sorted
  students.sort_by(&:last_name_and_initials)
end

#student_by_id(student_id) ⇒ Object



30
31
32
# File 'lib/source/repositories/data_sources/file_data_source.rb', line 30

def student_by_id(student_id)
  students.detect { |s| s.id == student_id }
end

#student_countObject



64
65
66
# File 'lib/source/repositories/data_sources/file_data_source.rb', line 64

def student_count
  students.count
end