Skip to content

Commit

Permalink
Some redundant nil checks (either safe navigation operatior or condit…
Browse files Browse the repository at this point in the history
…ionals) to make type check pass

Come back to this
  • Loading branch information
stevegeek committed Dec 23, 2022
1 parent ce1153b commit 6cbff32
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
16 changes: 10 additions & 6 deletions lib/quo/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ def first(limit = nil)
if transform?
res = query_with_logging.first(limit)
if res.is_a? Array
res.map.with_index { |r, i| transformer.call(r, i) }
res.map.with_index { |r, i| transformer&.call(r, i) }
elsif !res.nil?
transformer.call(query_with_logging.first(*args))
transformer&.call(query_with_logging.first(limit))
end
else
query_with_logging.first(limit)
Expand All @@ -117,9 +117,9 @@ def last(limit = nil)
if transform?
res = query_with_logging.last(limit)
if res.is_a? Array
res.map.with_index { |r, i| transformer.call(r, i) }
res.map.with_index { |r, i| transformer&.call(r, i) }
elsif !res.nil?
transformer.call(query_with_logging.last(*args))
transformer&.call(res)
end
else
query_with_logging.last(limit)
Expand All @@ -129,7 +129,7 @@ def last(limit = nil)
# Convert to array
def to_a
arr = query_with_logging.to_a
transform? ? arr.map.with_index { |r, i| transformer.call(r, i) } : arr
transform? ? arr.map.with_index { |r, i| transformer&.call(r, i) } : arr
end

# Convert to EagerQuery, and load all data
Expand Down Expand Up @@ -226,7 +226,11 @@ def transformer

def offset
per_page = sanitised_page_size
page = current_page&.positive? ? current_page : 1
page = if current_page && current_page&.positive?
current_page
else
1
end
per_page * (page - 1)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/quo/results.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def group_by(&block)
grouped = unwrapped.group_by do |*block_args|
x = block_args.first
transformed = transformer ? transformer.call(x) : x
block.call(transformed, *block_args[1..])
block ? block.call(transformed, *(block_args[1..] || [])) : transformed
end

grouped.tap do |groups|
Expand Down
2 changes: 1 addition & 1 deletion lib/quo/utilities/callstack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def debug_callstack
stack = Kernel.caller.grep_v(exclude).map { |l| l.gsub(working_dir + "/", "") }
stack_to_display = stack[0..callstack_size]
message = "\n[Query stack]: -> #{stack_to_display&.join("\n &> ")}\n"
message += " (truncated to #{callstack_size} most recent)" if stack.size > callstack_size
message += " (truncated to #{callstack_size} most recent)" if callstack_size && stack.size > callstack_size
Quo.configuration.logger&.info(message)
end
end
Expand Down

0 comments on commit 6cbff32

Please sign in to comment.