Skip to content

Commit

Permalink
Tweaking result predecates
Browse files Browse the repository at this point in the history
  • Loading branch information
stevegeek committed Sep 25, 2024
1 parent cda14df commit 717adf8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
16 changes: 4 additions & 12 deletions lib/quo/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ def results #: Quo::Results
:find,
:include?,
:each_with_object,
:none?,
:any?,
:empty?,
:exists?,
to: :results

# @rbs @__transformer: nil | ^(untyped, ?Integer) -> untyped
Expand All @@ -313,18 +317,6 @@ def transform(&block)
self
end

# Are there any results for this query?
def exists? #: bool
return configured_query.exists? if relation?
configured_query.present?
end

# Are there no results for this query?
def none? #: bool
!exists?
end
alias_method :empty?, :none?

# Is this query object a ActiveRecord relation under the hood?
def relation? #: bool
test_relation(configured_query)
Expand Down
18 changes: 17 additions & 1 deletion lib/quo/results.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ def initialize(query, transformer: nil)
@transformer = transformer
end

# TODO: RBS for these,
# @rbs!
# def include?: () -> bool
# def member?: () -> bool
# def all?: () -> bool
# def any?: () -> bool
# def none?: () -> bool
# def one?: () -> bool
def_delegators :unwrapped,
:include?,
:member?,
Expand All @@ -28,6 +34,16 @@ def initialize(query, transformer: nil)

def_delegators :query, :count

# Are there any results for this query?
def exists? #: bool
return unwrapped.exists? if query.relation?
configured_query.present?
end

def empty? #: bool
!exists?
end

# @rbs &block: (untyped, *untyped) -> untyped
# @rbs return: Hash[untyped, Array[untyped]]
def group_by(&block)
Expand Down
10 changes: 2 additions & 8 deletions sig/generated/quo/query.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ module Quo

def previous_page_query: () -> Quo::Query

def offset: () -> Integer

# Returns a active record query, or a Quo::Query instance
def query: () -> (Quo::Query | ::ActiveRecord::Relation)

Expand Down Expand Up @@ -148,12 +150,6 @@ module Quo
# @rbs return: self
def transform: () ?{ (?) -> untyped } -> self

# Are there any results for this query?
def exists?: () -> bool

# Are there no results for this query?
def none?: () -> bool

# Is this query object a ActiveRecord relation under the hood?
def relation?: () -> bool

Expand Down Expand Up @@ -181,8 +177,6 @@ module Quo

def transformer: () -> untyped

def offset: () -> Integer

# The configured query is the underlying query with paging
def configured_query: () -> ActiveRecord::Relation

Expand Down
17 changes: 17 additions & 0 deletions sig/generated/quo/results.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ module Quo
# @rbs return: void
def initialize: (Quo::Query query, ?transformer: (^(untyped, ?Integer) -> untyped)?) -> void

def include?: () -> bool

def member?: () -> bool

def all?: () -> bool

def any?: () -> bool

def none?: () -> bool

def one?: () -> bool

# Are there any results for this query?
def exists?: () -> bool

def empty?: () -> bool

# @rbs &block: (untyped, *untyped) -> untyped
# @rbs return: Hash[untyped, Array[untyped]]
def group_by: () { (untyped, *untyped) -> untyped } -> Hash[untyped, Array[untyped]]
Expand Down

0 comments on commit 717adf8

Please sign in to comment.