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



138
139
140
# File 'lib/hcloud/entry_loader.rb', line 138

def method_missing(method, *args, &block)
  _attributes.key?(method) ? _attributes[method] : super
end

Instance Attribute Details

#responseObject

Returns the value of attribute response.



95
96
97
# File 'lib/hcloud/entry_loader.rb', line 95

def response
  @response
end

Instance Method Details

#_attributesObject



134
135
136
# File 'lib/hcloud/entry_loader.rb', line 134

def _attributes
  @_attributes ||= {}.with_indifferent_access
end

#_load(resource) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/hcloud/entry_loader.rb', line 177

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



168
169
170
# File 'lib/hcloud/entry_loader.rb', line 168

def _run_callbacks(order)
  self.class._callbacks[order].each { |block| instance_exec(&block) }
end

#_update_attribute(key, value) ⇒ Object



172
173
174
175
# File 'lib/hcloud/entry_loader.rb', line 172

def _update_attribute(key, value)
  _attributes[key] = value
  instance_variable_set("@#{key}", value)
end

#clientObject



106
107
108
# File 'lib/hcloud/entry_loader.rb', line 106

def client
  @client || response&.context&.client
end

#initialize(client = nil, **kwargs) ⇒ Object



97
98
99
100
# File 'lib/hcloud/entry_loader.rb', line 97

def initialize(client = nil, **kwargs)
  @client = client
  _load(kwargs)
end

#inspectObject



102
103
104
# File 'lib/hcloud/entry_loader.rb', line 102

def inspect
  "#<#{self.class.name}:0x#{__id__.to_s(16)} #{_attributes.inspect}>"
end

#prepare_request(url_suffix = nil, **kwargs, &block) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/hcloud/entry_loader.rb', line 122

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_pathObject



118
119
120
# File 'lib/hcloud/entry_loader.rb', line 118

def resource_path
  self.class.resource_class.name.demodulize.underscore
end

#resource_urlObject



110
111
112
113
114
115
116
# File 'lib/hcloud/entry_loader.rb', line 110

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

Returns:

  • (Boolean)


142
143
144
# File 'lib/hcloud/entry_loader.rb', line 142

def respond_to_missing?(method, *args, &block)
  _attributes.key?(method) || super
end

#rollbackObject



164
165
166
# File 'lib/hcloud/entry_loader.rb', line 164

def rollback
  restore_attributes
end

#saveObject



160
161
162
# File 'lib/hcloud/entry_loader.rb', line 160

def save
  update(changes.map { |key, _value| [key.to_sym, _attributes[key]] }.to_h)
end

#update(**kwargs) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/hcloud/entry_loader.rb', line 146

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