Module: NetboxClientRuby::Entity::ClassMethods

Defined in:
lib/netbox_client_ruby/entity.rb

Instance Method Summary collapse

Instance Method Details

#creation_path(creation_path = nil) ⇒ Object

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
# File 'lib/netbox_client_ruby/entity.rb', line 64

def creation_path(creation_path = nil)
  @creation_path ||= creation_path

  return @creation_path if @creation_path

  raise ArgumentError, "No argument to 'creation_path' was given."
end

#deletable(deletable = false) ⇒ Object



52
53
54
# File 'lib/netbox_client_ruby/entity.rb', line 52

def deletable(deletable = false)
  @deletable ||= deletable
end

#id(*fields) ⇒ Object

Expects ids in the following format:

id 'an_id_field'
id :an_id_field
id 'an_id_field', 'another_id_field'
id :an_id_field, :another_id_field
id an_id_field: 'id_field_in_data'
id an_id_field: :id_field_in_data
id 'an_id_field' => :id_field_in_data
id 'an_id_field' => 'id_field_in_data'
id an_id_field: 'id_field_in_data', :another_id_field: 'id_field2_in_data'

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/netbox_client_ruby/entity.rb', line 24

def id(*fields)
  return @id_fields if @id_fields

  raise ArgumentError, "No 'id' was defined, but one is expected." if fields.empty?

  @id_fields = {}
  if fields.first.is_a?(Hash)
    fields.first.each { |key, value| @id_fields[key.to_s] = value.to_s }
  else
    fields.map(&:to_s).each do |field|
      field_as_string = field.to_s
      @id_fields[field_as_string] = field_as_string
    end
  end

  @id_fields.keys.each do |field|
    define_method(field) { instance_variable_get :"@#{field}" }
  end

  @id_fields
end

#object_fields(*fields_to_class_map) ⇒ Object

allowed values: object_fields :field_a, :field_b, :field_c, 'fieldname_as_string' # next is ame as previous object_fields field_a: nil, field_b: nil, field_c: nil, 'fieldname_as_string': nil # next defines the types of the objects object_fields field_a: Klass, field_b: proc { |data| Klass.new data }, field_c: nil, 'fieldname_as_string'



80
81
82
83
84
85
86
87
88
89
# File 'lib/netbox_client_ruby/entity.rb', line 80

def object_fields(*fields_to_class_map)
  return @object_fields if @object_fields

  if fields_to_class_map.nil? || fields_to_class_map.empty?
    @object_fields = {}
  else
    fields_map = sanitize_mapping(fields_to_class_map)
    @object_fields = fields_map
  end
end

#path(path = nil) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
# File 'lib/netbox_client_ruby/entity.rb', line 56

def path(path = nil)
  @path ||= path

  return @path if @path

  raise ArgumentError, "No argument to 'path' was given."
end

#readonly_fields(*fields) ⇒ Object



46
47
48
49
50
# File 'lib/netbox_client_ruby/entity.rb', line 46

def readonly_fields(*fields)
  return @readonly_fields if @readonly_fields

  @readonly_fields = fields.map(&:to_s)
end

#sanitize_mapping(fields_to_class_map) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/netbox_client_ruby/entity.rb', line 91

def sanitize_mapping(fields_to_class_map)
  fields_map = {}
  fields_to_class_map.each do |field_definition|
    if field_definition.is_a?(Hash)
      fields_to_class_map[0].each do |field, klass_or_proc|
        fields_map[field.to_s] = klass_or_proc
      end
    else
      fields_map[field_definition.to_s] = nil
    end
  end
  fields_map
end