Sha256: 5a61aa53ac2529d25ebc9be2aa507254fb493940634e13ac864f799776f8b0e6

Contents?: true

Size: 970 Bytes

Versions: 1

Compression:

Stored size: 970 Bytes

Contents

module CassandraObject
  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 CassandraObject::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 CassandraObject::RecordNotFound, "Couldn't find #{self.name} with key #{id.inspect}"
          elsif record = where(adapter.primary_key_column => id).first
            record
          else
            raise CassandraObject::RecordNotFound
          end
        end

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

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

          where(adapter.primary_key_column => ids).to_a
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gotime-cassandra_object-4.11.6 lib/cassandra_object/scope/finder_methods.rb