diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..54d1a4f --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.13.0 diff --git a/lib/netbox_client_ruby.rb b/lib/netbox_client_ruby.rb index a93bf0c..28df3dc 100644 --- a/lib/netbox_client_ruby.rb +++ b/lib/netbox_client_ruby.rb @@ -68,6 +68,16 @@ module NetboxClientRuby setting :ssl_options, default: { verify: true } end + def self.load_collection(collection, method_name, class_name) + collection.define_method(method_name) { class_name.new } + collection.__send__(:module_function, method_name) + end + + def self.load_entity(entity, method_name, class_name) + entity.define_method(method_name) { |id| class_name.new id } + entity.__send__(:module_function, method_name) + end + def self.circuits NetboxClientRuby::Circuits end diff --git a/lib/netbox_client_ruby/api/circuits.rb b/lib/netbox_client_ruby/api/circuits.rb index 652aa15..283c9d0 100644 --- a/lib/netbox_client_ruby/api/circuits.rb +++ b/lib/netbox_client_ruby/api/circuits.rb @@ -8,8 +8,7 @@ module Circuits circuit_types: CircuitTypes, circuit_terminations: CircuitTerminations, }.each_pair do |method_name, class_name| - define_method(method_name) { class_name.new } - module_function(method_name) + NetboxClientRuby.load_collection(self, method_name, class_name) end { @@ -18,8 +17,7 @@ module Circuits circuit_type: CircuitType, circuit_termination: CircuitTermination, }.each_pair do |method_name, class_name| - define_method(method_name) { |id| class_name.new id } - module_function(method_name) + NetboxClientRuby.load_entity(self, method_name, class_name) end end end diff --git a/lib/netbox_client_ruby/api/dcim.rb b/lib/netbox_client_ruby/api/dcim.rb index 9eb2483..3386f50 100644 --- a/lib/netbox_client_ruby/api/dcim.rb +++ b/lib/netbox_client_ruby/api/dcim.rb @@ -26,8 +26,7 @@ module DCIM sites: Sites, virtual_chassis_list: VirtualChassisList, }.each_pair do |method_name, class_name| - define_method(method_name) { class_name.new } - module_function(method_name) + NetboxClientRuby.load_collection(self, method_name, class_name) end { @@ -54,8 +53,7 @@ module DCIM site: Site, virtual_chassis: VirtualChassis, }.each_pair do |method_name, class_name| - define_method(method_name) { |id| class_name.new id } - module_function(method_name) + NetboxClientRuby.load_entity(self, method_name, class_name) end end end diff --git a/lib/netbox_client_ruby/api/extras.rb b/lib/netbox_client_ruby/api/extras.rb index 639acf6..f49a4e6 100644 --- a/lib/netbox_client_ruby/api/extras.rb +++ b/lib/netbox_client_ruby/api/extras.rb @@ -7,8 +7,7 @@ module Extras journal_entries: JournalEntries, tags: Tags, }.each_pair do |method_name, class_name| - define_method(method_name) { class_name.new } - module_function(method_name) + NetboxClientRuby.load_collection(self, method_name, class_name) end { @@ -16,8 +15,7 @@ module Extras journal_entry: JournalEntry, tag: Tag, }.each_pair do |method_name, class_name| - define_method(method_name) { |id| class_name.new id } - module_function(method_name) + NetboxClientRuby.load_entity(self, method_name, class_name) end end end diff --git a/lib/netbox_client_ruby/api/ipam.rb b/lib/netbox_client_ruby/api/ipam.rb index f87bb9e..865667e 100644 --- a/lib/netbox_client_ruby/api/ipam.rb +++ b/lib/netbox_client_ruby/api/ipam.rb @@ -14,8 +14,7 @@ module IPAM vlan_groups: VlanGroups, vrfs: Vrfs, }.each_pair do |method_name, class_name| - define_method(method_name) { class_name.new } - module_function(method_name) + NetboxClientRuby.load_collection(self, method_name, class_name) end { @@ -30,8 +29,7 @@ module IPAM vlan_group: VlanGroup, vrf: Vrf, }.each_pair do |method_name, class_name| - define_method(method_name) { |id| class_name.new id } - module_function(method_name) + NetboxClientRuby.load_entity(self, method_name, class_name) end end end diff --git a/lib/netbox_client_ruby/api/secrets.rb b/lib/netbox_client_ruby/api/secrets.rb index 5941c00..a550192 100644 --- a/lib/netbox_client_ruby/api/secrets.rb +++ b/lib/netbox_client_ruby/api/secrets.rb @@ -8,16 +8,14 @@ module Secrets generate_rsa_key_pair: RSAKeyPair, get_session_key: SessionKey, }.each_pair do |method_name, class_name| - define_method(method_name) { class_name.new } - module_function(method_name) + NetboxClientRuby.load_collection(self, method_name, class_name) end { secret_role: SecretRole, secret: Secret, }.each_pair do |method_name, class_name| - define_method(method_name) { |id| class_name.new id } - module_function(method_name) + NetboxClientRuby.load_entity(self, method_name, class_name) end def session_key=(session_key) diff --git a/lib/netbox_client_ruby/api/tenancy.rb b/lib/netbox_client_ruby/api/tenancy.rb index 0b885d9..57182f5 100644 --- a/lib/netbox_client_ruby/api/tenancy.rb +++ b/lib/netbox_client_ruby/api/tenancy.rb @@ -8,8 +8,7 @@ module Tenancy contacts: Contacts, contact_groups: ContactGroups, }.each_pair do |method_name, class_name| - define_method(method_name) { class_name.new } - module_function(method_name) + NetboxClientRuby.load_collection(self, method_name, class_name) end { @@ -18,8 +17,7 @@ module Tenancy contact: Contact, contact_group: ContactGroup, }.each_pair do |method_name, class_name| - define_method(method_name) { |id| class_name.new id } - module_function(method_name) + NetboxClientRuby.load_entity(self, method_name, class_name) end end end diff --git a/lib/netbox_client_ruby/api/virtualization.rb b/lib/netbox_client_ruby/api/virtualization.rb index 9235420..0aeb50c 100644 --- a/lib/netbox_client_ruby/api/virtualization.rb +++ b/lib/netbox_client_ruby/api/virtualization.rb @@ -9,8 +9,7 @@ module Virtualization virtual_machines: VirtualMachines, interfaces: Interfaces, }.each_pair do |method_name, class_name| - define_method(method_name) { class_name.new } - module_function(method_name) + NetboxClientRuby.load_collection(self, method_name, class_name) end { @@ -20,8 +19,7 @@ module Virtualization virtual_machine: VirtualMachine, interface: Interface, }.each_pair do |method_name, class_name| - define_method(method_name) { |id| class_name.new id } - module_function(method_name) + NetboxClientRuby.load_entity(self, method_name, class_name) end end end diff --git a/lib/netbox_client_ruby/entity.rb b/lib/netbox_client_ruby/entity.rb index fc97369..914a228 100644 --- a/lib/netbox_client_ruby/entity.rb +++ b/lib/netbox_client_ruby/entity.rb @@ -21,25 +21,12 @@ module ClassMethods # id 'an_id_field' => 'id_field_in_data' # id an_id_field: 'id_field_in_data', :another_id_field: 'id_field2_in_data' # - def id(*fields) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity + 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.each_key do |field| - define_method(field) { instance_variable_get :"@#{field}" } - end - + @id_fields = load_attributes(fields) @id_fields end @@ -88,6 +75,8 @@ def object_fields(*fields_to_class_map) end end + private + def sanitize_mapping(fields_to_class_map) # rubocop:disable Metrics/MethodLength fields_map = {} fields_to_class_map.each do |field_definition| @@ -101,6 +90,25 @@ def sanitize_mapping(fields_to_class_map) # rubocop:disable Metrics/MethodLength end fields_map end + + def load_attributes(fields) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength + 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.each_key do |field| + define_method(field) { instance_variable_get :"@#{field}" } + end + + id_fields + end end def initialize(given_values = nil) # rubocop:disable Metrics/MethodLength diff --git a/lib/netbox_client_ruby/version.rb b/lib/netbox_client_ruby/version.rb new file mode 100644 index 0000000..9d338d2 --- /dev/null +++ b/lib/netbox_client_ruby/version.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module NetboxClientRuby + def self.gem_version + Gem::Version.new VERSION::STRING + end + + module VERSION + STRING = File.read(File.expand_path('../../VERSION', __dir__)).strip + end +end diff --git a/netbox-client-ruby.gemspec b/netbox-client-ruby.gemspec index 31ec3f9..029a846 100644 --- a/netbox-client-ruby.gemspec +++ b/netbox-client-ruby.gemspec @@ -1,8 +1,10 @@ # frozen_string_literal: true +require_relative 'lib/netbox_client_ruby/version' + Gem::Specification.new do |spec| spec.name = 'netbox-client-ruby' - spec.version = `git describe --tags --match="v[0-9]*" --abbrev=0`.strip.delete_prefix("v") + spec.version = NetboxClientRuby::VERSION::STRING spec.summary = 'A read/write client for Netbox v2.' spec.homepage = 'https://github.com/ninech/netbox-client-ruby' @@ -19,9 +21,7 @@ Gem::Specification.new do |spec| spec.metadata['bug_tracker_uri'] = 'https://github.com/ninech/netbox-client-ruby/issues' end - spec.files = `git ls-files -z`.split("\x0").reject do |f| - f.match(%r{^(test|spec|features)/}) - end + spec.files = Dir['README.md', 'LICENSE.txt', 'VERSION', 'lib/**/*.rb'] spec.required_ruby_version = '>= 2.7.0'