Module: Hcloud::EntryLoader
- Extended by:
- ActiveSupport::Concern
- Included in:
- Action, Datacenter, FloatingIP, Image, Iso, Location, Network, Pagination, 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
143
144
145
|
# File 'lib/hcloud/entry_loader.rb', line 143
def method_missing(method, *args, &block)
_attributes.key?(method) ? _attributes[method] : super
end
|
Instance Attribute Details
#response ⇒ Object
Returns the value of attribute response.
95
96
97
|
# File 'lib/hcloud/entry_loader.rb', line 95
def response
@response
end
|
Instance Method Details
#_attributes ⇒ Object
139
140
141
|
# File 'lib/hcloud/entry_loader.rb', line 139
def _attributes
@_attributes ||= {}.with_indifferent_access
end
|
#_load(resource) ⇒ Object
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/hcloud/entry_loader.rb', line 182
def _load(resource)
@_attributes = {}.with_indifferent_access
resource.each do |key, value|
definition = self.class.schema[key]
if definition == :time
_update_attribute(key, value ? Time.parse(value) : nil)
next
end
if definition.is_a?(Class) && definition.include?(EntryLoader)
_update_attribute(key, value ? definition.new(client, value) : nil)
next
end
_update_attribute(key, value.is_a?(Hash) ? value.with_indifferent_access : value)
end
end
|
#_run_callbacks(order) ⇒ Object
173
174
175
|
# File 'lib/hcloud/entry_loader.rb', line 173
def _run_callbacks(order)
self.class._callbacks[order].each { |block| instance_exec(&block) }
end
|
#_update_attribute(key, value) ⇒ Object
177
178
179
180
|
# File 'lib/hcloud/entry_loader.rb', line 177
def _update_attribute(key, value)
_attributes[key] = value
instance_variable_set("@#{key}", value)
end
|
#client ⇒ Object
111
112
113
|
# File 'lib/hcloud/entry_loader.rb', line 111
def client
@client || response&.context&.client
end
|
#initialize(client = nil, kwargs = {}) ⇒ Object
97
98
99
100
101
102
103
104
105
|
# File 'lib/hcloud/entry_loader.rb', line 97
def initialize(client = nil, kwargs = {})
if client.is_a?(Hash)
kwargs = client
client = nil
end
@client = client
_load(kwargs)
end
|
#inspect ⇒ Object
107
108
109
|
# File 'lib/hcloud/entry_loader.rb', line 107
def inspect
"#<#{self.class.name}:0x#{__id__.to_s(16)} #{_attributes.inspect}>"
end
|
#prepare_request(url_suffix = nil, **kwargs, &block) ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/hcloud/entry_loader.rb', line 127
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
123
124
125
|
# File 'lib/hcloud/entry_loader.rb', line 123
def resource_path
self.class.resource_class.name.demodulize.underscore
end
|
#resource_url ⇒ Object
115
116
117
118
119
120
121
|
# File 'lib/hcloud/entry_loader.rb', line 115
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
147
148
149
|
# File 'lib/hcloud/entry_loader.rb', line 147
def respond_to_missing?(method, *args, &block)
_attributes.key?(method) || super
end
|
#rollback ⇒ Object
169
170
171
|
# File 'lib/hcloud/entry_loader.rb', line 169
def rollback
restore_attributes
end
|
#save ⇒ Object
165
166
167
|
# File 'lib/hcloud/entry_loader.rb', line 165
def save
update(changes.map { |key, _value| [key.to_sym, _attributes[key]] }.to_h)
end
|
#update(**kwargs) ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/hcloud/entry_loader.rb', line 151
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
|