Class: MicrosoftGraph::ClassBuilder
- Inherits:
-
Object
- Object
- MicrosoftGraph::ClassBuilder
- Defined in:
- lib/microsoft_graph/class_builder.rb
Constant Summary collapse
- @@loaded =
false
Class Method Summary collapse
- .add_action_method!(action) ⇒ Object
- .add_function_method!(function) ⇒ Object
- .add_graph_association!(entity_set) ⇒ Object
- .classify(name) ⇒ Object
- .create_class!(type) ⇒ Object
- .create_navigation_properties(klass, type) ⇒ Object
- .create_properties(klass, type) ⇒ Object
- .define_getter_and_setter(klass, property) ⇒ Object
- .get_namespaced_class(property_name) ⇒ Object
- .get_superklass(type) ⇒ Object
- .load!(service) ⇒ Object
- .loaded? ⇒ Boolean
Class Method Details
.add_action_method!(action) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/microsoft_graph/class_builder.rb', line 174 def self.add_action_method!(action) klass = get_namespaced_class(action.binding_type.name) klass.class_eval do define_method(OData.convert_to_snake_case(action.name).to_sym) do |args={}| raise NoAssociationError unless parent raise_no_graph_error! unless graph response = graph.service.post("#{path}/#{action.name}", OData.convert_keys_to_camel_case(args).to_json) if action.return_type if action.return_type.collection? Collection.new(action.return_type, response['value']) else ClassBuilder.get_namespaced_class(action.return_type.name).new(attributes: response['value']) end end end end end |
.add_function_method!(function) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/microsoft_graph/class_builder.rb', line 153 def self.add_function_method!(function) klass = get_namespaced_class(function.binding_type.name) klass.class_eval do define_method(OData.convert_to_snake_case(function.name).to_sym) do |params={}| raise NoAssociationError unless parent raise_no_graph_error! unless graph function_params = params.map do |param_key, param_value| "#{OData.convert_to_camel_case(param_key)}='#{param_value}'" end response = graph.service.get("#{path}/microsoft.graph.#{function.name}(#{function_params.join(',')})") if function.return_type if function.return_type.collection? Collection.new(function.return_type, response[:attributes]['value']) else ClassBuilder.get_namespaced_class(function.return_type.name).new(attributes: response[:attributes]['value']) end end end end end |
.add_graph_association!(entity_set) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/microsoft_graph/class_builder.rb', line 81 def self.add_graph_association!(entity_set) klass = get_namespaced_class(entity_set.member_type) resource_name = entity_set.name.gsub("#{@service_namespace}.", "") odata_collection = OData::CollectionType.new(member_type: klass.odata_type, name: entity_set.name) MicrosoftGraph.send(:define_method, resource_name) do @association_collections[entity_set.name] ||= MicrosoftGraph::CollectionAssociation .new( type: odata_collection, resource_name: resource_name, parent: self ) .tap do |collection| collection.graph = self end end end |
.classify(name) ⇒ Object
192 193 194 195 |
# File 'lib/microsoft_graph/class_builder.rb', line 192 def self.classify(name) raw_name = name.gsub("#{@service_namespace}.", "") raw_name.to_s.slice(0, 1).capitalize + raw_name.to_s.slice(1..-1) end |
.create_class!(type) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/microsoft_graph/class_builder.rb', line 68 def self.create_class!(type) superklass = get_superklass(type) klass = MicrosoftGraph.const_set(classify(type.name), Class.new(superklass)) klass.const_set("ODATA_TYPE", type) klass.instance_eval do def self.odata_type const_get("ODATA_TYPE") end end create_properties(klass, type) (klass, type) if type.respond_to? :navigation_properties end |
.create_navigation_properties(klass, type) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/microsoft_graph/class_builder.rb', line 128 def self.(klass, type) klass.class_eval do type..each do || = OData.convert_to_snake_case(.name).to_sym define_method(.to_sym) do (.to_sym) end unless .collection? define_method("#{}=".to_sym) do |value| (.to_sym, value) end end end define_method(:navigation_properties) do type..map { || [ OData.convert_to_snake_case(.name).to_sym, ] }.to_h end end end |
.create_properties(klass, type) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/microsoft_graph/class_builder.rb', line 100 def self.create_properties(klass, type) property_map = type.properties.map { |property| define_getter_and_setter(klass, property) [ OData.convert_to_snake_case(property.name).to_sym, property ] }.to_h klass.class_eval do define_method(:properties) do super().merge(property_map) end end end |
.define_getter_and_setter(klass, property) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/microsoft_graph/class_builder.rb', line 116 def self.define_getter_and_setter(klass, property) klass.class_eval do property_name = OData.convert_to_snake_case(property.name) define_method(property_name.to_sym) do get(property_name.to_sym) end define_method("#{property_name}=".to_sym) do |value| set(property_name.to_sym, value) end end end |
.get_namespaced_class(property_name) ⇒ Object
197 198 199 200 201 202 203 204 205 |
# File 'lib/microsoft_graph/class_builder.rb', line 197 def self.get_namespaced_class(property_name) klass_name = classify(property_name) klass = begin MicrosoftGraph.const_get(klass_name) if MicrosoftGraph.const_defined?(klass_name) rescue NameError return false end klass && MicrosoftGraph::BaseEntity != klass.superclass && klass end |
.get_superklass(type) ⇒ Object
207 208 209 210 211 212 213 214 215 |
# File 'lib/microsoft_graph/class_builder.rb', line 207 def self.get_superklass(type) if type.base_type.nil? (type.class == OData::ComplexType) ? MicrosoftGraph::Base : MicrosoftGraph::BaseEntity else Object.const_get("MicrosoftGraph::" + classify(type.base_type)) end end |
.load!(service) ⇒ Object
5 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/microsoft_graph/class_builder.rb', line 5 def self.load!(service) if !@@loaded @service_namespace = service.namespace service.entity_types.each do |entity_type| create_class! entity_type end service.complex_types.each do |complex_type| create_class! complex_type end service.entity_sets.each do |entity_set| add_graph_association! entity_set end service.actions.each do |action| add_action_method! action end service.functions.each do |function| add_function_method! function end service.singletons.each do |singleton| class_name = classify(singleton.type_name) MicrosoftGraph.instance_eval do resource_name = singleton.name define_method(OData.convert_to_snake_case(resource_name)) do MicrosoftGraph .const_get(class_name) .new( graph: self, resource_name: resource_name, parent: self ).tap(&:fetch) end end end MicrosoftGraph.instance_eval do define_method(:navigation_properties) do service.entity_sets .concat(service.singletons) .map { || [.name.to_sym, ] }.to_h end end @@loaded = true end end |
.loaded? ⇒ Boolean
58 59 60 |
# File 'lib/microsoft_graph/class_builder.rb', line 58 def self.loaded? @@loaded end |