Sha256: 23c0fd016928958b7ce5558725371e0962f34583b31580cf30c929c7cb8ae4ac

Contents?: true

Size: 1.43 KB

Versions: 6

Compression:

Stored size: 1.43 KB

Contents

# typed: strict
# frozen_string_literal: true

module DearInventory
  class EndpointClass
    extend T::Sig

    sig do
      params(
        class_type: String,
        resource_class: T.class_of(DearInventory::Resource),
        endpoint: T.nilable(String)
      ).returns(
        T.nilable(T.class_of(DearInventory::Parameters))
      )
    end
    def self.call(class_type:, resource_class:, endpoint:)
      new(
        class_type: class_type,
        resource_class: resource_class,
        endpoint: endpoint
      ).call
    end

    sig do
      params(
        class_type: String,
        resource_class: T.class_of(DearInventory::Resource),
        endpoint: T.nilable(String)
      ).void
    end
    def initialize(class_type:, resource_class:, endpoint:)
      @class_type = T.let(class_type, String)
      @resource_class = T.let(resource_class, T.untyped)
      @endpoint = T.let(endpoint || "Index", String)
      @class_name = T.let(nil, T.nilable(String))
    end

    sig { returns(T.nilable(T.class_of(DearInventory::Parameters))) }
    def call
      Object.const_get(class_name) if Object.const_defined?(class_name)
    end

    private

    sig { returns(String) }
    def class_name
      @class_name ||= begin
        name = ["DearInventory"]
        name << @class_type
        name << @resource_class.name.split("::").last
        name << @endpoint.split("_").map(&:capitalize).join
        name.join("::")
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dear_inventory-1.4.0 lib/dear_inventory/lib/endpoint_class.rb
dear_inventory-1.3.0 lib/dear_inventory/lib/endpoint_class.rb
dear_inventory-1.2.0 lib/dear_inventory/lib/endpoint_class.rb
dear_inventory-1.1.1 lib/dear_inventory/lib/endpoint_class.rb
dear_inventory-1.1.0 lib/dear_inventory/lib/endpoint_class.rb
dear_inventory-1.0.0 lib/dear_inventory/lib/endpoint_class.rb