Class: Student

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

Instance Attribute Summary collapse

Attributes inherited from StudentShort

#initials

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from StudentShort

from_str

Constructor Details

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

конструктор

Raises:

  • (ArgumentError)


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

def initialize(last_name:nil, first_name:nil, paternal_name:nil, id:nil, phone:nil, git:nil, telegram:nil, email:nil)
  raise ArgumentError, "Required fields: last_name, first_name and paternal_name!" if last_name.nil?|| first_name.nil? || paternal_name.nil?
  self.last_name = last_name
  self.first_name = first_name
  self.paternal_name = paternal_name
  self.id = id
  self.git = git
  self.set_contacts(phone:phone, telegram:telegram, email:email)
end

Instance Attribute Details

#email ⇒ Object

Returns the value of attribute email.



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

def email
  @email
end

#first_name ⇒ Object

Returns the value of attribute first_name.



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

def first_name
  @first_name
end

#git ⇒ Object

Returns the value of attribute git.



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

def git
  @git
end

#id ⇒ Object

создаем методы чтения и записи attr_accessor



6
7
8
# File 'lib/source/student/student.rb', line 6

def id
  @id
end

#last_name ⇒ Object

Returns the value of attribute last_name.



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

def last_name
  @last_name
end

#paternal_name ⇒ Object

Returns the value of attribute paternal_name.



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

def paternal_name
  @paternal_name
end

#phone ⇒ Object

Returns the value of attribute phone.



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

def phone
  @phone
end

#telegram ⇒ Object

Returns the value of attribute telegram.



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

def telegram
  @telegram
end

Class Method Details

.into_hash(hash) ⇒ Object



39
40
41
# File 'lib/source/student/student.rb', line 39

def self.into_hash(hash)
  Student.new(**hash)
end

.pars_str(str) ⇒ Object

++

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/source/student/student.rb', line 44

def self.pars_str(str)
  args = JSON.parse(str)
  raise ArgumentError,"The argument must have last_name, first_name and paternal_name" unless
    (args.has_key?('last_name') and args.has_key?('first_name') and args.has_key?('paternal_name'))

  new(last_name: args['last_name'],
      first_name: args['first_name'],
      paternal_name: args['paternal_name'],
      id: args['id'],
      phone: args['phone'],
      git: args['git'],
      telegram: args['telegram'],
      email: args['email'])
end

.valid_account?(account) ⇒ Boolean

валидатор для git и telegram #++++++

Returns:

  • (Boolean)


20
21
22
# File 'lib/source/student/student.rb', line 20

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

.valid_email?(email) ⇒ Boolean

валидатор почты ++

Returns:

  • (Boolean)


25
26
27
# File 'lib/source/student/student.rb', line 25

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)


15
16
17
# File 'lib/source/student/student.rb', line 15

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

.valid_phone?(phone) ⇒ Boolean

валидатор телефона #+++++++++

Returns:

  • (Boolean)


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

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

Instance Method Details

#contact ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/source/student/student.rb', line 65

def contact
  if phone
    @contact = {type: :phone, value: phone}
  elsif telegram
    @contact = {type: :telegram, value: telegram}
  elsif email
    @contact = {type: :email, value: email}
  end
end

#getInfo ⇒ Object

Raises:

  • (ArgumentError)


75
76
77
78
79
80
81
# File 'lib/source/student/student.rb', line 75

def getInfo
  raise ArgumentError, "Required fields: last_name, first_name and paternal_name!" if last_name.nil?|| first_name.nil? || paternal_name.nil?
  info = "#{shortName}"
  info += " #{getContact}"  unless getContact.nil?
  info += " git= #{git}" unless git.nil?
  info
end

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



133
134
135
136
137
138
# File 'lib/source/student/student.rb', line 133

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

#shortName ⇒ Object

(last_name, first_name, paternal_name, id:nil, phone:nil, git:nil, telegram:nil, email:nil) ++



61
62
63
# File 'lib/source/student/student.rb', line 61

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

#to_hash ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/source/student/student.rb', line 83

def to_hash
  attrs = {}

  %i[first_name last_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_s ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/source/student/student.rb', line 141

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

#validate? ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/source/student/student.rb', line 129

def validate?
  !git.nil? && !email.nil? && !telegram.nil? && !phone.nil?
end