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.34.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.33.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.32.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.31.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.30.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.29.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.28.1 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.28.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.27.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.26.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.25.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.24.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.23.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.22.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.21.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.20.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.19.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.18.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.17.0 lib/erp_integration/fulfil/finder_methods.rb
erp_integration-0.16.0 lib/erp_integration/fulfil/finder_methods.rb