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



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

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



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

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

#[]=(name, value) ⇒ Object



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

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

#create(raw_data) ⇒ Object

Raises:



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

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



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

def data=(data)
  @data = data
end

#deleteObject Also known as: remove



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

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

  self
end

#raw_data!Object



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

def raw_data!
  data
end

#reloadObject Also known as: get!

Raises:



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

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)


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

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



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

def revert
  dirty_data.clear
  self
end

#saveObject



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

def save
  return post unless ids_set?
  patch
end

#update(new_values) ⇒ Object



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

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

#urlObject



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

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