Class: Student

Inherits:
AbstractStudent show all
Defined in:
lib/model_gem_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.



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

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



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

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



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

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



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

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

#contacts_infoObject



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

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



102
103
104
# File 'lib/model_gem_source/student/student.rb', line 102

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

#get_infoObject



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

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

#have_any_contactObject



67
68
69
# File 'lib/model_gem_source/student/student.rb', line 67

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

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



77
78
79
80
81
82
# File 'lib/model_gem_source/student/student.rb', line 77

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



71
72
73
74
75
# File 'lib/model_gem_source/student/student.rb', line 71

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