Class: TimeTree::BaseModel

Inherits:
Object
  • Object
show all
Defined in:
lib/timetree/models/base_model.rb

Overview

TimeTree base model object.

Direct Known Subclasses

Activity, Application, Calendar, Event, Label, User

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

rubocop:disable Metrics/ParameterLists



59
60
61
62
63
64
65
# File 'lib/timetree/models/base_model.rb', line 59

def initialize(type:, id: nil, client: nil, attributes: nil, relationships: nil, included: nil) # rubocop:disable Metrics/ParameterLists
  @type = type
  @id = id
  @client = client
  set_attributes attributes
  set_relationships relationships, included
end

Instance Attribute Details

#idString (readonly)

Returns:

  • (String)


11
12
13
# File 'lib/timetree/models/base_model.rb', line 11

def id
  @id
end

#relationshipsArray<Hash<String,String>>

Returns:

  • (Array<Hash<String,String>>)


9
10
11
# File 'lib/timetree/models/base_model.rb', line 9

def relationships
  @relationships
end

#typeString (readonly)

Returns:

  • (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.

Parameters:

Returns:

Raises:

Since:

  • 0.0.1



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
54
55
56
57
# File 'lib/timetree/models/base_model.rb', line 23

def self.to_model(data, included: nil, client: nil) # rubocop:disable all
  id = data[:id]
  type = data[:type]
  raise Error.new('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 'activity'
    Activity.new(**params)
  when 'application'
    Application.new(**params)
  when 'calendar'
    Calendar.new(**params)
  when 'event'
    Event.new(**params)
  when 'label'
    Label.new(**params)
  when 'user'
    User.new(**params)
  else
    TimeTree.configuration.logger.warn("type '#{type}' is unknown. id:#{id}")
    # when unexpected model type, return the 'data' argument.
    data
  end
end

Instance Method Details

#inspectObject



67
68
69
# File 'lib/timetree/models/base_model.rb', line 67

def inspect
  "\#<#{self.class}:#{object_id} id:#{id}>"
end