Class: Student

Inherits:
Person show all
Includes:
Comparable
Defined in:
lib/models/student/student.rb

Instance Attribute Summary collapse

Attributes inherited from Person

#git, #id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Person

#validate_git?

Constructor Details

#initialize(first_name:, name:, patronymic:, birthdate:, id: nil, telegram: nil, phone_number: nil, email: nil, git: nil, key_type: :birthdate) ⇒ Student

constructor



11
12
13
14
15
16
17
18
19
20
# File 'lib/models/student/student.rb', line 11

def initialize(first_name:, name:, patronymic:, birthdate:, id: nil, telegram: nil, phone_number: nil, email: nil, git: nil, key_type: :birthdate)
    self.id = id
    self.first_name = first_name
    self.name = name
    self.patronymic = patronymic
    self.git = git
    self.set_contacts(email: email, telegram: telegram, phone_number: phone_number)
    self.birthdate = birthdate
    self.key_type = key_type
end

Instance Attribute Details

#birthdateObject

Returns the value of attribute birthdate.



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

def birthdate
  @birthdate
end

#emailObject (readonly)

Returns the value of attribute email.



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

def email
  @email
end

#first_nameObject

Returns the value of attribute first_name.



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

def first_name
  @first_name
end

#id=(value) ⇒ Object (writeonly)

Sets the attribute id

Parameters:

  • value

    the value to set the attribute id to.



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

def id=(value)
  @id = value
end

#key_typeObject

Returns the value of attribute key_type.



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

def key_type
  @key_type
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#patronymicObject

Returns the value of attribute patronymic.



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

def patronymic
  @patronymic
end

#phone_numberObject (readonly)

Returns the value of attribute phone_number.



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

def phone_number
  @phone_number
end

#telegramObject (readonly)

Returns the value of attribute telegram.



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

def telegram
  @telegram
end

Class Method Details

.new_from_hash(hash) ⇒ Object

constructor_from_hash



40
41
42
# File 'lib/models/student/student.rb', line 40

def self.new_from_hash(hash)
    self.new(**hash.transform_keys(&:to_sym))
end

.new_from_string(string) ⇒ Object

constructor_from_string



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/models/student/student.rb', line 23

def self.new_from_string(string)
    hash = self.parse_string(string)

    self.new(
        id: hash['id'].to_i,
        first_name: hash['first_name'],
        name: hash['name'],
        patronymic: hash['patronymic'],
        telegram: hash['telegram'],
        email: hash['email'],
        phone_number: hash['phone_number'],
        git: hash['git'],
        birthdate: hash['birthdate']
    )
end

.valid_birthdate?(birthdate) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
149
150
151
152
153
154
# File 'lib/models/student/student.rb', line 146

def self.valid_birthdate?(birthdate)
    valid = (birthdate =~ /^\d{2}\.\d{2}\.\d{4}$/)
    begin
        Date.parse(birthdate)
    rescue
        false
    end
    valid
end

Instance Method Details

#<=>(other) ⇒ Object



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

def <=>(other)
    if self.key.nil? && other.key.nil?
        0
    elsif self.key.nil?
        1
    elsif other.key.nil?
        -1
    else
        self.key <=> other.key
    end
end

#get_any_contactObject

get any contact in string



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/models/student/student.rb', line 66

def get_any_contact
    if telegram then
        "telegram: #{self.telegram}"
    elsif email
        "email: #{self.email}"
    elsif phone_number
        "phone_number: #{self.phone_number}"
    else
        "no contact provided"
    end
end

#get_full_nameObject

get full name in string



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

def get_full_name
    "full_name: #{self.first_name} #{self.name[0]}.#{self.patronymic[0]}."
end

#get_infoObject

get short info in string



56
57
58
# File 'lib/models/student/student.rb', line 56

def get_info
    "#{get_full_name}, git: #{self.git}, #{get_any_contact}"
end

#keyObject

key for binary tree



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/models/student/student.rb', line 111

def key
    case self.key_type
    when :birthdate
        self.birthdate
    when :id
        self.id
    when :full_name
        self.get_full_name
    when :contacts
        self.get_any_contact
    when :git
        self.git
    when :email
        self.email
    when :phone_number
        self.phone_number
    when :telegram
        self.telegram
    else
        raise ArgumentError, "Unknown key_type"
    end
end

#to_hObject

to hash



50
51
52
53
# File 'lib/models/student/student.rb', line 50

def to_h
    { id: self.id, first_name: self.first_name, name: self.name, patronymic: self.patronymic, 
    birthdate: self.birthdate, telegram: self.telegram, email: self.email, phone_number: self.phone_number, git: self.git }
end

#to_line_sObject

to string



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/models/student/student.rb', line 79

def to_line_s
    data = []
    data << "first_name: #{self.first_name}"
    data << "name: #{self.name}"
    data << "patronymic: #{self.patronymic}"
    data << "git: #{self.git}"
    if self.id then
        data << "id: #{self.id}"
    end
    if self.telegram then
        data << "telegram: #{self.telegram}"
    end
    if self.email then
        data << "email: #{self.email}"
    end
    if self.phone_number then
        data << "phone_number: #{phone_number}"
    end
    data.join(', ')
end

#to_sObject

to string



45
46
47
# File 'lib/models/student/student.rb', line 45

def to_s 
    "#{"-------------------\nID: #{self.id}\n" unless self.id.nil?}First Name: #{ self.first_name }\nName: #{ self.name }\nPatronymic: #{ self.patronymic }\nBithdate: #{ self.birthdate }\n#{"Phone Number: #{ self.phone_number }\n" unless self.phone_number.nil?}#{"Telegram: #{ self.phone_number }\n" unless self.telegram}#{"Email: #{ self.email }\n" unless self.email.nil?}#{"Git: #{ self.git }\n" unless self.git.nil?}-------------------"
end

#validate?Boolean

validate git and contacts

Returns:

  • (Boolean)


106
107
108
# File 'lib/models/student/student.rb', line 106

def validate?
    super && self.validate_contacts?
end

#validate_contacts?Boolean

checking for contacts availability

Returns:

  • (Boolean)


101
102
103
# File 'lib/models/student/student.rb', line 101

def validate_contacts?
    !self.telegram.nil? || !self.email.nil? || !self.phone_number.nil?
end