Class: MicrosoftGraph::BaseEntity

Inherits:
Base
  • Object
show all
Defined in:
lib/microsoft_graph/base_entity.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#as_json, #dirty?, #mark_clean, #odata_type, #properties, #to_json

Constructor Details

#initialize(options = {}) ⇒ BaseEntity


7
8
9
10
11
12
13
14
# File 'lib/microsoft_graph/base_entity.rb', line 7

def initialize(options = {})
  @resource_name            = options[:resource_name]
  @parent                   = options[:parent] || options[:graph]
  @graph                    = options[:graph] || parent && parent.graph
  @navigation_property_name = options[:navigation_property_name]
  @persisted                = options[:persisted] || false
  super
end

Instance Attribute Details

#graphObject

Returns the value of attribute graph


4
5
6
# File 'lib/microsoft_graph/base_entity.rb', line 4

def graph
  @graph
end

#parentObject

Returns the value of attribute parent


5
6
7
# File 'lib/microsoft_graph/base_entity.rb', line 5

def parent
  @parent
end

Instance Method Details

#containing_navigation_property(type_name) ⇒ Object


24
25
26
27
28
29
30
31
32
33
# File 'lib/microsoft_graph/base_entity.rb', line 24

def containing_navigation_property(type_name)
  candidate_navigation_properties = navigation_properties.values.select do |navigation_property|
    navigation_property.collection? && navigation_property.type.name == "Collection(#{type_name})"
  end
  candidate_navigation_properties.sort { |a, b|
    a_index = type_name.downcase.index(a.name[0..-2].downcase) || 0
    b_index = type_name.downcase.index(b.name[0..-2].downcase) || 0
    a_index <=> b_index
  }.last
end

#delete!Object


60
61
62
63
64
65
# File 'lib/microsoft_graph/base_entity.rb', line 60

def delete!
  if persisted?
    @persisted = false
    graph.service.delete(path)
  end
end

#fetchObject


51
52
53
54
# File 'lib/microsoft_graph/base_entity.rb', line 51

def fetch
  @persisted = true
  initialize_serialized_properties(graph.service.get(path)[:attributes])
end

#parental_chainObject


16
17
18
19
20
21
22
# File 'lib/microsoft_graph/base_entity.rb', line 16

def parental_chain
  if parent && parent.respond_to?(:parental_chain)
    parent.parental_chain.concat([parent])
  else
    [parent]
  end
end

#pathObject


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/microsoft_graph/base_entity.rb', line 35

def path
  containing_navigation_property_name = nil
  owning_ancestor = parental_chain.find do |ancestor|
    unless MicrosoftGraph::CollectionAssociation === ancestor
      containing_navigation_property = ancestor.containing_navigation_property(odata_type)
      containing_navigation_property && containing_navigation_property_name = containing_navigation_property.name
    end
  end

  if owning_ancestor && @cached_property_values[:id]
    [owning_ancestor.path, containing_navigation_property_name, @cached_property_values[:id]].compact.join("/")
  else
    @resource_name
  end
end

#persisted?Boolean


56
57
58
# File 'lib/microsoft_graph/base_entity.rb', line 56

def persisted?
  @persisted
end

#reload!Object


67
68
69
70
71
72
73
# File 'lib/microsoft_graph/base_entity.rb', line 67

def reload!
  @dirty_properties.keys.each do |dirty_property|
    @cached_property_values[dirty_property] = nil
  end
  mark_clean
  fetch if persisted?
end

#saveObject


90
91
92
93
94
# File 'lib/microsoft_graph/base_entity.rb', line 90

def save
  save!
rescue OData::HTTPError
  false
end

#save!Object

Raises:


75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/microsoft_graph/base_entity.rb', line 75

def save!
  raise NoAssociationError unless parent
  raise_no_graph_error! unless graph
  if persisted?
    graph.service.patch(path, to_json(only: @dirty_properties.keys, convert_to_camel_case: true))
  else
    initialize_serialized_properties(
      graph.service.post(parent.path, to_json(convert_to_camel_case: true))
    )
    @persisted = true
  end
  mark_clean
  true
end