Sha256: 1d1ba1954861859c156fd887ad1c30079fe424ecb0eaeb24451b7f8250446de1

Contents?: true

Size: 807 Bytes

Versions: 3

Compression:

Stored size: 807 Bytes

Contents

module Superstore
  module FinderMethods
    def find(ids)
      if ids.is_a?(Array)
        find_some(ids)
      else
        find_one(ids)
      end
    end

    def find_by_id(ids)
      find(ids)
    rescue Superstore::RecordNotFound
      nil
    end

    def all
      clone
    end

    def first
      limit(1).to_a.first
    end

    private

      def find_one(id)
        if id.blank?
          raise Superstore::RecordNotFound, "Couldn't find #{self.name} with key #{id.inspect}"
        elsif record = where_ids(id).first
          record
        else
          raise Superstore::RecordNotFound
        end
      end

      def find_some(ids)
        ids = ids.flatten
        return [] if ids.empty?

        ids = ids.compact.map(&:to_s).uniq

        where_ids(ids).to_a
      end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
superstore-2.1.0 lib/superstore/scope/finder_methods.rb
superstore-2.0.1 lib/superstore/scope/finder_methods.rb
superstore-2.0.0 lib/superstore/scope/finder_methods.rb