Class: Tester::Employee

Inherits:
Person show all
Defined in:
lib/tester/models/employee.rb

Overview

Employee Model.

Direct Known Subclasses

Boss

Instance Attribute Summary collapse

Attributes inherited from Person

#address, #age, #birthday, #birthtime, #name, #person_type, #uid

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseModel

#method_missing, #respond_to_missing?, #to_hash, #to_json

Constructor Details

#initialize(address = nil, age = nil, birthday = nil, birthtime = nil, department = nil, dependents = nil, hired_at = nil, joining_day = Days::MONDAY, name = nil, salary = nil, uid = nil, working_days = nil, boss = nil, person_type = nil, additional_properties = {}) ⇒ Employee

Returns a new instance of Employee.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/tester/models/employee.rb', line 61

def initialize(address = nil,
               age = nil,
               birthday = nil,
               birthtime = nil,
               department = nil,
               dependents = nil,
               hired_at = nil,
               joining_day = Days::MONDAY,
               name = nil,
               salary = nil,
               uid = nil,
               working_days = nil,
               boss = nil,
               person_type = nil,
               additional_properties = {})
  @department = department
  @dependents = dependents
  @hired_at = hired_at
  @joining_day = joining_day
  @salary = salary
  @working_days = working_days
  @boss = boss
  def hired_at.to_s
    httpdate
  end

  # Add additional model properties to the instance.
  additional_properties.each do |_name, value|
    instance_variable_set("@#{_name}", value)
  end

  # Call the constructor of the base class
  super(address,
        age,
        birthday,
        birthtime,
        name,
        uid,
        person_type)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Tester::BaseModel

Instance Attribute Details

#bossPerson

TODO: Write general description for this method

Returns:



36
37
38
# File 'lib/tester/models/employee.rb', line 36

def boss
  @boss
end

#departmentString

TODO: Write general description for this method

Returns:



12
13
14
# File 'lib/tester/models/employee.rb', line 12

def department
  @department
end

#dependentsList of Person

TODO: Write general description for this method

Returns:



16
17
18
# File 'lib/tester/models/employee.rb', line 16

def dependents
  @dependents
end

#hired_atDateTime

TODO: Write general description for this method

Returns:

  • (DateTime)


20
21
22
# File 'lib/tester/models/employee.rb', line 20

def hired_at
  @hired_at
end

#joining_dayDays

TODO: Write general description for this method

Returns:



24
25
26
# File 'lib/tester/models/employee.rb', line 24

def joining_day
  @joining_day
end

#salaryInteger

TODO: Write general description for this method

Returns:

  • (Integer)


28
29
30
# File 'lib/tester/models/employee.rb', line 28

def salary
  @salary
end

#working_daysList of Days

TODO: Write general description for this method

Returns:



32
33
34
# File 'lib/tester/models/employee.rb', line 32

def working_days
  @working_days
end

Class Method Details

.discriminatorsObject

Discriminators mapping.



39
40
41
42
43
44
45
# File 'lib/tester/models/employee.rb', line 39

def self.discriminators
  if @_discriminators.nil?
    @_discriminators = {}
    @_discriminators['Boss'] = Boss.method(:from_hash)
  end
  @_discriminators
end

.from_hash(hash) ⇒ Object

Creates an instance of the object from a hash.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/tester/models/employee.rb', line 103

def self.from_hash(hash)
  return nil unless hash

  # Delegate unboxing to another function if a discriminator
  # value for a child class is present.
  unboxer = discriminators[hash['personType']]
  return unboxer.call(hash) if unboxer

  # Extract variables from the hash.
  address = hash['address']
  age = hash['age']
  birthday = hash['birthday']
  birthtime = DateTime.rfc3339(hash['birthtime']) if hash['birthtime']
  department = hash['department']
  # Parameter is an array, so we need to iterate through it
  dependents = nil
  unless hash['dependents'].nil?
    dependents = []
    hash['dependents'].each do |structure|
      dependents << (Person.from_hash(structure) if structure)
    end
  end
  hired_at = DateTime.httpdate(hash['hiredAt']) if hash['hiredAt']
  joining_day = hash['joiningDay'] ||= Days::MONDAY
  name = hash['name']
  salary = hash['salary']
  uid = hash['uid']
  working_days = hash['workingDays']
  boss = Person.from_hash(hash['boss']) if hash['boss']
  person_type = hash['personType']

  # Clean out expected properties from Hash.
  names.each_value { |k| hash.delete(k) }

  # Create object from extracted values.
  Employee.new(address,
               age,
               birthday,
               birthtime,
               department,
               dependents,
               hired_at,
               joining_day,
               name,
               salary,
               uid,
               working_days,
               boss,
               person_type,
               hash)
end

.namesObject

A mapping from model property names to API property names.



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tester/models/employee.rb', line 48

def self.names
  @_hash = {} if @_hash.nil?
  @_hash['department'] = 'department'
  @_hash['dependents'] = 'dependents'
  @_hash['hired_at'] = 'hiredAt'
  @_hash['joining_day'] = 'joiningDay'
  @_hash['salary'] = 'salary'
  @_hash['working_days'] = 'workingDays'
  @_hash['boss'] = 'boss'
  @_hash = super().merge(@_hash)
  @_hash
end