Class: Student
- Inherits:
-
StudentBase
- Object
- StudentBase
- Student
- Defined in:
- lib/models/student.rb
Instance Attribute Summary collapse
-
#father_name ⇒ Object
Стандартные геттеры для полей.
-
#first_name ⇒ Object
Стандартные геттеры для полей.
-
#last_name ⇒ Object
Стандартные геттеры для полей.
Attributes inherited from StudentBase
Class Method Summary collapse
- .from_hash(hash) ⇒ Object
-
.from_json_str(str) ⇒ Object
Конструктор из JSON строки.
Instance Method Summary collapse
-
#initialize(last_name, first_name, father_name, **options) ⇒ Student
constructor
Стандартный конструктор.
-
#last_name_and_initials ⇒ Object
Имя пользователя в формате Фамилия И.
-
#set_contacts(phone: nil, telegram: nil, email: nil) ⇒ Object
Отдельный сеттер для массовой установки контактов.
-
#short_info ⇒ Object
Краткая информация о пользователе.
- #to_hash ⇒ Object
- #to_json_str ⇒ Object
-
#to_s ⇒ Object
Методы приведения объекта к строке.
Methods inherited from StudentBase
#has_contacts?, #has_git?, #short_contact, #valid?, valid_email?, valid_name?, valid_phone?, valid_profile_name?
Constructor Details
#initialize(last_name, first_name, father_name, **options) ⇒ Student
Стандартный конструктор
33 34 35 36 37 38 |
# File 'lib/models/student.rb', line 33 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
Стандартные геттеры для полей
30 31 32 |
# File 'lib/models/student.rb', line 30 def father_name @father_name end |
#first_name ⇒ Object
Стандартные геттеры для полей
30 31 32 |
# File 'lib/models/student.rb', line 30 def first_name @first_name end |
#last_name ⇒ Object
Стандартные геттеры для полей
30 31 32 |
# File 'lib/models/student.rb', line 30 def last_name @last_name end |
Class Method Details
.from_hash(hash) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/models/student.rb', line 10 def self.from_hash(hash) 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(str) ⇒ Object
Конструктор из JSON строки
21 22 23 24 |
# File 'lib/models/student.rb', line 21 def self.from_json_str(str) params = JSON.parse(str, { symbolize_names: true }) from_hash(params) end |
Instance Method Details
#last_name_and_initials ⇒ Object
Имя пользователя в формате Фамилия И. О.
67 68 69 |
# File 'lib/models/student.rb', line 67 def last_name_and_initials "#{last_name} #{first_name[0]}. #{father_name[0]}." end |
#set_contacts(phone: nil, telegram: nil, email: nil) ⇒ Object
Отдельный сеттер для массовой установки контактов
60 61 62 63 64 |
# File 'lib/models/student.rb', line 60 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
Краткая информация о пользователе
72 73 74 75 76 77 78 |
# File 'lib/models/student.rb', line 72 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
90 91 92 93 94 95 96 97 |
# File 'lib/models/student.rb', line 90 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
99 100 101 |
# File 'lib/models/student.rb', line 99 def to_json_str JSON.generate(to_hash) end |
#to_s ⇒ Object
Методы приведения объекта к строке
81 82 83 84 85 86 87 88 |
# File 'lib/models/student.rb', line 81 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 |