Class: ShnaiderCode::Student

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

Instance Attribute Summary

Attributes inherited from AbstractStudent

#firstname, #id, #lastname, #patronymic

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractStudent

#to_s

Constructor Details

#initialize(lastname:, firstname:, patronymic:, params: {}) ⇒ Student

Returns a new instance of Student.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/source/student/student.rb', line 13

def initialize(
    lastname:,
    firstname:,
    patronymic:,
    params: {}
)
    self.lastname = lastname
    self.firstname = firstname
    self.patronymic = patronymic
    self.id = params[:id]

    self.phone = params[:phone]
    self.telegram = params[:telegram]
    self.email = params[:email]
    self.git = params[:git]

    validate()
end

Class Method Details

.from_json(json) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/source/student/student.rb', line 32

def self.from_json(json)
    params = json.map { |v| 
        [v[0].to_sym, v[1]]
    }.filter { |v|
        v[1] != "NULL"
    }.to_h

    Student.new(
        lastname: json["lastname"],
        firstname: json["firstname"],
        patronymic: json["patronymic"],
        params: params
    )
end

.from_string(str) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/source/student/student.rb', line 47

def self.from_string(str)
    params = str
    .split(";")
    .map { |x| x.split(":") }
    .map { |x| [x[0].to_sym, x[1]] }
    .to_h

    if params[:fio] == nil
        raise "invalid string representation"
    end

    fio_components = params[:fio].split(" ")

    Student.new(
        lastname: fio_components[0],
        firstname: fio_components[1],
        patronymic: fio_components[2],
        params: params
    )
end

Instance Method Details

#as_jsonObject



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/source/student/student.rb', line 107

def as_json 
    {
        lastname: lastname,
        firstname: firstname,
        patronymic: patronymic,
        id: id,
        phone: phone,
        email: email,
        git: git,
        telegram: telegram
    }
end

#contacts_infoObject



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

def contacts_info
    contacts = ""
    if git != nil then contacts << "git:#{git};" end
    if phone != nil then contacts << "phone:#{phone};" end
    if email != nil then contacts << "email:#{email};" end
    if telegram != nil then contacts << "telegram:#{telegram};" end
    
    contacts
end

#fio_infoObject



103
104
105
# File 'lib/source/student/student.rb', line 103

def fio_info
    "fio:#{lastname} #{firstname} #{patronymic}"
end

#get_infoObject



95
96
97
98
99
100
101
# File 'lib/source/student/student.rb', line 95

def get_info
    if id != nil
        "id:#{id};#{fio_info};#{contacts_info}"
    else 
        "#{fio_info};#{contacts_info}"
    end
end

#have_any_contactObject



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

def have_any_contact
    phone != nil || telegram  != nil || email != nil || git != nil
end

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



78
79
80
81
82
83
# File 'lib/source/student/student.rb', line 78

def set_contacts(phone: nil, email: nil, git: nil, telegram: nil)
    if phone != nil then self.phone = phone end
    if email != nil then self.email = email end
    if git != nil then self.git = git end
    if telegram != nil then self.telegram = telegram end
end

#validateObject



72
73
74
75
76
# File 'lib/source/student/student.rb', line 72

def validate
    if !have_any_contact 
        raise "Not finded git or any contact"
    end
end