Class: Student

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

Instance Attribute Summary collapse

Attributes inherited from StudentShort

#git, #id, #initials, #last_name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from StudentShort

#contact?, from_str, #git?, #validate

Constructor Details

#initialize(last_name, first_name, paternal_name, id: nil, git: nil, phone: nil, email: nil, telegram: nil) ⇒ Student

стандартный конструктор



32
33
34
35
36
37
38
39
# File 'lib/source/models/student.rb', line 32

def initialize(last_name, first_name, paternal_name, id: nil, git: nil, phone: nil, email: nil, telegram: nil)
  self.last_name = last_name
  self.first_name = first_name
  self.paternal_name = paternal_name
  self.id = id
  self.git = git
  set_contacts(phone: phone, email: email, telegram: telegram)
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



9
10
11
# File 'lib/source/models/student.rb', line 9

def email
  @email
end

#first_nameObject

Returns the value of attribute first_name.



9
10
11
# File 'lib/source/models/student.rb', line 9

def first_name
  @first_name
end

#id=(value) ⇒ Object (writeonly)

стандартные геттеры и сеттеры для класса



8
9
10
# File 'lib/source/models/student.rb', line 8

def id=(value)
  @id = value
end

#paternal_nameObject

Returns the value of attribute paternal_name.



9
10
11
# File 'lib/source/models/student.rb', line 9

def paternal_name
  @paternal_name
end

#phoneObject

Returns the value of attribute phone.



9
10
11
# File 'lib/source/models/student.rb', line 9

def phone
  @phone
end

#telegramObject

Returns the value of attribute telegram.



9
10
11
# File 'lib/source/models/student.rb', line 9

def telegram
  @telegram
end

Class Method Details

.from_hash(hash) ⇒ Object

конструктор принимающий на вход хээээш

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
# File 'lib/source/models/student.rb', line 43

def self.from_hash(hash)
  hash = hash.dup
  raise ArgumentError, 'Missing fields: last_name, first_name, paternal_name' unless hash.key?(:first_name) && hash.key?(:last_name) && hash.key?(:paternal_name)

  first_name = hash.delete(:first_name)
  last_name = hash.delete(:last_name)
  paternal_name = hash.delete(:paternal_name)

  Student.new(last_name, first_name,  paternal_name, **hash)
end

.init_from_json(str) ⇒ Object

конструктор из json-строки



64
65
66
67
# File 'lib/source/models/student.rb', line 64

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

.valid_account?(account) ⇒ Boolean

валидаТОР профиля

Returns:

  • (Boolean)


22
23
24
# File 'lib/source/models/student.rb', line 22

def self.valid_account?()
  .match(/^@[A-Za-z0-9\-_]+$/)
end

.valid_email?(email) ⇒ Boolean

валидаТОР почты

Returns:

  • (Boolean)


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

def self.valid_email?(email)
  email.match(/^[A-Za-z0-9\-_]+@[A-Za-z]+\.([A-Za-z]+\.)*[A-Za-z]+$/)
end

.valid_name?(name) ⇒ Boolean

валидаТОР имени

Returns:

  • (Boolean)


17
18
19
# File 'lib/source/models/student.rb', line 17

def self.valid_name?(name)
  name.match(/^[А-Я][а-я]+$/)
end

.valid_phone?(phone) ⇒ Boolean

валидаТОР номера телефона

Returns:

  • (Boolean)


12
13
14
# File 'lib/source/models/student.rb', line 12

def self.valid_phone?(phone)
  phone.match(/^\+?[7,8] ?\(?\d{3}\)?-?\d{3}-?\d{2}-?\d{2}$/)
end

Instance Method Details

#git=(git) ⇒ Object

Raises:

  • (ArgumentError)


94
95
96
97
98
# File 'lib/source/models/student.rb', line 94

def git=(git)
  raise ArgumentError, "Incorrect value: git=#{git}!" if !git.nil? && !Student.valid_account?(git)

  @git = git
end

#last_name=(last_name) ⇒ Object

Raises:

  • (ArgumentError)


82
83
84
85
86
# File 'lib/source/models/student.rb', line 82

def last_name=(last_name)
  raise ArgumentError, "Incorrect value: last_name=#{last_name}" if !last_name.nil? && !Student.valid_name?(last_name)

  @last_name = last_name
end

#last_name_and_initialsObject

метод возвращающий фамилию и инициалы у объекта



113
114
115
# File 'lib/source/models/student.rb', line 113

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

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



127
128
129
130
131
132
# File 'lib/source/models/student.rb', line 127

def set_contacts(phone: nil, telegram: nil, email: nil)
  self.phone = phone if phone
  self.telegram = telegram if telegram
  self.email = email if email
  @contact = contact
end

#short_infoObject

метод возвращающий краткую инф-ю об объекте



118
119
120
121
122
123
124
# File 'lib/source/models/student.rb', line 118

def short_info
  info = {}
  info[:last_name_and_initials] = last_name_and_initials
  info[:contact] = contact
  info[:git] = git
  JSON.generate(info)
end

#to_hashObject



54
55
56
57
58
59
60
61
# File 'lib/source/models/student.rb', line 54

def to_hash
  attrs = {}
  %i[last_name first_name paternal_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_sObject

метод возвращающий представление объекта в виде строки



135
136
137
138
139
140
141
142
143
# File 'lib/source/models/student.rb', line 135

def to_s
  result = "#{last_name} #{first_name} #{paternal_name}"
  result += " id=#{id}" unless id.nil?
  result += " phone=#{phone}" unless phone.nil?
  result += " git=#{git}" unless git.nil?
  result += " telegram=#{telegram}" unless telegram.nil?
  result += " email=#{email}" unless email.nil?
  result
end