Class: Student
- Inherits:
-
AbstractStudent
- Object
- AbstractStudent
- Student
- Defined in:
- lib/source/models/student.rb,
lib/source/models/student/student.rb
Instance Attribute Summary collapse
-
#father_name ⇒ Object
Стандартные геттеры для полей.
-
#first_name ⇒ Object
Стандартные геттеры для полей.
-
#id ⇒ Object
writeonly
Sets the attribute id.
-
#last_name ⇒ Object
Стандартные геттеры для полей.
Attributes inherited from AbstractStudent
Class Method Summary collapse
- .from_hash(hash) ⇒ Object
- .from_json(str) ⇒ Object
-
.from_json_str(str) ⇒ Object
Конструктор из JSON строки.
- .from_yaml(str) ⇒ Object
- .read_from_txt(file_path) ⇒ Object
- .write_to_txt(file_path, students) ⇒ Object
Instance Method Summary collapse
- #contact ⇒ Object
- #fio ⇒ Object
-
#initialize(last_name, first_name, paternal_name, options = {}) ⇒ Student
constructor
Стандартный конструктор.
-
#last_name_and_initials ⇒ Object
Имя пользователя в формате Фамилия И.
-
#set_contacts(contacts) ⇒ Object
Отдельный сеттер для массовой установки контактов.
-
#short_info ⇒ Object
Краткая информация о пользователе.
- #to_hash ⇒ Object
- #to_json_str ⇒ Object
-
#to_s ⇒ Object
Методы приведения объекта к строке.
- #validate ⇒ Object
Methods inherited from AbstractStudent
Constructor Details
#initialize(last_name, first_name, paternal_name, options = {}) ⇒ Student
Стандартный конструктор
34 35 36 37 38 39 |
# File 'lib/source/models/student.rb', line 34 def initialize(last_name, first_name, father_name, **) self.last_name = last_name self.first_name = first_name self.father_name = father_name super(**) end |
Instance Attribute Details
#father_name ⇒ Object
Стандартные геттеры для полей
31 32 33 |
# File 'lib/source/models/student.rb', line 31 def father_name @father_name end |
#first_name ⇒ Object
Стандартные геттеры для полей
31 32 33 |
# File 'lib/source/models/student.rb', line 31 def first_name @first_name end |
#id=(value) ⇒ Object (writeonly)
Sets the attribute id
8 9 10 |
# File 'lib/source/models/student/student.rb', line 8 def id=(value) @id = value end |
#last_name ⇒ Object
Стандартные геттеры для полей
31 32 33 |
# File 'lib/source/models/student.rb', line 31 def last_name @last_name end |
Class Method Details
.from_hash(hash) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/source/models/student.rb', line 10 def self.from_hash(hash) hash = hash.dup raise ArgumentError, 'Fields required: fist_name, last_name, father_name' unless hash.key?(:first_name) && hash.key?(:last_name) && hash.key?(:father_name) first_name = hash.delete(:first_name) last_name = hash.delete(:last_name) father_name = hash.delete(:father_name) Student.new(last_name, first_name, father_name, **hash) end |
.from_json(str) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/source/models/student/student.rb', line 51 def self.from_json(str) unless str.is_a?(String) str = JSON.pretty_generate(str) end result = JSON.parse(str) raise ArgumentError, 'Missing fields: last_name, first_name, paternal_name' unless result.key?('first_name') && result.key?('last_name') && result.key?('paternal_name') last_name = result.delete('last_name') first_name = result.delete('first_name') paternal_name = result.delete('paternal_name') Student.new(last_name, first_name, paternal_name, **result.transform_keys(&:to_sym)) end |
.from_json_str(str) ⇒ Object
Конструктор из JSON строки
22 23 24 25 |
# File 'lib/source/models/student.rb', line 22 def self.from_json_str(str) params = JSON.parse(str, { symbolize_names: true }) from_hash(params) end |
.from_yaml(str) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/source/models/student/student.rb', line 64 def self.from_yaml(str) result = YAML.load(str) raise ArgumentError, 'Missing fields: last_name, first_name, paternal_name' unless result.key?('first_name') && result.key?('last_name') && result.key?('paternal_name') last_name = result.delete('last_name') first_name = result.delete('first_name') paternal_name = result.delete('paternal_name') Student.new(last_name, first_name, paternal_name, **result.transform_keys(&:to_sym)) end |
.read_from_txt(file_path) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/source/models/student/student.rb', line 74 def self.read_from_txt(file_path) arr = [] begin File.foreach(file_path) do |line| arr += [Student.from_json(line)] end return arr rescue => exception raise "File not found at the given address #{file_path}. Exception: #{exception.}" end end |
.write_to_txt(file_path, students) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/source/models/student/student.rb', line 86 def self.write_to_txt(file_path, students) begin File.open(file_path, 'w') do |file| students.each do |student| file.puts student.to_s end end rescue => exception raise "Error writing to file at the given address #{file_path}. Exception: #{exception.}" end end |
Instance Method Details
#contact ⇒ Object
34 35 36 37 38 39 |
# File 'lib/source/models/student/student.rb', line 34 def contact return "#{phone}" unless phone.nil? return "#{telegram}" unless telegram.nil? return "#{email}" unless email.nil? nil end |
#fio ⇒ Object
29 30 31 |
# File 'lib/source/models/student/student.rb', line 29 def fio "#{last_name} #{first_name.upcase[0]}. #{paternal_name.upcase[0]}." end |
#last_name_and_initials ⇒ Object
Имя пользователя в формате Фамилия И. О.
68 69 70 |
# File 'lib/source/models/student.rb', line 68 def last_name_and_initials "#{last_name} #{first_name[0]}. #{father_name[0]}." end |
#set_contacts(contacts) ⇒ Object
Отдельный сеттер для массовой установки контактов
61 62 63 64 65 |
# File 'lib/source/models/student.rb', line 61 def set_contacts(phone: nil, telegram: nil, email: nil) self.phone = phone if phone self.telegram = telegram if telegram self.email = email if email end |
#short_info ⇒ Object
Краткая информация о пользователе
73 74 75 76 77 78 79 |
# File 'lib/source/models/student.rb', line 73 def short_info info = {} info[:last_name_and_initials] = last_name_and_initials info[:contact] = short_contact info[:git] = git JSON.generate(info) end |
#to_hash ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/source/models/student.rb', line 91 def to_hash attrs = {} %i[last_name first_name father_name id phone telegram email git].each do |attr| attr_val = send(attr) attrs[attr] = attr_val unless attr_val.nil? end attrs end |
#to_json_str ⇒ Object
100 101 102 |
# File 'lib/source/models/student.rb', line 100 def to_json_str JSON.generate(to_hash) end |
#to_s ⇒ Object
Методы приведения объекта к строке
82 83 84 85 86 87 88 89 |
# File 'lib/source/models/student.rb', line 82 def to_s result = "#{last_name} #{first_name} #{father_name}" %i[id phone telegram email git].each do |attr| attr_val = send(attr) result += ", #{attr}=#{attr_val}" unless attr_val.nil? end result end |
#validate ⇒ Object
41 42 43 |
# File 'lib/source/models/student/student.rb', line 41 def validate git? && contact? end |