Class: MicrosoftGraph::CollectionAssociation

Inherits:
Collection
  • Object
show all
Defined in:
lib/microsoft_graph/collection_association.rb

Instance Attribute Summary collapse

Attributes inherited from Collection

#graph

Instance Method Summary collapse

Methods inherited from Collection

#[]=, #as_json, #dirty?, #mark_clean, #new, #parental_chain

Constructor Details

#initialize(options = {}) ⇒ CollectionAssociation

Returns a new instance of CollectionAssociation


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/microsoft_graph/collection_association.rb', line 6

def initialize(options = {})
  @type            = options[:type]
  @graph           = options[:graph]
  @resource_name   = options[:resource_name]
  @parent          = options[:parent]
  @order_by        = options[:order_by]
  @filter          = options[:filter]
  @dirty           = false
  @loaded          = false
  @internal_values = []

  raise MicrosoftGraph::TypeError.new("A collection cannot be both ordered and filtered.") if @order_by && @filter
  @order_by && @order_by.each do |field|
    field_name, direction = field.to_s.split(' ')
    field_names = field_name.split("/")
    unless valid_nested_property(field_names, @type)
      raise MicrosoftGraph::TypeError.new("Tried to order by invalid field: #{field_name}")
    end
    if direction && ! %w(asc desc).include?(direction)
      raise MicrosoftGraph::TypeError.new("Tried to order field #{field_name} by invalid direction: #{direction}")
    end
  end
  @filter && @filter.respond_to?(:keys) && @filter.keys.each do |field_name|
    unless @type.properties.map(&:name).include? OData.convert_to_camel_case(field_name)
      raise MicrosoftGraph::TypeError.new("Tried to filter by invalid field: #{field_name}")
    end
  end

end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent


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

def parent
  @parent
end

#typeObject (readonly)

Returns the value of attribute type


3
4
5
# File 'lib/microsoft_graph/collection_association.rb', line 3

def type
  @type
end

Instance Method Details

#<<(new_member) ⇒ Object


152
153
154
155
156
157
158
# File 'lib/microsoft_graph/collection_association.rb', line 152

def <<(new_member)
  super
  new_member.graph = graph
  new_member.parent = self
  new_member.save
  self
end

#build(attributes = {}) ⇒ Object


139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/microsoft_graph/collection_association.rb', line 139

def build(attributes = {})
  ClassBuilder
    .get_namespaced_class(type.member_type.name)
    .new(
      graph:         graph,
      resource_name: @resource_name,
      parent:        self,
      attributes:    attributes,
    ).tap { |new_member|
      @internal_values << new_member
    }
end

#collection?Boolean


176
177
178
# File 'lib/microsoft_graph/collection_association.rb', line 176

def collection?
  true
end

#create(attributes = {}) ⇒ Object


131
132
133
# File 'lib/microsoft_graph/collection_association.rb', line 131

def create(attributes = {})
  build(attributes).tap { |n| n.save }
end

#create!(attributes = {}) ⇒ Object


135
136
137
# File 'lib/microsoft_graph/collection_association.rb', line 135

def create!(attributes = {})
  build(attributes).tap { |n| n.save! }
end

#each(start = 0) ⇒ Object


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/microsoft_graph/collection_association.rb', line 160

def each(start = 0)
  return to_enum(:each, start) unless block_given?
  @next_link ||= query_path
  Array(@internal_values[start..-1]).each do |element|
    yield(element)
  end

  unless last?
    start = [@internal_values.size, start].max

    fetch_next_page

    each(start, &Proc.new)
  end
end

#filter(new_filters) ⇒ Object


99
100
101
102
103
104
105
106
107
108
# File 'lib/microsoft_graph/collection_association.rb', line 99

def filter(new_filters)
  self.class.new(
    type: @type,
    graph: @graph,
    resource_name: @resource_name,
    parent: @parent,
    order_by: @order_by,
    filter: merge_with_existing_filter(new_filters)
  )
end

#find(id) ⇒ Object


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

def find(id)
  if response = graph.service.get("#{path}/#{URI.escape(id.to_s)}")
    klass = if member_type = specified_member_type(response)
      ClassBuilder.get_namespaced_class(response)
    else
      default_member_class
    end
    klass.new(
        graph: graph,
        parent: self,
        attributes: response[:attributes],
        persisted: true
      )
  else
    nil
  end
end

#merge_with_existing_filter(new_filters) ⇒ Object


110
111
112
113
114
115
116
117
# File 'lib/microsoft_graph/collection_association.rb', line 110

def merge_with_existing_filter(new_filters)
  existing_filter = @filter || {}
  if new_filters.respond_to?(:keys) && existing_filter.respond_to?(:keys)
    existing_filter.merge(new_filters)
  else
    [stringify_filters(new_filters), stringify_filters(@filter)].compact.join(" and ")
  end
end

#order_by(*fields) ⇒ Object


76
77
78
79
80
81
82
83
84
85
# File 'lib/microsoft_graph/collection_association.rb', line 76

def order_by(*fields)
  self.class.new(
    type: @type,
    graph: @graph,
    resource_name: @resource_name,
    parent: @parent,
    order_by: fields,
    filter: @filter
  )
end

#pathObject


54
55
56
57
58
59
60
# File 'lib/microsoft_graph/collection_association.rb', line 54

def path
  if parent && parent.path
    "#{parent.path}/#{@resource_name}"
  else
    @resource_name
  end
end

#query_pathObject


62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/microsoft_graph/collection_association.rb', line 62

def query_path
  if @order_by
    order_by_names = @order_by.map do |field|
      URI.escape OData.convert_to_camel_case(field)
    end
    "#{path}?$orderby=#{order_by_names.join(',')}"
  elsif @filter
    escaped_filters = URI.escape(stringify_filters(@filter))
    "#{path}?$filter=#{escaped_filters}"
  else
    path
  end
end

#reload!Object


180
181
182
183
184
# File 'lib/microsoft_graph/collection_association.rb', line 180

def reload!
  @next_link = query_path
  @internal_values = []
  fetch_next_page
end

#stringify_filters(filters) ⇒ Object


119
120
121
122
123
124
125
126
127
128
129
# File 'lib/microsoft_graph/collection_association.rb', line 119

def stringify_filters(filters)
  if filters.respond_to?(:keys)
    filter_strings = filters.map do |k,v|
      v = v.is_a?(String) ? "'#{v}'" : v
      "#{OData.convert_to_camel_case(k)} eq #{v}"
    end
    filter_strings.sort.join(' and ')
  else
    filters
  end
end

#valid_nested_property(field_names, parent_type) ⇒ Object


87
88
89
90
91
92
93
94
95
96
97
# File 'lib/microsoft_graph/collection_association.rb', line 87

def valid_nested_property(field_names, parent_type)
  field_name = field_names.shift
  if field_name
    property = parent_type.properties.find do |prop|
      OData.convert_to_camel_case(prop.name) == OData.convert_to_camel_case(field_name)
    end
    property && valid_nested_property(field_names, property.type)
  else
    true
  end
end