Class: Student

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from StudentBase

#first_name=, #last_name=, #parental_name=, #valid_cont?, #validate?, validate_email?, validate_git_tg?, validate_name?, validate_phone?

Constructor Details

#initialize(last_name, first_name, parental_name, options = {}) ⇒ Student

Returns a new instance of Student.



10
11
12
13
14
15
16
17
# File 'lib/source/Student.rb', line 10

def initialize(last_name, first_name, parental_name, options = {})
	super(last_name, first_name, parental_name)
	self.id = options[:id]
	self.phone = options[:phone]
	self.git = options[:git]
	self.telegram = options[:telegram]
	self.email = options[:email]
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



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

def email
  @email
end

#first_nameObject (readonly)

Returns the value of attribute first_name.



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

def first_name
  @first_name
end

#gitObject

Returns the value of attribute git.



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

def git
  @git
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#last_nameObject (readonly)

Returns the value of attribute last_name.



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

def last_name
  @last_name
end

#parental_nameObject (readonly)

Returns the value of attribute parental_name.



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

def parental_name
  @parental_name
end

#phoneObject

Returns the value of attribute phone.



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

def phone
  @phone
end

#telegramObject

Returns the value of attribute telegram.



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

def telegram
  @telegram
end

Class Method Details

.from_hash(hash) ⇒ Object

Raises:

  • (ArgumentError)


66
67
68
69
70
# File 'lib/source/Student.rb', line 66

def self.from_hash(hash)
	raise ArgumentError,"Missing req fields" unless hash.key?(:last_name) && hash.key?(:first_name) && hash.key?(:parental_name)
	#переделал этот метод после тестов. Теперь вынимаю обязательные параметры из хэша, не удаляя из него
	Student.new(hash[:last_name], hash[:first_name], hash[:parental_name], hash)
end

.from_str(string) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/source/Student.rb', line 57

def self.from_str(string)
	stud = string.split(',')
			.map{|v| v.split(':')}
			.map{|v| [v[0].to_sym, v[1]]}
			.to_h
	last_name, first_name, parental_name = stud[:fio].split(' ')
	Student.new(last_name, first_name, parental_name, stud)
end

Instance Method Details

#get_gitObject



82
83
84
# File 'lib/source/Student.rb', line 82

def get_git
	"git:#{git}"
end

#get_infoObject



86
87
88
# File 'lib/source/Student.rb', line 86

def get_info
	"#{get_short_fio},#{get_short_contact[:type]}:#{get_short_contact[:val]},#{get_git}"
end

#get_info_hashObject



90
91
92
93
94
95
96
# File 'lib/source/Student.rb', line 90

def get_info_hash
	info={}
	info[:fio] = get_short_fio
	info[:contact] = get_short_contact
	info[:git] = git
	info
end

#get_short_contactObject



77
78
79
80
# File 'lib/source/Student.rb', line 77

def get_short_contact
	contact = %i[telegram phone email].find{|cont| send(cont)}
	{type: contact, val: send(contact)} if contact
end

#get_short_fioObject



72
73
74
# File 'lib/source/Student.rb', line 72

def get_short_fio
	"fio:#{last_name} #{first_name[0]}. #{parental_name[0]}."
end

#set_contacts(contacts = {}) ⇒ Object



51
52
53
54
55
# File 'lib/source/Student.rb', line 51

def set_contacts(contacts={})
	self.phone=contacts[:phone]
	self.telegram=contacts[:telegram]
	self.email=contacts[:email]
end

#to_hashObject



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

def to_hash
	fields = {}
	%i[last_name first_name parental_name id phone git telegram email].each do |field|
		field_val = send(field)
		fields[field] = field_val unless field_val.nil?
	end
	fields
end

#to_sObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/source/Student.rb', line 19

def to_s
	[
		"#{last_name} #{first_name} #{parental_name}",
		id ? "id: #{id}" : nil,
		git ? "git: #{git}" : nil,
		phone ? "phone: #{phone}" : nil,
		telegram ? "telegram: #{telegram}" : nil,
		email ? "email: #{email}" : nil
	].compact.join(' ')
end