Class: FileDataSource
- Inherits:
-
Object
- Object
- FileDataSource
- Defined in:
- lib/repositories/data_sources/file_data_source.rb
Instance Attribute Summary collapse
-
#data_transformer ⇒ Object
writeonly
Sets the attribute data_transformer.
Instance Method Summary collapse
- #add_student(student) ⇒ Object
-
#initialize(data_transformer) ⇒ FileDataSource
constructor
A new instance of FileDataSource.
- #load_from_file(file_path) ⇒ Object
-
#paginated_short_students(page, count, existing_data_list = nil) ⇒ Object
Получить page по счету count элементов (страница начинается с 1).
- #remove_student(student_id) ⇒ Object
- #replace_student(student_id, student) ⇒ Object
- #save_to_file(file_path) ⇒ Object
- #sorted ⇒ Object
- #student_by_id(student_id) ⇒ Object
- #student_count ⇒ Object
Constructor Details
#initialize(data_transformer) ⇒ FileDataSource
Returns a new instance of FileDataSource.
10 11 12 13 14 |
# File 'lib/repositories/data_sources/file_data_source.rb', line 10 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
8 9 10 |
# File 'lib/repositories/data_sources/file_data_source.rb', line 8 def data_transformer=(value) @data_transformer = value end |
Instance Method Details
#add_student(student) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/repositories/data_sources/file_data_source.rb', line 46 def add_student(student) student.id = seq_id students << student self.seq_id += 1 student.id end |
#load_from_file(file_path) ⇒ Object
16 17 18 19 20 |
# File 'lib/repositories/data_sources/file_data_source.rb', line 16 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
Получить page по счету count элементов (страница начинается с 1)
32 33 34 35 36 37 38 39 40 |
# File 'lib/repositories/data_sources/file_data_source.rb', line 32 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
58 59 60 |
# File 'lib/repositories/data_sources/file_data_source.rb', line 58 def remove_student(student_id) students.reject! { |s| s.id == student_id } end |
#replace_student(student_id, student) ⇒ Object
53 54 55 56 |
# File 'lib/repositories/data_sources/file_data_source.rb', line 53 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
22 23 24 25 |
# File 'lib/repositories/data_sources/file_data_source.rb', line 22 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 |
#sorted ⇒ Object
42 43 44 |
# File 'lib/repositories/data_sources/file_data_source.rb', line 42 def sorted students.sort_by(&:last_name_and_initials) end |
#student_by_id(student_id) ⇒ Object
27 28 29 |
# File 'lib/repositories/data_sources/file_data_source.rb', line 27 def student_by_id(student_id) students.detect { |s| s.id == student_id } end |
#student_count ⇒ Object
62 63 64 |
# File 'lib/repositories/data_sources/file_data_source.rb', line 62 def student_count students.count end |