Class: TimeTree::BaseModel
- Inherits:
-
Object
- Object
- TimeTree::BaseModel
show all
- Defined in:
- lib/timetree/models/base_model.rb
Overview
TimeTree base model object.
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(type:, id: nil, client: nil, attributes: nil, relationships: nil, included: nil) ⇒ BaseModel
Returns a new instance of BaseModel.
55
56
57
58
59
60
61
|
# File 'lib/timetree/models/base_model.rb', line 55
def initialize(type:, id: nil, client: nil, attributes: nil, relationships: nil, included: nil)
@type = type
@id = id
@client = client
set_attributes attributes
set_relationships relationships, included
end
|
Instance Attribute Details
#id ⇒ String
11
12
13
|
# File 'lib/timetree/models/base_model.rb', line 11
def id
@id
end
|
#relationships ⇒ Array<Hash<String,String>>
9
10
11
|
# File 'lib/timetree/models/base_model.rb', line 9
def relationships
@relationships
end
|
#type ⇒ String
13
14
15
|
# File 'lib/timetree/models/base_model.rb', line 13
def type
@type
end
|
Class Method Details
.to_model(data, included: nil, client: nil) ⇒ TimeTree::User, ...
TimeTree apis’s response data. A TimeTree model object that be based on the type.
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/timetree/models/base_model.rb', line 23
def self.to_model(data, included: nil, client: nil)
id = data[:id]
type = data[:type]
raise Error, 'type is required.' if type.nil?
attributes = data[:attributes] || {}
relationships = data[:relationships] || {}
params = {
id: id,
type: type,
client: client,
attributes: attributes,
relationships: relationships,
included: included
}
case type
when 'user'
User.new(**params)
when 'label'
Label.new(**params)
when 'calendar'
Calendar.new(**params)
when 'event'
Event.new(**params)
when 'activity'
Activity.new(**params)
else
raise Error, "type '#{type}' is unknown."
end
end
|
Instance Method Details
#inspect ⇒ Object
63
64
65
|
# File 'lib/timetree/models/base_model.rb', line 63
def inspect
"\#<#{self.class}:#{object_id} id:#{id}>"
end
|