Sha256: f5062dfb2d3d1613c33e7f60a6513fe744baf183604a3d1bffbeb69f5ce52d31

Contents?: true

Size: 898 Bytes

Versions: 18

Compression:

Stored size: 898 Bytes

Contents

module Superstore
  class Scope
    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
        to_a
      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
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
superstore-1.2.0 lib/superstore/scope/finder_methods.rb
superstore-1.1.4 lib/superstore/scope/finder_methods.rb
superstore-1.1.3 lib/superstore/scope/finder_methods.rb
superstore-1.1.2 lib/superstore/scope/finder_methods.rb
superstore-1.1.1 lib/superstore/scope/finder_methods.rb
superstore-1.1.0 lib/superstore/scope/finder_methods.rb
superstore-1.0.12 lib/superstore/scope/finder_methods.rb
superstore-1.0.11 lib/superstore/scope/finder_methods.rb
superstore-1.0.10 lib/superstore/scope/finder_methods.rb
superstore-1.0.9 lib/superstore/scope/finder_methods.rb
superstore-1.0.8 lib/superstore/scope/finder_methods.rb
superstore-1.0.7 lib/superstore/scope/finder_methods.rb
superstore-1.0.6 lib/superstore/scope/finder_methods.rb
superstore-1.0.5 lib/superstore/scope/finder_methods.rb
superstore-1.0.4 lib/superstore/scope/finder_methods.rb
superstore-1.0.3 lib/superstore/scope/finder_methods.rb
superstore-1.0.2 lib/superstore/scope/finder_methods.rb
superstore-1.0.0 lib/superstore/scope/finder_methods.rb