Module: Hcloud::EntryLoader
- Extended by:
- ActiveSupport::Concern
- Included in:
- Action, Certificate, Datacenter, Firewall, FloatingIP, Image, Iso, LoadBalancer, LoadBalancerType, Location, Network, Pagination, PlacementGroup, PrimaryIP, SSHKey, Server, ServerType, Volume
- Defined in:
- lib/hcloud/entry_loader.rb
Overview
rubocop:disable Metrics/ModuleLength
Defined Under Namespace
Modules: Collection
Instance Attribute Summary collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
187
188
189
|
# File 'lib/hcloud/entry_loader.rb', line 187
def method_missing(method, *args, &block)
_attributes.key?(method) ? _attributes[method] : super
end
|
Instance Attribute Details
#response ⇒ Object
Returns the value of attribute response.
144
145
146
|
# File 'lib/hcloud/entry_loader.rb', line 144
def response
@response
end
|
Instance Method Details
#_attributes ⇒ Object
183
184
185
|
# File 'lib/hcloud/entry_loader.rb', line 183
def _attributes
@_attributes ||= {}.with_indifferent_access
end
|
#_load(resource) ⇒ Object
226
227
228
229
230
231
232
233
234
235
236
237
|
# File 'lib/hcloud/entry_loader.rb', line 226
def _load(resource)
return if resource.nil?
@_attributes = {}.with_indifferent_access
loader = Hcloud::ResourceLoader.new(self.class.schema, client: client)
loaded_data = loader.load(resource)
loaded_data.each do |key, value|
_update_attribute(key, value.is_a?(Hash) ? value.with_indifferent_access : value)
end
end
|
#_run_callbacks(order) ⇒ Object
217
218
219
|
# File 'lib/hcloud/entry_loader.rb', line 217
def _run_callbacks(order)
self.class._callbacks[order].each { |block| instance_exec(&block) }
end
|
#_update_attribute(key, value) ⇒ Object
221
222
223
224
|
# File 'lib/hcloud/entry_loader.rb', line 221
def _update_attribute(key, value)
_attributes[key] = value
instance_variable_set("@#{key}", value)
end
|
#client ⇒ Object
155
156
157
|
# File 'lib/hcloud/entry_loader.rb', line 155
def client
@client || response&.context&.client
end
|
#initialize(client = nil, resource = {}) ⇒ Object
146
147
148
149
|
# File 'lib/hcloud/entry_loader.rb', line 146
def initialize(client = nil, resource = {})
@client = client
_load(resource)
end
|
#inspect ⇒ Object
151
152
153
|
# File 'lib/hcloud/entry_loader.rb', line 151
def inspect
"#<#{self.class.name}:0x#{__id__.to_s(16)} #{_attributes.inspect}>"
end
|
#prepare_request(url_suffix = nil, **kwargs, &block) ⇒ Object
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/hcloud/entry_loader.rb', line 171
def prepare_request(url_suffix = nil, **kwargs, &block)
kwargs[:resource_path] ||= resource_path
kwargs[:resource_class] ||= self.class
kwargs[:autoload_action] = true unless kwargs.key?(:autoload_action)
client.prepare_request(
[resource_url, url_suffix].compact.join('/'),
**kwargs,
&block
)
end
|
#resource_path ⇒ Object
167
168
169
|
# File 'lib/hcloud/entry_loader.rb', line 167
def resource_path
self.class.resource_class.name.demodulize.underscore
end
|
#resource_url ⇒ Object
159
160
161
162
163
164
165
|
# File 'lib/hcloud/entry_loader.rb', line 159
def resource_url
if block = self.class.resource_url
return instance_exec(&block)
end
[self.class.resource_class.name.demodulize.tableize, id].compact.join('/')
end
|
#respond_to_missing?(method, *args, &block) ⇒ Boolean
191
192
193
|
# File 'lib/hcloud/entry_loader.rb', line 191
def respond_to_missing?(method, *args, &block)
_attributes.key?(method) || super
end
|
#rollback ⇒ Object
213
214
215
|
# File 'lib/hcloud/entry_loader.rb', line 213
def rollback
restore_attributes
end
|
#save ⇒ Object
209
210
211
|
# File 'lib/hcloud/entry_loader.rb', line 209
def save
update(changes.map { |key, _value| [key.to_sym, _attributes[key]] }.to_h)
end
|
#update(**kwargs) ⇒ Object
195
196
197
198
199
200
201
202
203
204
205
206
207
|
# File 'lib/hcloud/entry_loader.rb', line 195
def update(**kwargs)
context = self
_run_callbacks(:before)
prepare_request(j: kwargs, method: :put) do |response|
response.resource_class.from_response(
response,
autoload_action: response.autoload_action
).tap do |*_args|
_run_callbacks(:after)
context.send(:changes_applied)
end
end
end
|