Module: NetboxClientRuby::Entity

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Communication

#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



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/netbox_client_ruby/entity.rb', line 194

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]}")
    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



7
8
9
# File 'lib/netbox_client_ruby/entity.rb', line 7

def self.included(other_klass)
  other_klass.extend ClassMethods
end

Instance Method Details

#[](name) ⇒ Object



185
186
187
188
# File 'lib/netbox_client_ruby/entity.rb', line 185

def [](name)
  s_name = name.to_s
  dirty_data[s_name] || data[s_name]
end

#[]=(name, value) ⇒ Object



190
191
192
# File 'lib/netbox_client_ruby/entity.rb', line 190

def []=(name, value)
  dirty_data[name.to_s] = value
end

#create(raw_data) ⇒ Object

Raises:



144
145
146
147
148
149
# File 'lib/netbox_client_ruby/entity.rb', line 144

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



230
231
232
# File 'lib/netbox_client_ruby/entity.rb', line 230

def data=(data)
  @data = data
end

#deleteObject Also known as: remove



151
152
153
154
155
156
157
158
159
# File 'lib/netbox_client_ruby/entity.rb', line 151

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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/netbox_client_ruby/entity.rb', line 106

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
      # via method_missing, because it checks for readonly fields, etc.
      method_missing("#{field}=", value)
    end
  end

  self
end

#raw_data!Object



181
182
183
# File 'lib/netbox_client_ruby/entity.rb', line 181

def raw_data!
  data
end

#reloadObject Also known as: get!

Raises:



131
132
133
134
135
136
137
# File 'lib/netbox_client_ruby/entity.rb', line 131

def reload
  raise LocalError, "Can't 'reload', this object has never been saved" unless ids_set?

  @data = get
  revert
  self
end

#respond_to_missing?(name_as_symbol, *args) ⇒ Boolean

Returns:

  • (Boolean)


215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/netbox_client_ruby/entity.rb', line 215

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

#revertObject



126
127
128
129
# File 'lib/netbox_client_ruby/entity.rb', line 126

def revert
  dirty_data.clear
  self
end

#saveObject



139
140
141
142
# File 'lib/netbox_client_ruby/entity.rb', line 139

def save
  return post unless ids_set?
  patch
end

#update(new_values) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/netbox_client_ruby/entity.rb', line 161

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}="
    if methods.include?(sym_attr_writer)
      public_send(sym_attr_writer, values)
    else
      dirty_data[s_attribute] = values
    end
  end

  patch
end

#urlObject



177
178
179
# File 'lib/netbox_client_ruby/entity.rb', line 177

def url
  "#{connection.url_prefix}#{path}"
end