Module: NetboxClientRuby::Entity
- Includes:
- Communication
- Included in:
- DCIM::Device, DCIM::DeviceRole, DCIM::DeviceType, DCIM::Interface, DCIM::InventoryItem, DCIM::Manufacturer, DCIM::Platform, DCIM::PowerOutlet, DCIM::PowerPort, DCIM::Rack, DCIM::RackGroup, DCIM::Region, DCIM::Site, IPAM::Aggregate, IPAM::IpAddress, IPAM::Prefix, IPAM::Rir, IPAM::Role, IPAM::Vlan, IPAM::VlanGroup, IPAM::Vrf, Secrets::Secret, Secrets::SecretRole, Tenancy::Tenant, Tenancy::TenantGroup, Virtualization::Cluster, Virtualization::ClusterGroup, Virtualization::ClusterType, Virtualization::Interface, Virtualization::VirtualMachine
- Defined in:
- lib/netbox_client_ruby/entity.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary
collapse
Instance Method Summary
collapse
#connection, #hash_to_object, #response
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name_as_symbol, *args, &block) ⇒ Object
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
# File 'lib/netbox_client_ruby/entity.rb', line 189
def method_missing(name_as_symbol, *args, &block)
name = name_as_symbol.to_s
if name.end_with?('=')
is_readonly_field = readonly_fields.include?(name[0..-2])
is_instance_variable = instance_variables.include?("@#{name[0..-2]}".to_sym)
not_this_classes_business = is_readonly_field || is_instance_variable
return super if not_this_classes_business
dirty_data[name[0..-2]] = args[0]
return args[0]
end
return dirty_data[name] if dirty_data.key? name
return objectify(data[name], object_fields[name]) if object_fields.key? name
return data[name] if data.is_a?(Hash) && data.key?(name)
super
end
|
Class Method Details
.included(other_klass) ⇒ Object
8
9
10
|
# File 'lib/netbox_client_ruby/entity.rb', line 8
def self.included(other_klass)
other_klass.extend ClassMethods
end
|
Instance Method Details
#[](name) ⇒ Object
180
181
182
183
|
# File 'lib/netbox_client_ruby/entity.rb', line 180
def [](name)
s_name = name.to_s
dirty_data[s_name] || data[s_name]
end
|
#[]=(name, value) ⇒ Object
185
186
187
|
# File 'lib/netbox_client_ruby/entity.rb', line 185
def []=(name, value)
dirty_data[name.to_s] = value
end
|
#create(raw_data) ⇒ Object
143
144
145
146
147
148
|
# File 'lib/netbox_client_ruby/entity.rb', line 143
def create(raw_data)
raise LocalError, "Can't 'create', this object already exists" if ids_set?
@dirty_data = raw_data
post
end
|
#data=(data) ⇒ Object
:internal: Used to pre-populate an entity
225
226
227
|
# File 'lib/netbox_client_ruby/entity.rb', line 225
def data=(data)
@data = data
end
|
#delete ⇒ Object
Also known as:
remove
150
151
152
153
154
155
156
157
158
|
# File 'lib/netbox_client_ruby/entity.rb', line 150
def delete
raise NetboxClientRuby::LocalError, "Can't delete unless deletable=true" unless deletable
return self if @deleted
@data = response connection.delete path
@deleted = true
revert
self
end
|
#initialize(given_values = nil) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/netbox_client_ruby/entity.rb', line 107
def initialize(given_values = nil)
return self if given_values.nil?
if id_fields.count == 1 && !given_values.is_a?(Hash)
instance_variable_set("@#{id_fields.keys.first}", given_values)
return self
end
given_values.each do |field, value|
if id_fields.key? field.to_s
instance_variable_set "@#{field}", value
else
method_missing("#{field}=", value)
end
end
self
end
|
#raw_data! ⇒ Object
176
177
178
|
# File 'lib/netbox_client_ruby/entity.rb', line 176
def raw_data!
data
end
|
#reload ⇒ Object
Also known as:
get!
132
133
134
135
136
|
# File 'lib/netbox_client_ruby/entity.rb', line 132
def reload
@data = get
revert
self
end
|
#respond_to_missing?(name_as_symbol, *args) ⇒ Boolean
210
211
212
213
214
215
216
217
218
219
220
221
|
# File 'lib/netbox_client_ruby/entity.rb', line 210
def respond_to_missing?(name_as_symbol, *args)
name = name_as_symbol.to_s
return false if name.end_with?('=') && readonly_fields.include?(name[0..-2])
return false if name.end_with?('=') && instance_variables.include?(name[0..-2])
return true if dirty_data.key? name
return true if object_fields.key? name
return true if data.is_a?(Hash) && data.key?(name)
super
end
|
#revert ⇒ Object
127
128
129
130
|
# File 'lib/netbox_client_ruby/entity.rb', line 127
def revert
dirty_data.clear
self
end
|
#save ⇒ Object
138
139
140
141
|
# File 'lib/netbox_client_ruby/entity.rb', line 138
def save
return post unless ids_set?
patch
end
|
#update(new_values) ⇒ Object
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
# File 'lib/netbox_client_ruby/entity.rb', line 160
def update(new_values)
new_values.each do |attribute, values|
s_attribute = attribute.to_s
next if readonly_fields.include? s_attribute
sym_attr_writer = "#{attribute}=".to_sym
if methods.include?(sym_attr_writer)
public_send(sym_attr_writer, values)
else
dirty_data[s_attribute] = values
end
end
patch
end
|