Class: Student

Inherits:
StudentBase show all
Defined in:
lib/source/models/student.rb

Overview

Модель студента

Instance Attribute Summary collapse

Attributes inherited from StudentBase

#git, #id

Class Method Summary collapse

Instance Method Summary collapse

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

Стандартный конструктор. Принимает: Фамилия, Имя, Отчество, а также именованные параметры для предка



39
40
41
42
43
44
# File 'lib/source/models/student.rb', line 39

def initialize(last_name, first_name, father_name, **options)
  self.last_name = last_name
  self.first_name = first_name
  self.father_name = father_name
  super(**options)
end

Instance Attribute Details

#father_nameObject

Returns the value of attribute father_name.



35
36
37
# File 'lib/source/models/student.rb', line 35

def father_name
  @father_name
end

#first_nameObject

Returns the value of attribute first_name.



35
36
37
# File 'lib/source/models/student.rb', line 35

def first_name
  @first_name
end

#last_nameObject

Returns the value of attribute last_name.



35
36
37
# File 'lib/source/models/student.rb', line 35

def last_name
  @last_name
end

Class Method Details

.from_hash(hash) ⇒ Object

Конструктор из Hash. Ключи являются символами

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
# File 'lib/source/models/student.rb', line 14

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(str) ⇒ Object

Конструктор из JSON строки



28
29
30
31
# File 'lib/source/models/student.rb', line 28

def self.from_json_str(str)
  params = JSON.parse(str, { symbolize_names: true })
  from_hash(params)
end

Instance Method Details

#last_name_and_initialsObject

Вернуть фамилию и инициалы в виде строки “Фамилия И. О.”



76
77
78
# File 'lib/source/models/student.rb', line 76

def last_name_and_initials
  "#{last_name} #{first_name[0]}. #{father_name[0]}."
end

#set_contacts(phone: nil, telegram: nil, email: nil) ⇒ Object

Сеттер для массовой установки контактов



67
68
69
70
71
# File 'lib/source/models/student.rb', line 67

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_infoObject

Краткая информация о пользователе в виде JSON строки. Поля: last_name_and_initials - Фамилия и инициалы в виде “Фамилия И. О.” contact - Приоритетный доступный контакт в виде хеша (StudentBase#short_contact) git - Имя пользователя на гите



87
88
89
90
91
92
93
# File 'lib/source/models/student.rb', line 87

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_hashObject

Преобразование студента в хеш. Поля являются ключами



110
111
112
113
114
115
116
117
# File 'lib/source/models/student.rb', line 110

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_strObject

Преобразование студента в JSON строку.



122
123
124
# File 'lib/source/models/student.rb', line 122

def to_json_str
  JSON.generate(to_hash)
end

#to_sObject

Преобразование студента в строку



98
99
100
101
102
103
104
105
# File 'lib/source/models/student.rb', line 98

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