Sha256: f1391f540eceda8a6a23fc9de84209319562583a1e9d4f7b788776edfbcd83af

Contents?: true

Size: 1.82 KB

Versions: 50

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

module ErpIntegration
  module Fulfil
    # The `FinderMethods` add various, simplistic lookup methods to find API resources.
    # These methods should be the last method in the chain as it will invoke the HTTP
    # request to Fulfil immediately.
    #
    # If you need more attributes than the default ID attribute for an ApiResource
    # to be returned, add the `select` method before the finder method is being called
    # in your application code.
    #
    # @example
    #   $ ErpIntegration::SalesOrder.select(:id, :product).find(100)
    #   => <ErpIntegration::SalesOrder @id=100 @product=10 />
    module FinderMethods
      # Looks up an individual resource based on resource's ID.
      #
      # @example
      #   $ ErpIntegration::SalesOrder.find(100)
      #   => <ErpIntegration::SalesOrder @id=100 />
      #
      # @param id [Integer] The ID of the API resource.
      # @return [ApiResource] The found resource in Fulfil.
      def find(id)
        find_by!(id: id) || raise(ResourceNotFound)
      end

      # Looks up an individual resource based on the given attributes.
      #
      # @example
      #   $ ErpIntegration::SalesOrder.find_by(product: 10)
      #   => <ErpIntegration::SalesOrder @id=100 />
      #
      # @return [ApiResource|nil] The found resource in Fulfil.
      def find_by(args)
        where(args).first
      end

      # Looks up an individual resource based on the given attributes and raises
      # an `ErpIntegration::ResourceNotFound` when the resource doesn't exist.
      #
      # @example
      #   $ ErpIntegration::SalesOrder.find_by!(product: 10)
      #   => ErpIntegration::ResourceNotFound
      #
      # @return [ApiResource|nil] The found resource in Fulfil.
      def find_by!(args)
        find_by(args) || raise(ResourceNotFound)
      end
    end
  end
end

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
erp_integration-0.57.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.56.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.55.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.54.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.53.1 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.53.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.52.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.50.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.49.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.48.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.43.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.42.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.41.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.40.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.39.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.38.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.37.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.36.1 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.36.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.35.0 lib/erp_integration/fulfil/finder_methods.rb