Skip to content

where: ranges: exclusive ranges are treated as inclusive, and endless/beginless ranges raise RangeError #400

Backlog

Description

@hackermengzhi

Version

18.5.0 (master at 0f1bdaa)

Summary

The README shows date ranges in where: (e.g. date: (DateTime.now - 2.year)..DateTime.now). XeroRuby::Where turns any Range into >= first AND <= last:

https://github.com/XeroAPI/xero-ruby/blob/0f1bdaa/lib/xero-ruby/where.rb#L32-L33

when Range
  "#{quoted_key} >= #{quote_value(value.first)} AND #{quoted_key} <= #{quote_value(value.last)}"

This has two problems:

  1. exclude_end? is ignored, so a...b also matches b.
  2. Range#first / Range#last raise on beginless and endless ranges, so idiomatic Ruby like date: (Date.new(2026, 1, 1)..) crashes before any request is made.

Steps to reproduce

XeroRuby::Where.new(amount_due: (100...200)).to_param
# => "AmountDue >= 100 AND AmountDue <= 200"

XeroRuby::Where.new(date: (Date.new(2026,1,1)...Date.new(2026,2,1))).to_param
# => "Date >= DateTime(2026,01,01) AND Date <= DateTime(2026,02,01)"

XeroRuby::Where.new(date: (Date.new(2026,1,1)..)).to_param
# => RangeError: cannot get the last element of endless range

Expected

AmountDue >= 100 AND AmountDue < 200
Date >= DateTime(2026,01,01) AND Date < DateTime(2026,02,01)
Date >= DateTime(2026,01,01)

Suggested fix

when Range
  parts = []
  parts << "#{quoted_key} >= #{quote_value(value.begin)}" unless value.begin.nil?
  unless value.end.nil?
    op = value.exclude_end? ? '<' : '<='
    parts << "#{quoted_key} #{op} #{quote_value(value.end)}"
  end
  parts.join(' AND ')

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions